QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#481923#8716. 树Pia_owoWA 1ms3616kbC++202.0kb2024-07-17 16:05:312024-07-17 16:05:31

Judging History

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

  • [2024-07-17 16:05:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3616kb
  • [2024-07-17 16:05:31]
  • 提交

answer

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <functional>
using namespace std;

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> head(n + 1), nxt(n * 2), to(n * 2);
    for (int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        to[i * 2] = v;
        nxt[i * 2] = head[u];
        head[u] = i * 2;
        to[i * 2 + 1] = u;
        nxt[i * 2 + 1] = head[v];
        head[v] = i * 2 + 1;
    }
    vector<int> depth(n + 1);
    vector<array<int, 20>> fa(n + 1);
    function<void(int)> build_tree = [&](int cur) {
        for (int e = head[cur]; e; e = nxt[e]) {
            int u = to[e];
            if (u != fa[cur][0]) {
                fa[u][0] = cur;
                depth[u] = depth[cur] + 1;
                for (int i = 0; fa[u][i]; i++)
                    fa[u][i + 1] = fa[fa[u][i]][i];
                build_tree(u);
            }
        }
    };
    build_tree(1);
    vector<int> b(m);
    for (int i = 0; i < m; ++i)
        cin >> b[i];
    vector<bool> is_target(m);
    int cnt = 0;
    function lca = [&](int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        for (int i = 19; i >= 0; --i)
            if (1 << i <= u - v) u = fa[u][i];
        if (u == v) return u;
        for (int i = 19; i >= 0; --i)
            if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; }
        return fa[u][0];
    };
    function update = [&](int i) {
        cnt -= is_target[i];
        if (i == 0 || i == m - 1) is_target[i] = true;
        else {
            int A = b[i - 1], B = b[i], C = b[i + 1];
            int R = lca(A, C);
            if (depth[B] >= depth[R] && (lca(A, B) == B || lca(B, C) == B)) is_target[i] = false;
            else is_target[i] = true;
        }
        cnt += is_target[i];
    };
    for (int i = 0; i < m; ++i)
        update(i);
    for (int i = 0; i < q; ++i) {
        int p, w; cin >> p >> w;
        --p;
        b[p] = w;
        if (p > 0) update(p - 1);
        update(p);
        if (p < m - 1) update(p + 1);
        cout << cnt << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3616kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

198
198
198
198
198
198
197
197
196
196
196
196
197
197
197
197
197
197
197
197
197
197
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
198
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
...

result:

wrong answer 1st numbers differ - expected: '174', found: '198'