QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#857725#9676. Ancestorscancan1234560 1965ms61296kbC++145.4kb2025-01-16 00:51:442025-01-16 00:51:45

Judging History

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

  • [2025-01-16 00:51:45]
  • 评测
  • 测评结果:0
  • 用时:1965ms
  • 内存:61296kb
  • [2025-01-16 00:51:44]
  • 提交

answer

#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005, M = 1000005, B_MODUI = 100;
vector < int > son[N];
int fa[N], hson[N], sz[N], dep[N], dfn[N], dfncnt, invdfn[N], st[2 * N][18], stcnt, _log2[2 * N], start[N];
void dfs(int u) {
    dfncnt++;
    dfn[u] = dfncnt;
    invdfn[dfncnt] = u;
    dep[u] = dep[fa[u]] + 1;
    sz[u] = 1;
    stcnt++;
    st[stcnt][0] = dep[u];
    start[u] = stcnt;
    for (int v : son[u]) {
        dfs(v);
        sz[u] += sz[v];
        if (sz[v] > sz[hson[u]]) {
            hson[u] = v;
        }
        stcnt++;
        st[stcnt][0] = dep[u];
    }
}
int dep_LCA(int u, int v) {
    u = start[u];
    v = start[v];
    if (u > v) {
        u ^= v ^= u ^= v;
    }
    int q = _log2[v - u + 1];
    return min(st[u][q], st[v - (1 << q) + 1][q]);
}
struct Query {
    int l, r, x, id;
    Query(int l_, int r_, int x_, int id_) {
        l = l_;
        r = r_;
        x = x_;
        id = id_;
    }
};
bool operator < (const Query & a, const Query & b) {
    return a.r > b.r;
}
bool recording;
int big[350], small[N];
struct Operation1 {
    int x, w;
    Operation1(int x_, int w_) {
        x = x_;
        w = w_;
    }
    void undo();
};
stack < Operation1 > stk1;
void add(int x, int w) {
    if (recording) {
        stk1.push(Operation1(x, w));
    }
    big[x / 350] += w;
    small[x] += w;
}
void Operation1::undo() {
    add(x, -w);
}
void add(int l, int r, int w) {
    add(l, w);
    add(r + 1, -w);
}
int query(int x) {
    int ans = 0;
    for (int i = 0; i < x / 350; i++) {
        ans += big[i];
    }
    for (int i = x / 350 * 350; i <= x; i++) {
        ans += small[i];
    }
    return ans;
}
vector < Query > que[N / B_MODUI + 5];
int ans[M];
vector < int > layer[N];
struct Node {
    int prv, nxt;
} node[N];
struct Operation2 {
    int a, b, c;
    Operation2(int x, int y, int z) {
        a = x;
        b = y;
        c = z;
    }
    void undo() {
        node[a].nxt = b;
        node[c].prv = b;
    }
};
stack < Operation2 > stk2;
void remove(int u) {
    if (recording) {
        stk2.push(Operation2(node[u].prv, u, node[u].nxt));
    }
    node[node[u].prv].nxt = node[u].nxt;
    node[node[u].nxt].prv = node[u].prv;
}
void link(int u, int v, int w) {
    if (u == 0 || v == 0) {
        return;
    }
    // printf("link(%d, %d, %d)\n", u, v, w);
    // printf("add(%d, %d)\n", dep[u] - dep_LCA(u, v), w);
    add(dep[u] - dep_LCA(u, v), w);
}
void del(int v) {
    // printf("-- del %d\n", v);
    // printf("add(%d, %d, %d)\n", 1, dep[v] - 1, -1);
    add(1, dep[v] - 1, -1);
    v = dfn[v];
    remove(v);
    int u = invdfn[node[v].prv];
    int w = invdfn[node[v].nxt];
    v = invdfn[v];
    link(u, v, +1);
    link(v, w, +1);
    link(u, w, -1);
}
void build(const vector < int > & vec) {
    if (vec.empty()) {
        return;
    }
    for (int i = 1; i < (int)vec.size(); i++) {
        link(invdfn[vec[i - 1]], invdfn[vec[i]], -1);
        node[vec[i]].prv = vec[i - 1];
        node[vec[i - 1]].nxt = vec[i];
    }
    node[vec.front()].prv = node[vec.back()].nxt = 0;
}
void undo_all() {
    while (!stk1.empty()) {
        stk1.top().undo();
        stk1.pop();
    }
    while (!stk2.empty()) {
        stk2.top().undo();
        stk2.pop();
    }
}
int main() {
    // freopen("in.txt", "r", stdin);
    int n, m;
    scanf("%d %d", &n, &m);
    int root = 0;
    for (int u = 1; u <= n; u++) {
        scanf("%d", &fa[u]);
        if (fa[u] == 0) {
            root = u;
        } else {
            son[fa[u]].push_back(u);
        }
    }
    dfs(root);
    for (int i = 2; i <= stcnt; i++) {
        _log2[i] = _log2[i / 2] + 1;
    }
    for (int j = 1; j < 18; j++) {
        for (int i = 1; i <= stcnt - (1 << j) + 1; i++) {
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
        }
    }
    for (int l, r, x, i = 1; i <= m; i++) {
        scanf("%d %d %d", &l, &r, &x);
        que[(l - 1) / B_MODUI].push_back(Query(l, r, x, i));
    }
    for (int i = 0; i <= n / B_MODUI; i++) {
        for (int j = 1; j <= n; j++) {
            layer[j].clear();
        }
        memset(small, 0, sizeof(small));
        memset(big, 0, sizeof(big));
        int l = i * B_MODUI + 1, r = n;
        // printf("[%d, %d]\n", l, r);
        for (int j = 1; j <= n; j++) {
            int u = invdfn[j];
            if (l <= u && u <= r) {
                // printf("add(%d, %d, %d)\n", 1, dep[u] - 1, 1);
                add(1, dep[u] - 1, 1);
                layer[dep[u]].push_back(dfn[u]);
            }
        }
        for (int j = 1; j <= n; j++) {
            build(layer[j]);
        }
        sort(que[i].begin(), que[i].end());
        // printf("------------------ built\n");
        for (Query q : que[i]) {
            while (r > q.r) {
                del(r);
                r--;
            }
            recording = true;
            for (int j = i * B_MODUI + 1; j < q.l; j++) {
                del(j);
            }
            recording = false;
            ans[q.id] = query(q.x);
            // printf("answer %d - query(%d) - %d\n", q.id, q.x, ans[q.id]);
            undo_all();
        }
        // printf("------------------ end\n");
    }
    for (int i = 1; i <= m; i++) {
        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: 2ms
memory: 16776kb

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: 0ms
memory: 14756kb

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: 3ms
memory: 14756kb

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
Wrong Answer

Test #30:

score: 17
Accepted
time: 789ms
memory: 32384kb

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
Wrong Answer
time: 1009ms
memory: 31988kb

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:

5170
-15383
13398
-1701
3554
6061
1857
7661
5180
32442
-3127
-9334
14916
11194
5382
8886
13473
25278
17730
11427
300
-6085
-4456
100
1628
22466
10514
23958
13072
27259
12765
-1988
8997
13493
21960
17461
-8877
-8805
-9843
17389
8546
-12497
12173
-4484
-5431
21114
7164
6964
2105
446
1820
-3367
9850
27...

result:

wrong answer 1st numbers differ - expected: '14606', found: '5170'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 17
Accepted
time: 1777ms
memory: 61296kb

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
Wrong Answer
time: 1965ms
memory: 56756kb

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:

33411
13362
1366
423
42259
2285
12735
-2717
33899
6459
3061
-3050
15938
48938
11754
22848
14393
24451
7669
1086
7680
4226
-185
38521
763
-2156
46735
5093
2951
2689
6538
-11194
35927
13905
15562
6754
35634
-7607
139
47748
-12563
18547
-12252
6761
-3630
9933
-2629
9516
-21139
5208
32852
-6320
5141
337...

result:

wrong answer 1st numbers differ - expected: '44469', found: '33411'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%