QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#857843#9676. Ancestorsgyydp123_LIM0 773ms53020kbC++203.9kb2025-01-16 06:46:122025-01-16 06:46:13

Judging History

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

  • [2025-01-16 06:46:13]
  • 评测
  • 测评结果:0
  • 用时:773ms
  • 内存:53020kb
  • [2025-01-16 06:46:12]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);++i)
#define ForDown(i,j,k) for(int i=(j);i>=(k);--i)
#define Debug(fmt, args...) fprintf(stderr,"(func %s, line #%d): " fmt,__func__,__LINE__,##args),fflush(stderr)
#define debug(fmt, args...) fprintf(stderr,fmt,##args),fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N=1e5+5,M=1e6+5;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read(){
  char ch=getchar();int x=0,f=1;
  while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9')
    x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
  return x*f;
}
int n,m,rt,f[N],siz[N],dep[N],dfn[N],idfn[N],cdfn;
vector<int>G[N],vec[N];
void dfs(int u,int fa){
  dep[u]=dep[fa]+1;idfn[dfn[u]=++cdfn]=u;siz[u]=1;
  for(int v within G[u]) dfs(v,u),siz[u]+=siz[v];
}
struct Query{int x,y,z,op,id;}Mdf[N<<5];int tot;
int ans[M];
struct LCT{
  struct Node{int s[2],fa,vl,tag;}T[N];
  void PutTag(int u,int c){if(u) T[u].tag=T[u].vl=c;}
  void pushdown(int u){if(T[u].tag!=-1) PutTag(T[u].s[0],T[u].tag),PutTag(T[u].s[1],T[u].tag),T[u].tag=-1;}
  #define isroot(x) (T[T[x].fa].s[0]!=x&&T[T[x].fa].s[1]!=x)
  #define get(x) (T[T[x].fa].s[1]==x)
  void Update(int x){if(!isroot(x)) Update(T[x].fa);pushdown(x);}
  void rotate(int x){
    int y=T[x].fa,z=T[y].fa,k=get(x),w=T[x].s[!k];
    if(!isroot(y)) T[z].s[get(y)]=x;
    T[x].s[!k]=y;T[y].s[k]=w;
    T[x].fa=z;T[y].fa=x;if(w) T[w].fa=y;
  }void splay(int x){
    Update(x);for(int f;f=T[x].fa,!isroot(x);rotate(x))
      if(!isroot(f)) rotate(get(f)==get(x)?f:x);
  }void access(int x){
    // Debug("%d\n",x);
    int u=x,p;static vector<pair<int,int> >tmp;tmp.clear();
    for(p=0;x;x=T[p=x].fa){
      splay(x);T[x].s[1]=p;
      if(T[x].vl) tmp.emplace_back(dep[x],T[x].vl);
    }splay(u);PutTag(u,u);reverse(tmp.begin(),tmp.end());x=u;
    // for(auto [p,t] within tmp) Debug("%d %d\n",p,t);
    // For(i,1,n) debug("%d ",T[i].fa);debug("\n");
    // For(i,1,n) debug("%d ",T[i].s[0]);debug("\n");
    // For(i,1,n) debug("%d ",T[i].s[1]);debug("\n");
    // For(i,1,n) debug("%d ",T[i].fa);debug("\n");
    Mdf[++tot]={x,x,1,0,1};Mdf[++tot]={x,x,dep[x],0,-1};
    int lp=x,lt=dep[x];
    for(auto [p,t] within tmp){
      Mdf[++tot]={t,x,lt,0,1};
      Mdf[++tot]={t,x,dep[x]-p,0,-1};
      lp=dep[x]-p;lt=t;
    }
  }void erase(int x){
    // Debug("%d\n",x);
    int p,u=x;for(p=0;x;x=T[p=x].fa) splay(x),T[x].s[1]=p;
    splay(u);PutTag(u,0);
  }
}tr;
struct BIT{
  int c[N];
  void update(int x,int v){while(x<=n) c[x]+=v,x+=(x&-x);}
  int query(int x){int s=0;while(x) s+=c[x],x^=(x&-x);return s;}
}bit;
void cdq(int l,int r){
  if(l==r) return;
  int mid=(l+r)>>1;
  cdq(l,mid);cdq(mid+1,r);
  sort(Mdf+l,Mdf+mid+1,[](Query a,Query b){return a.y<b.y;});
  sort(Mdf+mid+1,Mdf+r,[](Query a,Query b){return a.y<b.y;});
  int j=l;for(int i=mid+1;i<=r;i++) if(Mdf[i].op){
    while(j<=mid&&Mdf[j].y<=Mdf[i].y){
      if(!Mdf[j].op) bit.update(Mdf[j].z,Mdf[j].id);j++;
    }ans[Mdf[i].id]+=bit.query(Mdf[i].z);
  }For(i,l,j-1) if(!Mdf[i].op) bit.update(Mdf[i].z,-Mdf[i].id);
}
signed LJY(){
  n=read();m=read();
  For(i,1,n){
    G[f[i]=read()].emplace_back(i),tr.T[i].fa=f[i];
    if(!f[i]) rt=i;
  }dfs(rt,0);
  For(i,1,n) vec[dep[i]].emplace_back(i);
  For(i,1,n) if(vec[i].size()){
    // for(int x within vec[i]) debug("%d ",x);debug("\n");
    for(int x within vec[i]) tr.access(x);
    for(int x within vec[i]) tr.erase(x);
  }
  For(i,1,m){
    int l=read(),r=read(),x=read();
    Mdf[++tot]={l,r,x,1,i};
  }sort(Mdf+1,Mdf+1+tot,[&](Query x,Query y){return x.x!=y.x?x.x>y.x:(x.y!=y.y?x.y<y.y:x.z!=y.z?x.z<y.z:x.op<y.op);});
  // For(i,1,tot) debug("%d %d %d %d %d\n",Mdf[i].x,Mdf[i].y,Mdf[i].z,Mdf[i].op,Mdf[i].id);
  cdq(1,tot);For(i,1,m) printf("%d\n",ans[i]);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 0ms
memory: 6004kb

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: 0
Wrong Answer
time: 2ms
memory: 6276kb

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
738
2
69
110
286
172
0
40
31
16
105
75
45
8
94
540
115
357
7
11
431
581
37
...

result:

wrong answer 62nd numbers differ - expected: '731', found: '738'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 247ms
memory: 22532kb

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:

wrong answer 894th numbers differ - expected: '33056', found: '33310'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 17
Accepted
time: 642ms
memory: 53020kb

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: 17
Accepted
time: 720ms
memory: 50752kb

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:

44469
16565
2546
424
43423
23219
20745
3045
35596
11249
3763
322
27634
52470
20391
28546
14538
26343
9739
15234
20199
4332
1072
43633
3512
1432
47918
11347
4284
9054
6824
9773
39448
19949
18592
16534
43419
3303
11308
51071
1613
30619
8351
15408
6155
28590
13050
10085
2851
15353
33854
11870
5231
3742...

result:

ok 1000000 numbers

Test #69:

score: 0
Wrong Answer
time: 773ms
memory: 49216kb

input:

100000 1000000
25194 62545 57529 6618 90347 4663 56805 81144 16504 79307 43199 91078 67805 33396 46754 26525 75274 39333 20904 40286 76299 83579 79770 37838 8666 33691 40228 71445 16473 53091 68958 32037 72079 38640 89820 67042 68888 34997 65595 87669 425 95432 78811 30382 34738 51750 96653 87265 96...

output:

8121
6601
11260
13919
33045
13988
12704
13603
20103
3347
1471
12843
3932
3223
17701
6347
6632
15706
15192
26116
7617
1895
18
9512
18377
3439
11304
3647
11599
6554
6912
25151
12563
5131
3887
11264
11147
2106
1395
15601
28656
12176
24127
5093
14429
4161
2406
14267
11793
5540
6269
18490
29383
21216
151...

result:

wrong answer 1st numbers differ - expected: '8417', found: '8121'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%