QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#860127#9676. AncestorsJ_R_X0 757ms97516kbC++143.9kb2025-01-18 10:39:472025-01-18 10:39:47

Judging History

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

  • [2025-01-18 10:39:47]
  • 评测
  • 测评结果:0
  • 用时:757ms
  • 内存:97516kb
  • [2025-01-18 10:39:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int maxm=1e6+5, maxn=1e5+5, B=400;

int n;
int fa[20][maxn], dep[maxn], rk[maxn], lg[maxn], bl[maxn], ed[maxn];
vector<int> g[maxn], D[maxn], wn[maxn][20];

int lca(int u,int v) {
    if(dep[u]<dep[v]) swap(u,v);
    for(int i=19;i>=0;i--) if(dep[fa[i][u]]>=dep[v]) u=fa[i][u];
    if(u==v) return u;
    for(int i=19;i>=0;i--) if(fa[i][u]!=fa[i][v]) u=fa[i][u], v=fa[i][v];
    return fa[0][u];
}

void dfs(int x) {
    dep[x]=dep[fa[0][x]]+1;
    D[dep[x]].push_back(x);
    rk[x]=D[dep[x]].size()-1;
    for(int y:g[x]) dfs(y);
    return ;
}

int ask(int id,int l,int r) {
    int k=lg[r-l];
    return max(wn[id][k][l],wn[id][k][r-(1<<k)]);
}

struct node {
    int l;
    int r;
    int x;
    int id;
}e[maxm];

int ans[maxm];

bool cmp(node x,node y) {
    if(bl[x.l]!=bl[y.l]) return bl[x.l]<bl[y.l];
    return x.r<y.r;
}

int vis[maxn], pre[maxn], nxt[maxn];

struct Block {
    int s[maxn], s2[maxn];
    void add(int x,int v) {
        s[x]+= v, s2[bl[x]]+= v;
        return ;
    }
    int ask(int x) {
        int res=0;
        for(int i=1;i<bl[x];i++) res+= s2[i];
        for(int i=ed[bl[x]-1]+1;i<=x;i++) res+= s[i];
        return res;
    }
    void init() {
        for(int i=1;i<=n;i++) s[i]=s2[i]=0;
        return ;
    }
}T;

void add(int x,int o) {
    int v=0;
    if(!pre[x]&&!nxt[x]) v=dep[x];
    else if(!pre[x]) v=dep[x]-ask(dep[x],rk[x],rk[nxt[x]]);
    else if(!nxt[x]) v=dep[x]-ask(dep[x],rk[pre[x]],rk[x]);
    else v=dep[x]-max(ask(dep[x],rk[x],rk[nxt[x]]),ask(dep[x],rk[pre[x]],rk[x]));
    T.add(1,o), T.add(v,-o);
    return ;
}

struct node2 {
    int l;
    int m;
    int r;
};

vector<int> vec;

void del(int x,int t) {
    add(x,-1);
    if(!pre[x]&&!nxt[x]) ;
    else if(!pre[x]) {
        if(t) vec.push_back(x);
        pre[nxt[x]]=0;
    } else if(!nxt[x]) {
        if(t) vec.push_back(x);
        nxt[pre[x]]=0;
    } else {
        if(t) vec.push_back(x);
        nxt[pre[x]]=nxt[x];
        pre[nxt[x]]=pre[x];
    }
    return ;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int m; cin>>n>>m;
    int rt=0;
    for(int i=1;i<=n;i++) {
        cin>>fa[0][i];
        if(!fa[0][i]) rt=i;
        else g[fa[0][i]].push_back(i);
    }
    for(int i=1;i<20;i++) {
        for(int j=1;j<=n;j++) {
            fa[i][j]=fa[i-1][fa[i-1][j]];
        }
    }
    dfs(rt);
    for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
    for(int i=1;i<=n;i++) {
        if(!D[i].size()) continue;
        int sz=D[i].size()-1;
        wn[i][0].resize(sz);
        for(int j=0;j<sz;j++) wn[i][0][j]=dep[lca(D[i][j],D[i][j+1])];
        for(int j=1;(1<<j)<=sz;j++) {
            wn[i][j].resize(sz);
            for(int k=0;k+(1<<j)<=sz;k++) wn[i][j][k]=min(wn[i][j-1][k],wn[i][j-1][k+(1<<j-1)]);
        }
    }
    for(int i=1;i<=n;i++) bl[i]=(i-1)/B+1, ed[bl[i]]=i;
    for(int i=1;i<=m;i++) {
        cin>>e[i].l>>e[i].r>>e[i].x;
        e[i].id=i;
    }
    sort(e+1,e+m+1,cmp);
    int j=1;
    for(int i=1;i<=bl[n];i++) {
        int R=j;
        while(bl[e[R].l]==i) ++R;
        int l=ed[i-1]+1, r=n;
        for(int k=l;k<=r;k++) vis[k]=1;
        for(int k=1;k<=n;k++) {
            int lst=0;
            for(auto x:D[k]) if(vis[x]) pre[x]=lst, nxt[lst]=x, lst=x, add(x,1);
        }
        for(int k=R-1;k>=j;k--) {
            while(r>e[k].r) del(r--,0);
            while(l<e[k].l) del(l++,1);
            ans[e[k].id]=T.ask(e[k].x);
            while(vec.size()) {
                int x=vec.back(); vec.pop_back();
                nxt[pre[x]]=pre[nxt[x]]=x;
                add(x,1);
            }
            l=ed[i-1]+1;
        }
        j=R;
        for(int k=1;k<=n;k++) pre[k]=nxt[k]=vis[k]=0;
        T.init();
    }
    for(int i=1;i<=m;i++) cout<<ans[i]<<'\n';
    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 4ms
memory: 69348kb

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: 8ms
memory: 69344kb

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:

274
35
65
-11
-107
-430
-715
-61
-750
-415
42
265
-1566
-430
-1961
-38
147
186
-2
-205
-292
-619
-738
-1000
-1504
-348
-117
-234
-272
-109
5
-51202
-98047
-11744
-7828
-10953
-48371
-74837
-7513
-76032
-71184
-6440
-43190
-69342
-17976
-21991
-41140
-47638
-32385
-33211
-42281
-43514
-35280
-15593
-...

result:

wrong answer 1st numbers differ - expected: '452', found: '274'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 373ms
memory: 78196kb

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:

11967
1308
12292
2430
32022
36233
38575
30209
16362
16648
46802
41144
23339
1745
18385
6831
26396
29415
8794
16175
38569
29730
22061
7622
3798
34375
17582
6437
12490
23083
11657
16401
24199
11437
3945
2183
37350
12946
29052
24114
39121
34550
36959
20673
11645
41253
44355
17805
23391
26494
42276
4069...

result:

wrong answer 1st numbers differ - expected: '12045', found: '11967'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 17
Accepted
time: 658ms
memory: 97516kb

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: 757ms
memory: 95216kb

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: 739ms
memory: 93428kb

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:

8417
8694
11361
18174
35962
18154
12865
15343
20557
3347
1471
13539
3932
8790
18648
6347
6750
15815
15254
27157
8688
2879
18
9789
18525
3458
17955
3648
11622
6554
6912
26184
14239
5131
9324
11512
12379
2347
2070
15793
33946
14861
30678
5093
21318
4920
2406
14339
13174
5540
8462
20728
32471
22062
151...

result:

wrong answer 25th numbers differ - expected: '18477', found: '18525'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%