kkxacj @ 2023-08-13 20:16:59
rt,t一个点,1.02秒
#include<bits/stdc++.h>
using namespace std;
int n,m,b[200010],c[200010],l,r,mid1,ql[200010],qr[200010];
long long s,sum,ans,ans1,miz;
struct w
{
int l,r;
long long da,da1;
}a[800010];
void build(int p,int l,int r)
{
a[p].l = l; a[p].r = r;
if(l == r)
{
a[p].da = 0;
a[p].da1 = 0;
if(b[l] >= mid1)
{
a[p].da = 1;
a[p].da1 = c[l];
}
return;
}
int mid = (l + r) / 2;
build(p * 2,l,mid); build(p * 2 + 1,mid + 1,r);
a[p].da = a[p * 2].da + a[p * 2 + 1].da;
a[p].da1 = a[p * 2].da1 + a[p * 2 + 1].da1;
}
void ask(int p,int l,int r)
{
if(l <= a[p].l && a[p].r <= r)
{
ans += a[p].da;
ans1 += a[p].da1;
return;
}
int mid = (a[p].l + a[p].r) / 2;
if(l <= mid) ask(p * 2,l,r);
if(mid < r) ask(p * 2 + 1,l,r);
}
inline int read(){
int f=1,x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while('0'<=s&&s<='9'){x=(x<<1)+(x<<3)+s-48;s=getchar();}
return x*f;
}
int main()
{
n = read(); m = read(); scanf("%lld",&s),miz = s;
for(int i = 1;i <= n;++i) b[i] = read(),c[i] = read();
for(int i = 1;i <= m;++i) ql[i] = read(),qr[i] = read();
l = 1,r = 1e6;
while(l <= r)
{
mid1 = l + (r - l) / 2;
build(1,1,n);
sum = 0;
for(int i = 1;i <= m;++i)
{
ans = ans1 = 0;
ask(1,ql[i],qr[i]);
sum += ans * ans1;
}
if(abs(sum - s) < miz) miz = abs(sum - s);
if(sum > s) l = mid1 + 1;
else r = mid1 - 1;
}
printf("%lld",miz);
return 0;
}
by kkxacj @ 2023-08-14 11:20:31
说句闲话:还是好好打正解吧,最慢
#include<bits/stdc++.h>
using namespace std;
int n,m,b[200010],c[200010],l,r,mid1,ql[200010],qr[200010];
long long s,sum,miz,aq[200010],aq1[200010];
int main()
{
scanf("%d%d%lld",&n,&m,&s),miz = s;
for(int i = 1;i <= n;++i) scanf("%d%d",&b[i],&c[i]);
for(int i = 1;i <= m;++i) scanf("%d%d",&ql[i],&qr[i]);
l = 1,r = 1e6;
while(l <= r)
{
mid1 = l + (r - l) / 2;
sum = 0;
for(int i = 1;i <= n;++i) aq[i] = aq[i - 1] + (b[i] > mid1),aq1[i] = aq1[i - 1] + (b[i] > mid1) * c[i];
for(int i = 1;i <= m;++i) sum += (aq[qr[i]] - aq[ql[i] - 1]) * (aq1[qr[i]] - aq1[ql[i] - 1]);
if(abs(sum - s) < miz) miz = abs(sum - s);
if(sum > s) l = mid1 + 1;
else r = mid1 - 1;
}
printf("%lld",miz);
return 0;
}