Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //#pragma GCC optimize("Ofast")
- using namespace std;
- typedef long long ll;
- #ifdef ONPC
- mt19937 rnd(228);
- #else
- mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
- #endif
- const int BLOCK = 750;
- const int N = 1e5 + 7;
- int dsu[N];
- int sz[N];
- bool bad[N];
- int w[N];
- int u[N], v[N];
- inline int get(int v)
- {
- if (dsu[v] == v) return v;
- return dsu[v] = get(dsu[v]);
- }
- inline void uni(int u, int v)
- {
- u = get(u), v = get(v);
- if (u == v) return;
- if (sz[u] > sz[v]) swap(u, v);
- dsu[u] = v;
- sz[v] += sz[u];
- }
- int gays[N];
- int spanning[N];
- int was_id[N];
- int par[N];
- bool vis[N];
- int ans[N];
- vector <int> events[N];
- vector <pair <int, int> > gr[N];
- bool flex[N];
- int marked[N];
- int ind[N];
- bool cmp(int i, int j) {
- if (w[i] == w[j]) return i < j;
- else return w[i] < w[j];
- }
- int edge_sz = 0;
- int nxt[N];
- int fst[N];
- int id[N];
- int us[N];
- int tt = 0;
- void add(int a, int b) {
- nxt[edge_sz] = fst[a];
- fst[a] = edge_sz;
- id[edge_sz] = b;
- edge_sz++;
- }
- int dfs(int v) {
- if (us[v] == tt) {
- return 0;
- }
- us[v] = tt;
- int sum = sz[v];
- for (int x = fst[v]; x; x = nxt[x]) {
- sum += dfs(id[x]);
- }
- return sum;
- }
- int _a[N], _b[N];
- int main()
- {
- #ifdef ONPC
- freopen("a.in", "r", stdin);
- #endif
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n = 50000, m = 49999;
- int inf = 1e9;
- cin >> n >> m;
- for (int i = 0; i < m; i++)
- {
- u[i] = rnd() % n, v[i] = rnd() % n;
- w[i] = rnd() % inf;
- cin >> u[i] >> v[i] >> w[i];
- u[i]--, v[i]--;
- ind[i] = i;
- w[i] *= -1;
- }
- int q = 100000;
- cin >> q;
- vector <int> t(q), a(q), b(q);
- for (int i = 0; i < q; i++)
- {
- cin >> t[i] >> a[i] >> b[i];
- a[i]--;
- // t[i] = rnd() % 2 + 1;
- //t[i] = rnd() % 2 + 1;
- //a[i] = rnd() % n;
- //b[i] = rnd() % inf;
- //a[i]--;
- b[i] *= -1;
- if (t[i] == 2) events[i].reserve(BLOCK);
- }
- for (int i = 0; i < n; i++)
- {
- dsu[i] = i;
- sz[i] = 1;
- }
- set <pair <int,int> > glob;
- for (int i = 0; i < m; i++)
- {
- glob.insert({w[i], i});
- }
- int x = 0;
- for (auto &c : glob) {
- ind[x++] = c.second;
- }
- vector <pair <int, int> > e;
- int sum = 0;
- for (int i = 0; i < q; i += BLOCK)
- {
- for (int i = 0; i < m; i++) bad[i] = 0, flex[i] = 0;
- int l = i, r = min(q, i + BLOCK);
- for (int ptr = l; ptr < r; ptr++)
- {
- if (t[ptr] == 1)
- {
- bad[a[ptr]] = 1;
- }
- }
- for (int i = 0; i < n; i++) dsu[i] = i, sz[i] = 1;
- for (int i = 0; i < m; i++)
- {
- int id = ind[i];
- if (!bad[id] && get(u[id]) != get(v[id]))
- {
- uni(u[id], v[id]);
- flex[id] = true;
- }
- }
- for (int i = 0; i < n; i++) dsu[i] = i,sz[i] = 1;
- int span_len = 0;
- for (int i = 0; i < m; i++)
- {
- int id = ind[i];
- if (get(u[id]) != get(v[id]))
- {
- uni(u[id], v[id]);
- flex[id] = false;
- if (!bad[id])
- {
- spanning[span_len++] = id;
- }
- }
- }
- for (int i = 0; i < n; i++) dsu[i] = i, sz[i] = 1;
- sum = 0;
- for (int i = 0; i < m; i++)
- {
- if (bad[i] || flex[i])
- {
- marked[sum++] = i;
- }
- }
- e.clear();
- for (int ptr = l; ptr < r; ptr++)
- {
- if (t[ptr] == 1)
- {
- w[a[ptr]] = b[ptr];
- }
- else
- {
- for (int i = 0; i < sum; i++)
- {
- if (w[marked[i]] > b[ptr]) continue;
- events[ptr].push_back(marked[i]);
- }
- e.push_back({b[ptr], ptr});
- }
- }
- sort(e.begin(), e.end());
- for (int i = 0; i < n; i++)
- {
- dsu[i] = i;
- sz[i] = 1;
- }
- int _st = -1;
- for (int i = 0; i <= span_len; i++)
- {
- while (_st + 1 < (int) e.size() && (i == span_len || e[_st + 1].first < w[spanning[i]]))
- {
- _st++;
- int id = e[_st].second;
- fst[get(a[id])] = 0;
- int root = get(a[id]);
- for (int x : events[id])
- {
- int l = get(u[x]);
- fst[l] = 0;
- l = get(v[x]);
- fst[l] = 0;
- }
- edge_sz = 1;
- for (int x : events[id]) {
- int a = get(u[x]), b = get(v[x]);
- add(a, b);
- add(b, a);
- }
- tt++;
- ans[id] = dfs(root);
- }
- if (i == span_len) break;
- uni(u[spanning[i]], v[spanning[i]]);
- }
- int ptr_a = 0, ptr_b = 0;
- for (int i = 0; i < m; i++) {
- int id = ind[i];
- if (bad[id] || flex[id]) {
- _a[ptr_a++] = id;
- } else {
- _b[ptr_b++] = id;
- }
- }
- sort(_a, _a + ptr_a, cmp);
- merge(_a, _a + ptr_a, _b, _b + ptr_b, ind, cmp);
- }
- for (int i = 0; i < q; i++)
- {
- if (t[i] == 2)
- {
- cout << ans[i] << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement