解题思路:
https://www.acwing.com/solution/content/33961/
代码如下:
#include <iostream>
using namespace std;
const int N = 200010;
int w[N],v[N],L[N],R[N];
typedef long long LL;
LL cnt[N],b[N];
int n,m;
LL S;
LL get_Y(int mid)
{for (int i = 1;i<=n;i++){if (w[i] >= mid){b[i] = b[i-1]+v[i];cnt[i] = cnt[i-1]+1;}else{b[i] = b[i-1];cnt[i] = cnt[i-1];}}LL res = 0;for (int i = 0;i<m;i++){res+=(b[R[i]]-b[L[i]-1])*(cnt[R[i]]-cnt[L[i]-1]);}return res;
}int main()
{cin>>n>>m>>S;for (int i = 1;i<=n;i++) cin>>w[i]>>v[i];for (int i = 0;i<m;i++) cin>>L[i]>>R[i];int l = 0,r = 1e6+1;while(l < r){int mid = l+r+1>>1;if (get_Y(mid)>=S) l = mid;else r = mid-1;}cout<<min(abs(S-get_Y(r)),abs(S-get_Y(r+1)))<<endl;return 0;
}