QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#857724#9676. Ancestorscancan1234560 1736ms61348kbC++145.3kb2025-01-16 00:34:032025-01-16 00:34:04

Judging History

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

  • [2025-01-16 00:34:04]
  • 评测
  • 测评结果:0
  • 用时:1736ms
  • 内存:61348kb
  • [2025-01-16 00:34:03]
  • 提交

answer

#include <stack>
#include <cstdio>
#include <vector>
#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);
    v = dfn[v];
    remove(v);
    // printf("add(%d, %d, %d)\n", 1, dep[v] - 1, -1);
    add(1, dep[v] - 1, -1);
    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();
        }
        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;
            while (l < q.l) {
                del(l);
                l++;
            }
            recording = false;
            ans[q.id] = query(q.x);
            // printf("answer %d - query(%d) - %d\n", q.id, q.x, ans[q.id]);
            l = i * B_MODUI + 1;
            undo_all();
        }
    }
    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: 0ms
memory: 14160kb

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: 15020kb

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:

448
65
601
132
337
510
386
34
623
692
240
329
703
178
618
408
219
426
19
479
239
351
251
260
353
405
141
259
362
310
66
19
61
16
12
23
94
171
19
230
250
22
172
337
101
133
283
403
286
338
531
624
588
273
639
676
710
700
51
464
326
733
50
190
235
286
235
57
111
37
16
151
86
104
29
132
537
175
468
4
1...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 794ms
memory: 34256kb

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:

12081
1330
12371
2468
32751
37008
39131
30938
16894
17181
48432
42276
24109
1692
19199
6984
27143
30568
9063
17288
40110
31213
22969
7720
3946
36130
18516
6818
13548
24425
12408
17980
25460
12277
4201
2101
40845
14684
31479
25891
42785
37239
41215
23059
13116
46029
49678
19776
25971
29983
49007
4726...

result:

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

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 1736ms
memory: 61348kb

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:

52991
18977
1376
13479
11119
453
50619
81492
6849
3716
58975
11165
76
4795
42
9590
39828
1210
67398
74716
2713
45187
4952
6875
1665
4241
52332
30727
60174
48651
29899
2834
6963
73143
22018
49729
10036
46809
29961
6898
2472
20835
2971
34865
4390
2414
4592
7571
38486
7232
3574
63937
28331
43823
20475
...

result:

wrong answer 1st numbers differ - expected: '52956', found: '52991'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%