早知道线段树也会被...

P1314 [NOIP2011 提高组] 聪明的质监员

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 ran_qwq @ 2023-08-13 20:28:23

@kkxacj 用C++98


by kkxacj @ 2023-08-13 20:28:38

@fangzichang 怎么用?放前面失败了


by fangzichang @ 2023-08-13 20:28:41

@woshilaji_ 最近天天看到问IO效率的。
luogu机子上的简单测试


by Elairin176 @ 2023-08-13 20:29:10

@kkxacj 这份代码开 C++98 和 O2 就能过了。


by kkxacj @ 2023-08-13 20:30:03

@ran_qwq 6


by kkxacj @ 2023-08-13 20:30:42

此贴结:C++98 + O2 a了


by fangzichang @ 2023-08-13 20:33:00

@kkxacj 你混用了快读和scanf。你全用快读就行了。


by kkxacj @ 2023-08-13 20:34:21

@fangzichang 混用是因为 s 开了long long


by fangzichang @ 2023-08-13 20:36:05

@kkxacj 呜,只需要知道fread超快其实就行了。以后总会用上的。


by Jerrlee✅ @ 2023-08-13 20:37:02

@kkxacj 你说得对,但是 C++20 O2 能过:https://www.luogu.com.cn/record/120718966


上一页 | 下一页