QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603080#7439. 铃原露露wsc20080 817ms97688kbC++174.1kb2024-10-01 14:36:412024-10-01 14:36:42

Judging History

你现在查看的是最新测评结果

  • [2024-10-01 14:36:42]
  • 评测
  • 测评结果:0
  • 用时:817ms
  • 内存:97688kb
  • [2024-10-01 14:36:41]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void write(ll x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const ll N=2e5+9;
ll n,m,a[N],fa[N];
vector<ll>to[N];
ll tr[N<<2],tag[N<<2],cnt[N<<2],sm[N<<2],tagh[N<<2];
void Build(ll x,ll l,ll r){
    cnt[x]=r-l+1;
    if(l==r)return ;
    ll mid=(l+r)>>1;
    Build(x<<1,l,mid);
    Build(x<<1|1,mid+1,r);
}
void Pushtag(ll x,ll k){
    tr[x]+=k,tag[x]+=k;
}
void Pushsum(ll x,ll k){
    sm[x]+=k*cnt[x],tagh[x]+=k;
}
void Pushup(ll x){
    tr[x]=min(tr[x<<1],tr[x<<1|1]);
    sm[x]=sm[x<<1]+sm[x<<1|1];
    cnt[x]=(tr[x<<1]==tr[x])*cnt[x<<1]+(tr[x<<1|1]==tr[x])*cnt[x<<1|1];
}
void Pushdown(ll x){
    if(tag[x]){
        Pushtag(x<<1,tag[x]);
        Pushtag(x<<1|1,tag[x]);
        tag[x]=0;
    }
    if(tagh[x]){
        if(tr[x<<1]==tr[x])Pushsum(x<<1,tagh[x]);
        if(tr[x<<1|1]==tr[x])Pushsum(x<<1|1,tagh[x]);
        tagh[x]=0;
    }
}
void Upd(ll x,ll l,ll r,ll ql,ll qr,ll k){
    if(ql<=l&&r<=qr){
        Pushtag(x,k);
        return ;
    }
    ll mid=(l+r)>>1;
    Pushdown(x);
    if(ql<=mid)Upd(x<<1,l,mid,ql,qr,k);
    if(qr>mid)Upd(x<<1|1,mid+1,r,ql,qr,k);
    Pushup(x);
}
void Hist(ll x,ll l,ll r,ll ql,ll qr){
    if(ql<=l&&r<=qr){
        Pushsum(x,1);
        return ;
    }
    ll mid=(l+r)>>1;
    Pushdown(x);
    if(ql<=mid)Hist(x<<1,l,mid,ql,qr);
    if(qr>mid)Hist(x<<1|1,mid+1,r,ql,qr);
    Pushup(x);
}
ll Query(ll x,ll l,ll r,ll ql,ll qr){
    if(ql<=l&&r<=qr)return sm[x];
    ll mid=(l+r)>>1;
    Pushdown(x);
    if(qr<=mid)return Query(x<<1,l,mid,ql,qr);
    if(ql>mid)return Query(x<<1|1,mid+1,r,ql,qr);
    return Query(x<<1,l,mid,ql,qr)+Query(x<<1|1,mid+1,r,ql,qr);
}
struct E{
    int l,r,sgn,nxt;
}es[N*40];
int tote,hd[N];
void adde(int x,int l,int r,int sgn){
    if(x>n||x<=0)return ;
    es[++tote]=(E){l,r,sgn,hd[x]};
    hd[x]=tote;
}
void Addq(ll lx,ll rx,ll ly,ll ry){
    adde(lx,ly,ry,1),adde(rx+1,ly,ry,-1);
}
ll sz[N],son[N],dfn[N],tim,ord[N];
void dfs1(ll x){
    sz[x]=1,dfn[x]=++tim,ord[tim]=x;
    for(ll y:to[x]){
        dfs1(y);
        sz[x]+=sz[y];
        if(sz[y]>sz[son[x]])son[x]=y;
    }
}
set<ll>S;
void Addp(ll x,ll sgn){
    if(sgn==1)S.insert(a[x]);
    else S.erase(S.find(a[x]));
}
void Cons(ll x,ll y,ll z){
    if(z<x)Addq(z+1,x,y,n);
    else if(z>y)Addq(1,x,y,z-1);
}
void Add(ll x,ll sgn,ll z){
    if(sgn==1){
        if(!S.empty()){
            rep(i,dfn[x],dfn[x]+sz[x]-1){
                ll u=ord[i];
                auto it=S.lower_bound(a[u]);
                if(it!=S.begin())Cons(*prev(it),a[u],a[z]);
                if(it!=S.end())Cons(a[u],*it,a[z]);
            }
        }
    }
    rep(i,dfn[x],dfn[x]+sz[x]-1)Addp(ord[i],sgn);
}
void dfs2(ll x,bool heavy=0){
    for(ll y:to[x]){
        if(y==son[x])continue;
        dfs2(y);
    }
    if(son[x])dfs2(son[x],1);
    for(ll y:to[x]){
        if(y==son[x])continue;
        Add(y,1,x);
    }
    Addp(x,1);
    if(heavy)return;
    Add(x,0,x);
}
vector<pii>vq[N];
ll ans[N];
bool Med;
int main(){
    cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
    n=read(),m=read();
    rep(i,1,n)a[i]=read();
    rep(i,2,n)fa[i]=read(),to[fa[i]].push_back(i);
    dfs1(1),dfs2(1);
    Build(1,1,n);
    rep(i,1,m){
        ll l=read(),r=read();
        vq[r].push_back({l,i});
    }
    rep(i,1,n){
        for(ll j=hd[i];j;j=es[j].nxt){
            ll l=es[j].l,r=es[j].r,sgn=es[j].sgn;
            Upd(1,1,n,l,r,sgn);
        }
        Hist(1,1,n,1,i);
        for(pii p:vq[i])ans[p.second]=Query(1,1,n,p.first,i);
    }
    rep(i,1,m)write(ans[i]),putchar('\n');
    cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 22364kb

input:

100 100
5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

45
2701
561
10
91
78
946
120
2080
300
861
351
1540
666
210
210
595
2278
465
435
496
496
2556
210
28
1326
496
1596
1
741
3828
10
595
630
10
861
15
105
2556
120
1275
528
2080
21
861
1830
2485
136
10
861
1770
4186
2211
171
21
946
351
6
2415
28
190
435
36
4560
2080
1596
1953
1540
820
105
3
91
15
741
241...

result:

wrong answer 1st numbers differ - expected: '9', found: '45'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 817ms
memory: 97688kb

input:

200000 1
73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...

output:

20000100000

result:

wrong answer 1st numbers differ - expected: '216932', found: '20000100000'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%