Submission #1245317

#TimeUsernameProblemLanguageResultExecution timeMemory
1245317nh0902Bridges (APIO19_bridges)C++20
13 / 100
3092 ms24980 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 5e4 + 10;

const int M = 1e5 + 10;

const int inf = 1e18;

int n, m, q, sq, k;

int u[M], v[M], d[M], cur_d[M];

int t[M], pos[M], w[M];

int pa[N], sz[N];

stack<int> store;

int ans[M];

void reset_dsu() {
    for (int i = 1; i <= n; i ++) {
        pa[i] = i;
        sz[i] = 1;
    }
    while (!store.empty()) store.pop();
}

int find_anc(int x) {
    return pa[x] == x ? x : find_anc(pa[x]);
}

void mrg(int a, int b) {
    a = find_anc(a);
    b = find_anc(b);
    if (a == b) return;
    if (sz[a] > sz[b]) swap(a, b);
    pa[a] = b;
    sz[b] += sz[a];
    store.push(a);
}

void roll_back(int x) {
    while (store.size() > x) {
        int a = store.top();
        store.pop();
        sz[pa[a]] -= sz[a];
        pa[a] = a;
    }
}

void prep() {
    cin >> n >> m;
    sq = min((int) 1000, n);
    vector<int> V;
    for (int i = 1; i <= m; i ++) {
        cin >> u[i] >> v[i] >> d[i];
        V.push_back(d[i]);
    }
    cin >> q;
    for (int i = 1; i <= q; i ++) {
        cin >> t[i] >> pos[i] >> w[i];
        V.push_back(w[i]);
    }
    sort(V.begin(), V.end());
    map<int, int> mp;
    mp[V[0]] = 1;
    for (int i = 1; i < V.size(); i ++) {
        if (V[i] > V[i - 1]) mp[V[i]] = mp[V[i - 1]] + 1;
        k = max(k, mp[V[i]]);
    }

    for (int i = 1; i <= m; i ++) cur_d[i] = d[i] = mp[d[i]];
    for (int i = 1; i <= q; i ++) w[i] = mp[w[i]];

    //for (int i = 1; i <= m; i ++) cout << d[i] << "\n";
    //for (int i = 1; i <= q; i ++) cout << w[i] << "\n";
}

bool cmp_query(int i, int j) {
    return w[i] > w[j];
}

vector<int> to_mrg[1001];
vector<int> E[2 * M];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    prep();
    
    vector<bool> unchange(m + 1, true);

    int cur = 1;
    while (cur <= q) {
        for (int i = 1; i <= m; i ++) {
            E[d[i]].push_back(i);
        }

        vector<int> query;
        vector<int> update;
        for (int i = cur; i <= min(cur + sq - 1, q); i ++) {
            if (t[i] == 1) {
                update.push_back(i);
                unchange[pos[i]] = false;

            } else {
                query.push_back(i);
            }
        }

        for (int i = cur; i <= min(cur + sq - 1, q); i ++) {
            if (t[i] == 1) d[pos[i]] = w[i];
            else {
                to_mrg[i - cur].clear();
                for (int j : update) {
                    if (d[pos[j]] >= w[i]) to_mrg[i - cur].push_back(pos[j]);
                }
            }
        }

        reset_dsu();
        sort(query.begin(), query.end(), cmp_query);
        int cur_w = k;

        for (int x : query) {

            //cout << store.size() << "\n";

            while (cur_w >= w[x]) {
                for (int i : E[cur_w]) if (unchange[i]) {
                    mrg(u[i], v[i]);
                }
                cur_w --;
            }


            int pre = store.size();
            for (int i : to_mrg[x - cur]) {
                mrg(u[i], v[i]);
            }

            ans[x] = sz[find_anc(pos[x])];

            roll_back(pre);

            //cout << cnt << "\n\n";

        }

        cur += sq;

        for (int i : update) unchange[pos[i]] = true;
        for (int i = 1; i <= k; i ++) E[i].clear();
    }
    
    for (int i = 1; i <= q; i ++) {
        if (t[i] == 2) cout << ans[i] << "\n";
    }

    return 0;
}








#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...
#Verdict Execution timeMemoryGrader output
Fetching results...