제출 #551013

#제출 시각아이디문제언어결과실행 시간메모리
551013valerikk다리 (APIO19_bridges)C++17
100 / 100
2874 ms10912 KiB
#include <bits/stdc++.h> using namespace std; const int N = 50057; const int M = 100123; const int B = 500; int n, m; int a[M], b[M], c[M]; int Q; int t[M], x[M], y[M]; int dsu[N]; int sz[N]; vector<int> g[N]; bool used[N]; int ans[M]; int e[M]; bool inb[M]; bool was[M]; int q[N]; int get(int i) { return i == dsu[i] ? i : dsu[i] = get(dsu[i]); } void merge(int i, int j) { i = get(i); j = get(j); if (i == j) return; if (sz[i] > sz[j]) swap(i, j); sz[j] += sz[i]; dsu[i] = j; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i] >> c[i]; --a[i]; --b[i]; } vector<int> ord(m); iota(ord.begin(), ord.end(), 0); sort(begin(ord), end(ord), [&](const int &i, const int &j) { return c[i] > c[j]; }); cin >> Q; for (int i = 0; i < Q; ++i) { cin >> t[i] >> x[i] >> y[i]; --x[i]; } for (int bl = 0; bl < Q; bl += B) { int br = min(Q, bl + B); for (int i = bl; i < br; ++i) { if (t[i] == 1) inb[x[i]] = true; } int esz = 0; for (int i : ord) { if (!inb[i]) e[esz++] = i; } vector<int> p; for (int i = bl; i < br; ++i) { if (t[i] == 2) p.push_back(i); } sort(begin(p), end(p), [&](const int &i, const int &j) { return y[i] > y[j]; }); for (int i = 0; i < n; ++i) { dsu[i] = i; sz[i] = 1; } int eptr = 0; for (int i : p) { while (eptr < esz && c[e[eptr]] >= y[i]) { merge(a[e[eptr]], b[e[eptr]]); ++eptr; } // cout << "i " << i << endl; for (int j = i - 1; j >= bl; --j) { if (t[j] == 1 && !was[x[j]]) { if (y[j] >= y[i]) { // cout << "add1 " << j << " " << a[x[j]] << " " << b[x[j]] << " " << y[j] << endl; g[get(a[x[j]])].emplace_back(get(b[x[j]])); g[get(b[x[j]])].emplace_back(get(a[x[j]])); } was[x[j]] = true; } } for (int j = br - 1; j > i; --j) { if (t[j] == 1 && !was[x[j]]) { if (c[x[j]] >= y[i]) { // cout << "add2 " << j << " " << x[j] << " " << a[x[j]] << " " << b[x[j]] << " " << c[x[j]] << endl; g[get(a[x[j]])].emplace_back(get(b[x[j]])); g[get(b[x[j]])].emplace_back(get(a[x[j]])); } was[x[j]] = true; } } int qh = 0, qt = 0; q[qt++] = get(x[i]); used[get(x[i])] = true; while (qh < qt) { int v = q[qh++]; ans[i] += sz[v]; for (int u : g[v]) { if (!used[u]) { used[u] = true; q[qt++] = u; } } } used[get(x[i])] = false; for (int j = i - 1; j >= bl; --j) { if (t[j] == 1 && was[x[j]]) { // if (y[j] >= y[i]) { // cout << "remove1 " << j << " " << a[x[j]] << " " << b[x[j]] << " " << y[j] << endl; used[get(a[x[j]])] = false; used[get(b[x[j]])] = false; g[get(a[x[j]])].clear(); g[get(b[x[j]])].clear(); // } was[x[j]] = false; } } for (int j = br - 1; j > i; --j) { if (t[j] == 1 && was[x[j]]) { // if (c[x[j]] >= y[i]) { // cout << "remove2 " << j << " " << a[x[j]] << " " << b[x[j]] << " " << c[x[j]] << endl; used[get(a[x[j]])] = false; used[get(b[x[j]])] = false; g[get(a[x[j]])].clear(); g[get(b[x[j]])].clear(); // } was[x[j]] = false; } } } for (int i = bl; i < br; ++i) { if (t[i] == 1) { inb[x[i]] = false; c[x[i]] = y[i]; } } sort(ord.begin(), ord.end(), [&](const int &i, const int &j) { return c[i] > c[j]; }); } for (int i = 0; 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...