https://www.luogu.org/problemnew/show/P1314
题解:二分+前缀和
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const ll INF = 0x3f3f3f3f3f3f3f;
ll t,n,m,k,p,l,r;
ll ans,cnt,flag,temp,sum;
int w[N],v[N],c[N];
char str;
long long pre_n[N],pre_v[N];
long long Y;
struct node{
int l,r;
bool operator <(const node &S){
if(l==S.l)
return r<S.r;
return l<S.l;
}
}e[N];
bool check(int W)
{
Y=0,sum=0;
memset(pre_n,0,sizeof(pre_n));
memset(pre_v,0,sizeof(pre_v));
for(int i=1;i<=n;i++)
{
if(w[i]>=W) pre_n[i]=pre_n[i-1]+1,pre_v[i]=pre_v[i-1]+v[i];
else pre_n[i]=pre_n[i-1],pre_v[i]=pre_v[i-1];
}
for(int i=1;i<=m;i++)
Y+=(pre_n[e[i].r]-pre_n[e[i].l-1])*(pre_v[e[i].r]-pre_v[e[i].l-1]);
sum=llabs(Y-k);
if(Y>k) return true;
else return false;
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
scanf("%lld%lld%lld",&n,&m,&k);
for(int i=1;i<=n;i++)scanf("%d%d",&w[i],&v[i]);
for(int i=1;i<=m;i++)scanf("%d%d",&e[i].l,&e[i].r);
ll l=0;
ll r=N;
ans=INF;
while(l<=r){
ll mid=(l+r)>>1;
if(check(mid))l=mid+1;
else r=mid-1;
if(sum<ans)ans=sum;
}
cout<<ans<<endl;
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}