twocats @ 2022-07-20 20:06:07
#include<bits/stdc++.h>
#define int long long
#define N 200005
#define mid ((l+r)>>1)
#define ls (u<<1)
#define rs (u<<1|1)
#define lson ls,l,mid
#define rson rs,mid+1,r
using namespace std;
struct Node
{
int num;
int sum;
}T[N<<2];
int n,m,s,d=0xFFFFFFFFFFFFFFF,L,R=N>>1,w[N],v[N],l[N],r[N];
inline void push_up(int u)
{
T[u].num=T[ls].num+T[rs].num;
T[u].sum=T[ls].sum+T[rs].sum;
}
void build(int u,int l,int r,int W)
{
if(l==r)
{
T[u].num=w[l]>=W?1:0;
T[u].sum=T[u].num*v[l];
return;
}
build(lson,W);
build(rson,W);
push_up(u);
}
int query_num(int u,int l,int r,int x,int y)
{
if(x<=l&&r<=y) return T[u].num;
int res=0;
if(x<=mid) res+=query_num(lson,x,y);
if(y>mid) res+=query_num(rson,x,y);
return res;
}
int query_sum(int u,int l,int r,int x,int y)
{
if(x<=l&&r<=y) return T[u].sum;
int res=0;
if(x<=mid) res+=query_sum(lson,x,y);
if(y>mid) res+=query_sum(rson,x,y);
return res;
}
int y(int W)
{
int res=0;
build(1,1,n,W);
for(int i=1;i<=m;i++)
res+=query_num(1,1,n,l[i],r[i])*query_sum(1,1,n,l[i],r[i]);
return res;
}
signed main()
{
scanf("%lld%lld%lld",&n,&m,&s);
for(int i=1;i<=n;i++) scanf("%lld%lld",&w[i],&v[i]);
for(int i=1;i<=m;i++) scanf("%lld%lld",&l[i],&r[i]);
while(L<=R)
{
int M=(L+R)>>1,sumy=y(M);
if(d>abs(s-sumy)) d=abs(s-sumy);
if(sumy<s) R=M-1;
if(sumy>s) L=M+1;
if(sumy==s) break;
}
printf("%lld",d);
return 0;
}
by 0x3b800001 @ 2022-07-20 20:19:31
建议线段树->前缀和