QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#857866#9676. Ancestorscancan1234560 1638ms83916kbC++145.5kb2025-01-16 08:30:232025-01-16 08:30:24

Judging History

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

  • [2025-01-16 08:30:24]
  • 评测
  • 测评结果:0
  • 用时:1638ms
  • 内存:83916kb
  • [2025-01-16 08:30:23]
  • 提交

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], dfncnt, invdfn[N], st[2 * N][18], stcnt, _log2[2 * N], start[N];
void dfs(int 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;
    }
    int d = dep_LCA(u, v);
    add(dep[u] - d, dep[u] - 1, w);
}
void del(int v) {
    add(1, dep[v] - 1, -1);
    remove(v);
    int u = node[v].prv;
    int w = node[v].nxt;
    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(vec[i - 1], 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();
    }
}
char input[32 * 1024 * 1024];
int read() {
    static char *p = input;
    int x = 0;
    while (*p < 48) {
        p++;
    }
    while (*p >= 48) {
        x = x * 10 + *p - 48;
        p++;
    }
    return x;
}
char output[7 * M], *output_p;
void write_char(char c) {
    *output_p = c;
    output_p++;
}
void write(int x) {
    do {
        write_char(x % 10 + 48);
        x /= 10;
    } while (x != 0);
    write_char(10);
}
int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    fread(input, 1, sizeof(input), stdin);
    int n, m;
    n = read();
    m = read();
    int root = 0;
    for (int u = 1; u <= n; u++) {
        fa[u] = read();
        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++) {
        l = read();
        r = read();
        x = read();
        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;
        for (int j = 1; j <= n; j++) {
            int u = invdfn[j];
            if (l <= u && u <= r) {
                add(1, dep[u] - 1, 1);
                layer[dep[u]].push_back(u);
            }
        }
        for (int j = 1; j <= n; j++) {
            build(layer[j]);
        }
        sort(que[i].begin(), que[i].end());
        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);
            undo_all();
        }
    }
    output_p = output;
    for (int i = 1; i <= m; i++) {
        write(ans[i]);
    }
    fwrite(output, 1, output_p - output, stdout);
    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: 1ms
memory: 17656kb

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

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:

254
76
116
621
923
684
453
52
955
585
481
562
675
611
984
982
741
782
31
282
151
291
641
141
841
131
34
27
96
92
5
51
75
01
9
61
78
261
91
712
232
42
871
433
301
931
392
004
992
153
925
236
295
692
046
876
517
807
25
564
223
137
2
96
011
682
271
0
04
13
61
501
57
54
8
49
045
511
753
7
11
134
185
73
...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 761ms
memory: 44236kb

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:

54021
1231
29221
4442
34423
43563
57583
50503
46461
84661
35174
44114
10432
2671
76581
1386
68462
61792
5098
59261
96583
09992
16022
8367
0283
45743
28571
6646
23621
48332
75611
39461
43442
30511
5493
5022
60873
63031
25092
41142
85193
20743
62373
51802
66711
35214
55344
70871
25432
71762
70134
4331...

result:

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

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 1638ms
memory: 83916kb

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:

65925
76781
9131
50431
12011
554
59505
18418
3186
0463
73985
19901
07
3174
63
7159
13793
6611
64376
73647
7662
28154
4194
4776
5261
8914
07225
53403
73106
45684
86792
5182
6486
19037
44912
39694
3299
59764
78792
6686
5342
37702
9592
66643
7734
8242
2854
7257
29283
3527
6853
71836
57082
82834
51202
7...

result:

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

Subtask #6:

score: 0
Skipped

Dependency #1:

0%