https://www.acwing.com/problem/content/501/
1 #include<iostream> 2 using namespace std; 3 const int N=200010; 4 typedef long long LL; 5 int v[N],w[N]; 6 int L[N],R[N]; 7 int n,m; 8 LL S; 9 LL sum[N],cnt[N]; 10 LL get_Y(int W){ 11 for(int i=1;i<=n;i++){ 12 if(w[i]>=W){ 13 sum[i]=sum[i-1]+v[i]; 14 cnt[i]=cnt[i-1]+1; 15 }else{ 16 sum[i]=sum[i-1]; 17 cnt[i]=cnt[i-1]; 18 } 19 } 20 LL res=0; 21 for(int i=1;i<=m;i++){ 22 res += (sum[R[i]] - sum[L[i] - 1]) * (cnt[R[i]] - cnt[L[i] - 1]); 23 } 24 return res; 25 } 26 int main(void){ 27 cin>>n>>m>>S; 28 for(int i=1;i<=n;i++){ 29 cin>>w[i]>>v[i]; 30 } 31 for(int i=1;i<=m;i++){ 32 cin>>L[i]>>R[i]; 33 } 34 int l=0,r=1e6+1; 35 while(l<r){ 36 int mid=l+r+1>>1; 37 if(get_Y(mid)>=S){ 38 l=mid; 39 }else{ 40 r=mid-1; 41 } 42 } 43 cout<<min(get_Y(r)-S,S-get_Y(r+1)); 44 return 0; 45 }