正题
评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=P1314
题目大意
有nnn个石头,mmm个区间,对于每个WWW有一个YYY。
Y=∑i=0n((∑j=liri(wi>=w)∗vi)∗(∑j=liri(wi>=w)))Y=\sum_{i=0}^n((\sum_{j=l_i}^{r_i}(w_i>=w)*v_i)*(\sum_{j=l_i}^{r_i}(w_i>=w)))Y=i=0∑n((j=li∑ri(wi>=w)∗vi)∗(j=li∑ri(wi>=w)))
求一个WWW,使得∣S−Y∣|S-Y|∣S−Y∣最小。
解题思路
二分WWW,前缀和处理。
codecodecode
#include<cstdio>
#include<algorithm>
#include<cctype>
#define ll long long
#define N 200001
#define V 107
using namespace std;
ll read() {ll x=0,f=1; char c=getchar();while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();return x*f;
}
void print(ll x){if (x>9) print(x/10); putchar(x%10+48); return;
}
ll n,m,S,num[N],ans[N],l[N],r[N],mins,answer,w[N],v[N];
ll check(ll x){ll sum=0;for(ll i=1;i<=n;i++){ans[i]=ans[i-1]+(w[i]<x?0:v[i]);num[i]=num[i-1]+(w[i]>=x);}//前缀和for(ll i=1;i<=m;i++)sum+=(ans[r[i]]-ans[l[i]-1])*(num[r[i]]-num[l[i]-1]);return sum;
}//判断
int main()
{//printf("%dKb",(sizeof(num)+sizeof(ans)+)/1024);n=read();m=read();S=read();for(ll i=1;i<=n;i++)w[i]=read(),v[i]=read();for(ll i=1;i<=m;i++)l[i]=read(),r[i]=read();ll w;answer=1e18;ll ls=1,rs=1e12;//二分while(ls<=rs){ll mid=(ls+rs)/2,w;if((w=check(mid))<S) rs=mid-1;else ls=mid+1;answer=min(answer,abs(S-w));}print(answer);
}