QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#858244#9676. AncestorsAsiraeM0 492ms204508kbC++143.3kb2025-01-16 15:17:592025-01-16 15:18:04

Judging History

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

  • [2025-01-16 15:18:04]
  • 评测
  • 测评结果:0
  • 用时:492ms
  • 内存:204508kb
  • [2025-01-16 15:17:59]
  • 提交

answer

#include<bits/stdc++.h>
namespace xcy{
typedef int ll;
const int MAXN=100005;
const int MAXM=1000005;
#define emp emplace
#define emb emplace_back
ll tr[MAXN<<9],l[MAXN<<9],r[MAXN<<9],rt[MAXN],head[MAXN],q[MAXM][3],ans[MAXM],num,cnt,trt,n,m,i,j,k;
ll dfn[MAXN],rv[MAXN],top[MAXN],son[MAXN],siz[MAXN],fa[MAXN],dep[MAXN],tim;
std::vector<ll>vq[MAXN];struct Ed{ll pre,to;}ed[MAXN];
struct Nd{mutable ll l,r,v;Nd(ll L,ll R,ll V){l=L,r=R,v=V;}
    bool operator<(const Nd &A)const{return l==A.l?r<A.r:l<A.l;}
};std::set<Nd>s[MAXN<<1];

inline void fread(ll &X){ll A=1;X=0;char C=getchar();while(!isdigit(C)&&C!='-')C=getchar();if(C=='-')A=-1,C=getchar();while(isdigit(C))X=(X<<1)+(X<<3)+(C^48),C=getchar();X*=A;}
inline void fout(ll X,char Ch=' '){if(X<0)putchar('-'),X=-X;if(!X){putchar('0'),putchar(Ch);return;}char C[25]{};int Len=0;while(X)C[++Len]=X%10+'0',X/=10;for(;Len;--Len)putchar(C[Len]);putchar(Ch);}
inline void add(ll F,ll T){ed[++cnt].to=T;ed[cnt].pre=head[F];head[F]=cnt;}
inline void dfs1(ll N)
{
    siz[N]=1;for(ll I=head[N],J;I;I=ed[I].pre)
        J=ed[I].to,dfs1(J),siz[N]+=siz[J],son[N]=J;
}
inline void dfs2(ll N,ll T)
{
    dfn[N]=++tim;top[N]=T;if(son[N])dfs2(son[N],T);
    for(ll I=head[N],J;I;I=ed[I].pre)
        if((J=ed[I].to)!=son[N])dfs2(J,J);
}
inline ll mdf(ll N,ll L,ll R,ll X,ll V)
{
    if(!N)N=++num;if(L==R){tr[N]+=V;return N;}ll M=L+R>>1;
    if(X<=M)l[N]=mdf(l[N],L,M,X,V);else r[N]=mdf(r[N],M+1,R,X,V);
    tr[N]=tr[l[N]]+tr[r[N]];return N;
}
inline ll qry(ll N,ll L,ll R,ll X)//suffix
{
    if(!N)return 0;
    if(L==X)return tr[N];ll M=L+R>>1;
    if(X<=M)return qry(l[N],L,M,X)+tr[r[N]];
    return qry(r[N],M+1,R,X);
}
inline void tmdf(ll L,ll R,ll X,ll V){if(!X||L>R)return;
    for(;L<=n;L+=L&-L)rt[L]=mdf(rt[L],1,n,X,V);
    for(++R;R<=n;R+=R&-R)rt[R]=mdf(rt[R],1,n,X,-V);}
inline ll tqry(ll I,ll X)
    {ll Res=0;for(;I;I-=I&-I)Res+=qry(rt[I],1,n,X);return Res;}
inline void sins(ll N,ll L,ll R,ll V){if(L<=R)s[N].emp(L,R,V);}
inline void work(ll N,ll L,ll R,ll V)
{
    auto Li=--s[N].lower_bound({L+1,0,0}),
         Ri=--s[N].lower_bound({R+1,0,0}),Nw=Li,Ls=Li;
    assert(Li->l<=L&&R<=Ri->r);
    ll Ll=Li->l,Rr=Li->r,Vv=Li->v;
    if(Li==Ri){assert(Ll<=L&&R<=Rr);s[N].erase(Li),
        sins(N,Ll,L-1,Vv),sins(N,L,R,V),sins(N,R+1,Rr,Vv),
        tmdf(L,R,Vv,-1),tmdf(L,R,V,1);return;}
    for(++Nw;Nw!=Ri;)tmdf(Nw->l,Nw->r,Nw->v,-1),
        Ls=Nw,++Nw,s[N].erase(Ls);
    s[N].erase(Li),sins(N,Ll,L-1,Vv),tmdf(L,Rr,Vv,-1);
    Ll=Ri->l,Rr=Ri->r,Vv=Ri->v;
    s[N].erase(Ri),sins(N,R+1,Rr,Vv),tmdf(Ll,R,Vv,-1);
    sins(N,L,R,V),tmdf(L,R,V,1);
}
inline void ins(ll N)
{
    ll L,R,X=0,Y=0,V=N;for(N=fa[N];N;N=fa[top[N]])
        assert(top[N]),L=dfn[top[N]],R=dfn[N],Y=X+1,X+=R-L+1,work(X-L+MAXN,Y,X,V);
}

int mian()
{
    fread(n);fread(m);for(i=1;i<=n;++i)
        fread(fa[i]),add(fa[i],i),(fa[i]?0:trt=i);
    for(i=1;i<=m;++i)fread(q[i][0]),fread(q[i][1]),
        fread(q[i][2]),vq[q[i][1]].emb(i);
    for(i=0;i<2*MAXN;++i)s[i].emp(1,n,0);
    dfs1(trt);dfs2(trt,trt);for(i=1;i<=n;++i)
        {ins(i);for(auto I:vq[i])ans[I]=tqry(q[I][2],q[I][0]);}
    for(i=0;i<2*MAXN;++i){j=0;for(auto I:s[i])assert(I.l==j+1&&I.l<=I.r),j=I.r;assert(j==n);}
    for(i=1;i<=m;++i)fout(ans[i],'\n');return 0;
}}
int main(){return xcy::mian();}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 8ms
memory: 35088kb

