QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#327183#3307. Query on a Tree 17HunsterWA 7ms6304kbC++143.6kb2024-02-14 20:19:462024-02-14 20:19:47

Judging History

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

  • [2024-02-14 20:19:47]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:6304kb
  • [2024-02-14 20:19:46]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;
#define int LL

constexpr int N = 100005;

int n;
std::vector<int> graph[N];
int dfs_cnt, fa[N][20], dfn[N], idfn[N], hson[N], top[N], size[N], dep[N];

void dfs1(int u, int p) {
    dep[u] = dep[p] + 1;
    fa[u][0] = p;
    for (int i = 1; i < 20; i++)
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    size[u] = 1;
    for (int v : graph[u]) {
        if (v == p) continue;
        dfs1(v, u);
        size[u] += size[v];
        if (size[v] > size[hson[u]]) hson[u] = v;
    }
}
void dfs2(int u, int t) {
    top[u] = t;
    dfn[u] = ++dfs_cnt;
    idfn[dfn[u]] = u;
    if (hson[u]) dfs2(hson[u], t);
    for (int v : graph[u]) {
        if (v == fa[u][0]) continue;
        if (v == hson[u]) continue;
        dfs2(v, v);
    }
}

struct ST {
    LL lazy[N << 2], sum[N << 2];
    constexpr int ls(int u) { return u << 1; }
    constexpr int rs(int u) { return u << 1 | 1; }
    void push_down(int u, int l, int r) {
        int mid = (l + r) >> 1;
        if (lazy[u]) {
            lazy[ls(u)] += lazy[u];
            sum[ls(u)] += (mid - l + 1) * lazy[u];
            lazy[rs(u)] += lazy[u];
            sum[rs(u)] += (r - mid) * lazy[u];
            lazy[u] = 0;
        }
    }
    void push_up(int u) { sum[u] = sum[ls(u)] + sum[rs(u)]; }
    void update(int u, int l, int r, int x, int y, int v) {
        if (x <= l && r <= y) {
            lazy[u] += v;
            sum[u] += 1ll * (r - l + 1) * v;
            return;
        }
        int mid = (l + r) >> 1;
        push_down(u, l, r);
        if (x <= mid) update(ls(u), l, mid, x, y, v);
        if (y > mid) update(rs(u), mid + 1, r, x, y, v);
        push_up(u);
    }
    LL query(int u, int l, int r, int x, int y) {
        if (x <= l && r <= y) return sum[u];
        int mid = (l + r) >> 1;
        push_down(u, l, r);
        LL res = 0;
        if (x <= mid) res += query(ls(u), l, mid, x, y);
        if (y > mid) res += query(rs(u), mid + 1, r, x, y);
        return res;
    }
    int lower_bound(int u, int l, int r, LL v) {
        if (l == r) return l;
        int mid = (l + r) >> 1;
        if (sum[ls(u)] >= v) return lower_bound(ls(u), l, mid, v);
        else return lower_bound(rs(u), mid + 1, r, v - sum[ls(u)]);
    }
};
ST st;

void update(int x, int y, int v) {
    while (top[x] != top[y]) {
        if (dep[top[x]] > dep[top[y]]) std::swap(x, y);
        st.update(1, 1, n, dfn[top[y]], dfn[y], v);
        y = fa[top[y]][0];
    }
    if (dep[x] > dep[y]) std::swap(x, y);
    st.update(1, 1, n, dfn[x], dfn[y], v);
}

LL query(int u) { return st.query(1, 1, n, dfn[u], dfn[u] + size[u] - 1); }

