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

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 LeNotFound @ 2023-08-13 20:19:45

@kkxacj 试试改成cincout然后关流同步能不能冲过去,cpp20 O2


by 黄海辰 @ 2023-08-13 20:21:27

开O2


by Celestial_Scarlet @ 2023-08-13 20:21:44

@LeNotFound

getchar 快读不比 cin 快?


by fangzichang @ 2023-08-13 20:25:19

read前面加上这两句。fread,启动!

char buf[1<<20],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)

by fangzichang @ 2023-08-13 20:25:30

@kkxacj


by so_find_skind @ 2023-08-13 20:25:53

直接c++20fread快读秒全场


by so_find_skind @ 2023-08-13 20:26:09

@fangzichang

哎呀你抢我话


by LoserKugua @ 2023-08-13 20:26:58

@XCEsupremacy 朴素getchar快读还真比关闭流通步的cin慢


by Celestial_Scarlet @ 2023-08-13 20:27:24

@zhezhikongdanruxue 请教一下,为什么要用 C++20 呢,一般而言会比 C++98 和 C++14 (GCC 9) 快吗?


by Celestial_Scarlet @ 2023-08-13 20:28:11

@woshilaji_ 这么牛 /fad 自己以前没做过测试,属实没想到,感谢科普


| 下一页