QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#857863#9676. Ancestorsfzx0 2ms8092kbC++202.2kb2025-01-16 08:19:202025-01-16 08:19:21

Judging History

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

  • [2025-01-16 08:19:21]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:8092kb
  • [2025-01-16 08:19:20]
  • 提交

answer

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int INF=2e5+5;
int n,m,sz[INF],son1[INF],fa[INF],dep[INF];
int top1[INF],dfn[INF],pre[INF],A[INF];
vector <int> e[INF];
void DFS(int x) {
    sz[x]=1;
    for (int v:e[x]) {
        dep[v]=dep[x]+1;
        DFS(v);
        sz[x]+=sz[v];
        if (sz[v]>sz[son1[x]]) 
            son1[x]=v;
    }
}
void DFS1(int x,int t) {
    top1[x]=t;dfn[++dfn[0]]=x;pre[x]=dfn[0];
    if (!son1[x]) return ;
    DFS1(son1[x],t);
    for (int v:e[x]) {
        if (v==son1[x]) continue;
        DFS1(v,v);
    }
}
struct P3{
    int l,r,id;
};
vector <P3> e2[INF];
vector < pii > ch[INF];
int num[INF],res[INF];

struct BIT{
    int tree[INF];
    void add(int x,int y) {for (int i=x;i<=1e5;i+=i&-i) tree[i]+=y;}
    int query(int x) {int sum=0;for (int i=x;i;i-=i&-i) sum+=tree[i];return sum;}
}T1;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;int rt=0;
    for (int i=1;i<=n;i++) {
        int x=0;cin>>x;
        fa[i]=x;if (x) e[x].pb(i);else rt=i;
    }
    DFS(rt);
    DFS1(rt,rt);
    for (int i=1;i<=n;i++) A[i]=pre[i];
    for (int i=1;i<=m;i++) {
        int l=0,r=0,k=0;
        cin>>l>>r>>k;
        e2[k].pb({l,r,i});
    }
    for (int x=1;x<=n;x++) {
        int xx=x,d=0;
        while (dep[top1[xx]]) {
            ch[dep[xx]-dep[fa[top1[xx]]]+d].pb({x,pre[fa[top1[xx]]]});
            d+=dep[xx]-dep[fa[top1[xx]]];
            xx=fa[top1[xx]];
        }
        ch[dep[xx]+1+d].pb({x,-1});
    }
    for (int i=1;i<=n;i++) {
        for (pii it:ch[i]) 
            if (it.se==-1) T1.add(it.fi,-1);
            else A[it.fi]=it.se+i;
        for (P3 it:e2[i]) {
            res[it.id]+=it.r-it.l+1+(T1.query(it.r)-T1.query(it.l-1));
            for (int u=it.l;u<=it.r;u++) {
                if (A[u]<=-1) continue;
                if (num[A[u]]) res[it.id]--;
                num[A[u]]=1;
            }
            for (int u=it.l;u<=it.r;u++) {
                if (A[u]<=-1) continue;
                num[A[u]]=0;
            }
        }
    }

    for (int i=1;i<=m;i++) 
        cout<<res[i]<<"\n";
    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: 2ms
memory: 5860kb

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: 1ms
memory: 8092kb

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: 2ms
memory: 5992kb

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:

344
245
163
504
38
307
289
-148
392
-119
461
-119
269
197
194
-33
46
203
12
121
162
69
91
67
44
101
57
23
61
32
7
25
54
25
57
10
41
30
54
236
59
67
192
337
131
-24
368
213
305
50
419
-30
-163
501
93
-39
141
139
-70
392
-136
117
39
467
204
298
144
0
-15
215
309
159
76
322
147
98
-23
398
46
130
280
27...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #30:

score: 0
Time Limit Exceeded

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:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Runtime Error

Test #67:

score: 0
Runtime Error

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:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%