QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#327168#3307. Query on a Tree 17HunsterWA 1ms8996kbC++143.6kb2024-02-14 20:11:332024-02-14 20:11:33

Judging History

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

  • [2024-02-14 20:11:33]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8996kb
  • [2024-02-14 20:11:33]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;

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 upper_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 upper_bound(ls(u), l, mid, v);
        else return upper_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[y], dfn[x], v);
}

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

int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    int q;
    scanf("%d", &q);
    while (q--) {
        int op;
        scanf("%d", &op);
        if (op == 1) {
            int u;
            scanf("%d", &u);
            st.update(1, 1, n, dfn[u], dfn[u] + size[u] - 1, 1);
        }
        else {
            int x, y;
            scanf("%d%d", &x, &y);
            update(x, y, 1);
        }
        LL all = st.sum[1];
        int u = idfn[st.upper_bound(1, 1, n, all / 2)];
        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("%d\n", u);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8584kb

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: -100
Wrong Answer
time: 0ms
memory: 8996kb

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:

6
6
6
2
2
4
2
2
2
2
2

result:

wrong answer 1st lines differ - expected: '2', found: '6'