input:

7 5
3 1 0 5 3 5 1
1 3 1
5 7 2
1 5 1
4 7 1
4 7 2

output:

2
1
3
3
1

result:

ok 5 number(s): "2 1 3 3 1"

Test #2:

score: 11
Accepted
time: 11ms
memory: 35876kb

input:

1000 1000
686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...

output:

452
67
611
126
329
486
354
25
559
585
184
265
576
116
489
289
147
287
13
282
151
192
146
141
148
131
43
72
69
29
5
15
57
10
9
16
87
162
19
217
232
24
178
334
103
139
293
400
299
351
529
632
592
296
640
678
715
708
52
465
322
731
2
69
110
286
172
0
40
31
16
105
75
45
8
94
540
115
357
7
11
431
581
37
...

result:

ok 1000 numbers

Test #3:

score: 0
Wrong Answer
time: 71ms
memory: 43788kb

input:

1000 1000
594 766 788 546 408 364 152 525 963 359 339 746 747 58 628 60 144 646 222 863 418 1000 494 84 225 21 202 146 81 879 239 526 662 816 483 140 7 834 978 26 370 619 12 112 824 319 855 852 877 32 708 236 296 791 40 102 238 930 734 609 740 309 982 837 272 451 825 977 717 597 761 90 305 224 216 8...

output:

356
297
245
428
167
313
297
26
351
27
403
8
264
210
206
34
89
195
47
131
163
85
100
79
51
102
60
26
59
31
7
25
53
25
57
17
45
41
60
220
76
85
192
309
143
42
334
217
286
117
368
81
5
437
177
113
210
218
116
377
96
236
58
397
200
285
143
0
108
205
296
152
76
295
152
94
63
350
45
121
252
255
249
126
15...

result:

wrong answer 1st numbers differ - expected: '441', found: '356'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #30:

score: 17
Accepted
time: 329ms
memory: 107472kb

input:

50000 200000
42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...

output:

12045
1321
12292
2444
32443
36534
38575
30505
16464
16648
47153
41144
23401
1762
18567
6831
26486
29716
8905
16295
38569
29990
22061
7638
3820
34754
17582
6466
12632
23384
11657
16493
24434
11503
3945
2205
37806
13036
29052
24114
39158
34702
37326
20815
11766
41253
44355
17807
23452
26717
43107
4133...

result:

ok 200000 numbers

Test #31:

score: 0
Time Limit Exceeded

input:

50000 200000
13611 7267 47234 19765 39458 30493 19935 34465 46369 11995 35207 5752 13718 47418 12913 13725 27694 47726 25253 30406 27587 25790 21091 12008 13534 8804 26484 34270 33810 49102 25288 37607 11863 23340 22747 40502 4461 24803 31317 25617 46077 6504 46299 34533 6829 17841 2812 35413 16394 ...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Time Limit Exceeded

Test #67:

score: 17
Accepted
time: 492ms
memory: 204508kb

input:

100000 1000000
6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...

output:

52956
18767
1319
13405
11021
455
50595
81481
6813
3640
58937
10991
70
4713
36
9517
39731
1166
67346
74637
2667
45182
4914
6774
1625
4198
52270
30435
60137
48654
29768
2815
6846
73091
21944
49693
9923
46795
29787
6866
2435
20773
2959
34666
4377
2428
4582
7527
38292
7253
3586
63817
28075
43828
20215
1...

result:

ok 1000000 numbers

Test #68:

score: 0
Time Limit Exceeded

input:

100000 1000000
82160 95864 48267 17482 19568 35077 14202 20440 4649 64145 148 2227 6969 39096 36508 20991 67700 90300 69215 57284 18492 9246 9629 7538 7845 30368 55600 48445 18542 41242 45052 25380 20894 91677 77040 73134 15572 21966 25343 14501 16227 23870 39207 50024 30787 11148 16884 63700 33205 ...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%