Submission #1247140

#TimeUsernameProblemLanguageResultExecution timeMemory
1247140arvindr9Synchronization (JOI13_synchronization)C++20
100 / 100
229 ms33916 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
 
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

#define int long long
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define eb emplace_back
#define f first
#define s second
#define lep(i,a,b) for (int i = (a); i <= (b); i++)
#define rep(i,a,b) for (int i = (a); i >= (b); i--)
const int inf = 1e18;
int t;

const int maxn = 1e5 + 5;
vector<int> adj[maxn];
const int ln = 20;
int F[maxn][ln+1];
int tin[maxn], tout[maxn];
int bit[maxn];
int depth[maxn];
int timer = 0;

void dfs(int u, int p) {
    tin[u] = ++timer;
    F[u][0] = p;
    lep(i,1,ln) {
        F[u][i] = F[F[u][i-1]][i-1];
    }
    for (int v: adj[u]) {
        if (v != p) {
            depth[v] = depth[u] + 1;
            dfs(v,u);
        }
    }
    tout[u] = timer;
}

void add(int pos, int delta) {
    for (int i = pos; i < maxn; i += i&-i) {
        bit[i] += delta;
    }
}

int query(int pos) {
    int res = 0;
    for (int i = pos; i > 0; i -= i&-i) {
        res += bit[i];
    }
    return res;
}

int get_root(int u) {
    // keep jumping up, and don't change the depth
    int qu = query(tin[u]);
    rep(i,ln,0) {
        int v = F[u][i];
        if (query(tin[v]) == qu) {
            u = v;
        }
    }
    return u;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    vector<pi> edges(n);
    lep(i,1,n-1) {
        int u,v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
        edges[i] = {u,v};
    }

    DEBUG(adj[1]);


    
    
    dfs(1,0);

    DEBUG("dfsed");
    
    lep(i,1,n) {
        add(tin[i], +1);
        add(tout[i]+1, -1);
    }
    DEBUG("added");
    vector<int> cnt0(n+1,0), cnt1(n+1,1);

    lep(i,1,n-1) {
        if (depth[edges[i].f] < depth[edges[i].s]) swap(edges[i].f, edges[i].s);
    }

    DEBUG(edges);

    vector<bool> on(n+1);

    lep(mm,1,m) {
        int dj; cin >> dj;
        // DEBUG(dj);
        auto [u,p] = edges[dj];
        DEBUG(dj,u,p);
        // DEBUG(query(1),query(2),query(5));
        if (on[u]) {
            // turn it off
            int r = get_root(u);
            DEBUG(r);
            cnt0[u] = cnt1[u] = cnt1[r];
            add(tin[u],+1);add(tout[u]+1,-1);
            on[u] = false;
        } else {
            int rp = get_root(p);
            int new_cnt = cnt1[rp] + cnt1[u] - cnt0[u];
            cnt1[rp] = new_cnt;
            add(tin[u],-1);
            add(tout[u]+1,+1);
            on[u] = true;
        }
        DEBUG(cnt0,cnt1);
    }

    lep(qq,1,q) {
        int x;
        cin >> x;
        int rx = get_root(x);
        cout << cnt1[rx] << "\n";
    }


}
/*
4 1
1 2
1 3
1 4
2 3
2 4
1 3
*/

// ans should be 3
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...