답안 #1090207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1090207 2024-09-18T02:08:15 Z efishel 동기화 (JOI13_synchronization) C++17
100 / 100
290 ms 35712 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;

const ll MAXN = 1E5+16, LOG = 17;
ll anc[MAXN][LOG];
vii adj[MAXN];
bool isOn[MAXN];
ii edg[MAXN];
ll ans[MAXN];
ll last[MAXN];
ll tin[MAXN], tout[MAXN], timer = 0;

struct Fenwick {
    vll tree;
    ll n;

    Fenwick (ll n): tree(n), n(n) {}

    void update (ll ql, ll qr, ll val) { update(ql, val); update(qr+1, -val); }
    void update (ll id, ll val) {
        for (; id < n; id |= id+1) tree[id] += val;
    }

    ll query (ll id) {
        ll ans = 0;
        for (; id >= 0; id &= id+1, id--) ans += tree[id];
        return ans;
    }
} ft(MAXN);

void dfs (ll u, ll par) {
    tin[u] = timer++;
    anc[u][0] = par;
    for (ll bit = 1; bit < LOG; bit++) anc[u][bit] = anc[anc[u][bit-1]][bit-1];
    for (auto [v, id] : adj[u]) {
        if (v == par) continue;
        edg[id] = { u, v };
        dfs(v, u);
    }
    tout[u] = timer-1;
    ft.update(tin[u], tout[u], 1); // make the subtree have +1
}

ll findF0 (ll u, ll bit) {
    if (bit == -1) return u;
    if (ft.query(tin[anc[u][bit]]) - ft.query(tin[u]) == 0) {
        return findF0(anc[u][bit], bit-1);
    } else {
        return findF0(u, bit-1);
    }
}

int main () {
    cin.tie(nullptr) -> sync_with_stdio(false);
    ll n, Q, qa;
    cin >> n >> Q >> qa;
    for (ll id = 1; id < n; id++) {
        ll u, v;
        cin >> u >> v;
        u--; v--;
        adj[u].push_back({ v, id });
        adj[v].push_back({ u, id });
    }
    fill(isOn, isOn+n, false);
    fill(ans, ans+n, 1);
    fill(last, last+n, 0);
    dfs(0, 0);
    auto getAns = [&](ll u) -> ll& { return ans[findF0(u, LOG-1)]; };
    while (Q--) {
        ll id;
        cin >> id;
        auto [parU, u] = edg[id];
        if (isOn[id] ^= 1) {
            ll &ansU = getAns(u);
            assert(ansU == ans[u]);
            ll &ansParU = getAns(parU);
            ll sumU = ansParU - last[id]; // how much v has grown by itself
            ll sumV = ansU - last[id]; // how much u has grown by itself
            ll newAns = last[id] + sumU + sumV;
            // ansU = newAns;
            ansParU = newAns;
            ft.update(tin[u], tout[u], -1);
        } else {
            ans[u] = last[id] = getAns(parU);
            ft.update(tin[u], tout[u], +1);
        }
    }
    while (qa--) {
        ll u;
        cin >> u;
        u--;
        cout << getAns(u) << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 1 ms 3628 KB Output is correct
5 Correct 1 ms 3620 KB Output is correct
6 Correct 2 ms 3684 KB Output is correct
7 Correct 16 ms 6136 KB Output is correct
8 Correct 16 ms 5980 KB Output is correct
9 Correct 16 ms 5956 KB Output is correct
10 Correct 203 ms 29012 KB Output is correct
11 Correct 188 ms 29120 KB Output is correct
12 Correct 250 ms 34844 KB Output is correct
13 Correct 113 ms 28104 KB Output is correct
14 Correct 140 ms 28484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 31436 KB Output is correct
2 Correct 115 ms 31176 KB Output is correct
3 Correct 118 ms 34132 KB Output is correct
4 Correct 118 ms 34132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3416 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3624 KB Output is correct
4 Correct 2 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 4 ms 3932 KB Output is correct
7 Correct 21 ms 6776 KB Output is correct
8 Correct 285 ms 35496 KB Output is correct
9 Correct 283 ms 35712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 35668 KB Output is correct
2 Correct 163 ms 35236 KB Output is correct
3 Correct 157 ms 35320 KB Output is correct
4 Correct 156 ms 35308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 3420 KB Output is correct
5 Correct 4 ms 3704 KB Output is correct
6 Correct 20 ms 6208 KB Output is correct
7 Correct 237 ms 29872 KB Output is correct
8 Correct 290 ms 35556 KB Output is correct
9 Correct 139 ms 29376 KB Output is correct
10 Correct 167 ms 29776 KB Output is correct
11 Correct 142 ms 32716 KB Output is correct
12 Correct 141 ms 32708 KB Output is correct
13 Correct 171 ms 35408 KB Output is correct