Submission #903446

# Submission time Handle Problem Language Result Execution time Memory
903446 2024-01-11T07:48:11 Z GrindMachine Synchronization (JOI13_synchronization) C++17
30 / 100
1466 ms 30500 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://github.com/mostafa-saad/MyCompetitiveProgramming/blob/master/Olympiad/JOI/JOIOC-13-synchronization.txt

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int siz;
    vector<T> tree;

    fenwick() {

    }

    fenwick(int n) {
        siz = n;
        tree = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void build(vector<T> &a, int n) {
        for (int i = 1; i <= n; ++i) {
            int par = i + lsb(i);
            tree[i] += a[i];

            if (par <= siz) {
                tree[par] += tree[i];
            }
        }
    }

    void pupd(int i, T v) {
        while (i <= siz) {
            tree[i] += v;
            i += lsb(i);
        }
    }

    T sum(int i) {
        T res = 0;

        while (i) {
            res += tree[i];
            i -= lsb(i);
        }

        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }
};

vector<pll> adj[N];

struct lca_algo {
    // LCA template (for graphs with 1-based indexing)
 
    int LOG = 1;
    vector<int> depth;
    vector<vector<int>> up;
    vector<int> tin, tout;
    vector<int> edge_node;
    int timer = 1;
 
    lca_algo() {
 
    }
 
    lca_algo(int n) {
        lca_init(n);
    }
 
    void lca_init(int n) {
        while ((1 << LOG) < n) LOG++;
        up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
        depth = vector<int>(n + 1);
        tin = vector<int>(n + 1);
        tout = vector<int>(n + 1);
        edge_node = vector<int>(n + 1);
        lca_dfs(1, -1);
    }
 
    void lca_dfs(int node, int par) {
        tin[node] = timer++;
 
        for(auto [child,id] : adj[node]) {
            if (child == par) conts;
 
            up[child][0] = node;
            rep1(j, LOG - 1) {
                up[child][j] = up[up[child][j - 1]][j - 1];
            }
 
            depth[child] = depth[node] + 1;
            edge_node[id] = child;

            lca_dfs(child, node);
        }
 
        tout[node] = timer-1;
    }
 
    int lift(int u, int k) {
        rep(j, LOG) {
            if (k & (1 << j)) {
                u = up[u][j];
            }
        }
 
        return u;
    }
 
    int query(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int k = depth[u] - depth[v];
        u = lift(u, k);
 
        if (u == v) return u;
 
        rev(j, LOG - 1, 0) {
            if (up[u][j] != up[v][j]) {
                u = up[u][j];
                v = up[v][j];
            }
        }
 
        u = up[u][0];
        return u;
    }
 
    int get_dis(int u, int v) {
        int lca = query(u, v);
        return depth[u] + depth[v] - 2 * depth[lca];
    }
 
    bool is_ances(int u, int v){
        return tin[u] <= tin[v] and tout[u] >= tout[v];
    }
};

void solve(int test_case)
{
    ll n,m,q; cin >> n >> m >> q;
    vector<pll> edges(n+5);

    rep1(i,n-1){
        ll u,v; cin >> u >> v;
        adj[u].pb({v,i}), adj[v].pb({u,i});
        edges[i] = {u,v};
    }

    vector<ll> a(n+5);
    rep1(i,n) a[i] = 1;

    lca_algo LCA(n);
    fenwick<ll> fenw(n+5);

    auto edge_change = [&](ll id, ll val){
        ll u = LCA.edge_node[id];
        fenw.pupd(LCA.tin[u],val);
        fenw.pupd(LCA.tout[u]+1,-val);
    };

    auto path_sum = [&](ll u, ll v){
        assert(LCA.is_ances(u,v));
        return fenw.query(LCA.tin[u],LCA.tin[v]);
    };

    auto find_cc = [&](ll u){
        ll l = 1, r = LCA.depth[u];
        ll mx_ances = u;

        while(l <= r){
            ll mid = (l+r) >> 1;
            ll just_below = LCA.lift(u,mid-1);
            ll ances = LCA.up[just_below][0];
            ll len = LCA.depth[u]-LCA.depth[ances];
            ll sum = path_sum(just_below,u);
            if(sum == len){
                mx_ances = ances;
                l = mid+1;
            } 
            else{
                r = mid-1;
            }
        }

        return mx_ances;
    };

    vector<bool> active(n+5);
    vector<ll> edge_sub(n+5);

    while(m--){
        ll id; cin >> id;
        auto [u,v] = edges[id];
        ll pu = find_cc(u), pv = find_cc(v);
        
        if(!active[id]){
            active[id] = 1;
            ll new_val = a[pu]+a[pv]-edge_sub[id];
            edge_sub[id] = 0;
            edge_change(id,1);
            pu = find_cc(u);
            a[pu] = new_val;
        }
        else{
            active[id] = 0;
            // assert(pu == pv);
            edge_sub[id] = a[pu];
            edge_change(id,-1);
            ll pu_new = find_cc(u), pv_new = find_cc(v);
            a[pu_new] = a[pv_new] = a[pu];
        }
    }

    while(q--){
        ll u; cin >> u;
        ll pu = find_cc(u);
        ll ans = a[pu];
        cout << ans << endl;
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Incorrect 1 ms 2652 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 388 ms 26884 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 5 ms 2904 KB Output is correct
7 Correct 54 ms 5372 KB Output is correct
8 Correct 1466 ms 29996 KB Output is correct
9 Correct 1446 ms 30004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1321 ms 30500 KB Output is correct
2 Correct 506 ms 30048 KB Output is correct
3 Correct 525 ms 30248 KB Output is correct
4 Correct 502 ms 30288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Incorrect 1 ms 2652 KB Output isn't correct
3 Halted 0 ms 0 KB -