signed main() {
    scanf("%lld", &n);
    for (int i = 1; i < n; i++) {
        int x, y;
        scanf("%lld%lld", &x, &y);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    int q;
    scanf("%lld", &q);
    while (q--) {
        int op;
        scanf("%lld", &op);
        if (op == 1) {
            int u;
            scanf("%lld", &u);
            st.update(1, 1, n, dfn[u], dfn[u] + size[u] - 1, 1);
        }
        else {
            int x, y;
            scanf("%lld%lld", &x, &y);
            update(x, y, 1);
        }
        LL all = st.sum[1];
        int u = idfn[st.lower_bound(1, 1, n, all / 2 + 1)];
        if (query(u) * 2 <= all)  {
            for (int i = 19; i >= 0; i--)
                if (fa[u][i] && query(fa[u][i]) * 2 <= all)
                    u = fa[u][i];
            u = fa[u][0];
        }
        printf("%lld\n", u);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
1 6
1 7
7 3
3 2
7 5
5 4
4
1 2
1 4
1 6
2 6 7

output:

2
7
7
1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 6268kb

input:

15
1 2
1 3
2 4
3 7
6 2
3 8
4 9
2 5
5 11
7 13
10 4
11 15
12 5
14 7
11
2 6 11
1 6
2 10 9
1 9
2 2 6
1 4
2 7 6
1 2
2 8 13
1 10
2 8 15

output:

2
2
2
2
2
2
2
2
2
2
2

result:

ok 11 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 6252kb

input:

2
1 2
1
1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 1ms
memory: 6276kb

input:

2
1 2
1
2 1 2

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 6ms
memory: 6208kb

input:

100
1 2
58 2
2 87
63 87
65 63
65 19
6 87
58 21
87 8
87 74
91 6
95 65
2 61
87 59
3 61
37 87
67 87
23 2
63 9
87 46
40 67
70 2
12 58
46 75
75 36
28 3
12 33
72 87
39 6
75 52
85 70
1 76
26 40
47 28
2 49
41 65
66 28
51 37
15 47
86 51
8 60
97 19
48 58
72 90
33 83
97 54
36 5
23 14
78 52
90 7
99 2
48 82
48 6...

output:

72
3
3
87
87
2
2
2
2
87
87
87
87
87
2
87
87
87
87
87
87
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87
87...

result:

ok 10000 lines

Test #6:

score: 0
Accepted
time: 2ms
memory: 6172kb

input:

100
27 1
27 35
96 1
54 35
25 54
81 35
69 25
27 18
69 83
27 99
70 83
81 61
1 77
73 54
35 68
61 89
20 89
99 95
37 20
63 95
95 38
7 83
63 78
86 35
89 13
71 77
70 14
95 34
13 62
24 95
77 98
99 33
25 100
36 63
35 8
65 54
66 83
40 8
15 81
78 59
15 4
62 28
97 14
15 58
69 3
89 44
47 100
52 73
12 95
53 38
39...

output:

46
35
46
83
83
83
83
35
35
35
25
25
25
35
25
25
25
25
25
54
54
35
35
35
54
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 5ms
memory: 6284kb

input:

100
1 19
19 45
58 45
58 72
36 45
45 94
94 13
90 58
64 90
90 23
45 12
90 52
27 36
19 42
72 35
23 32
67 36
1 18
54 36
33 67
10 1
55 90
54 65
92 72
53 42
65 24
9 45
81 42
85 35
8 81
10 44
85 68
23 30
69 12
43 69
23 25
12 88
85 99
91 9
24 100
48 81
60 94
33 41
52 51
17 19
51 82
30 49
32 28
52 7
82 79
82...

output:

33
45
45
45
45
45
58
58
58
58
58
45
58
45
58
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
...

result:

ok 10000 lines

Test #8:

score: -100
Wrong Answer
time: 7ms
memory: 6304kb

input:

100
67 1
34 67
34 72
26 72
63 26
26 14
63 24
63 49
42 14
45 34
14 71
71 87
71 7
14 22
64 72
90 7
14 58
87 99
33 87
24 70
65 64
42 30
33 74
99 62
3 71
60 90
84 90
40 70
47 45
84 69
89 7
57 3
99 59
66 65
58 76
37 14
6 37
72 54
38 24
15 99
51 84
93 76
59 8
89 53
75 51
97 99
20 8
28 53
95 15
11 53
95 13...

output:

37
37
14
14
14
14
71
14
14
14
14
14
14
14
14
14
14
14
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
14
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
71
...

result:

wrong answer 1st lines differ - expected: '21', found: '37'