Submission #168378

#TimeUsernameProblemLanguageResultExecution timeMemory
168378qkxwsmBridges (APIO19_bridges)C++14
73 / 100
3029 ms8316 KiB
#include <bits/stdc++.h> using namespace std; template<class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template<class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) #define SZ(x) ((int) ((x).size())) #define ALL(x) (x).begin(), (x).end() #define MAXN 100013 #define MAGIC 880 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; int N, M, Q; pii edge[MAXN]; int weight[MAXN], tmp[MAXN]; array<int, 3> quer[MAXN]; vector<array<int, 3> > events; bitset<MAXN> mentioned; vi inds; int dsu[MAXN], siz[MAXN]; vi dos; vpi szs; int ans[MAXN]; int get(int u) { return (u == dsu[u] ? u : get(dsu[u])); } void merge(int u, int v) { u = get(u); v = get(v); if (u == v) return; if (siz[v] > siz[u]) swap(u, v); dos.PB(v); szs.PB({v, siz[v]}); szs.PB({u, siz[u]}); dsu[v] = u; siz[u] += siz[v]; siz[v] = 0; } void rollback() { reverse(ALL(dos)); reverse(ALL(szs)); for (int x : dos) { dsu[x] = x; } for (pii p : szs) { siz[p.fi] = p.se; } dos.clear(); szs.clear(); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); cerr << fixed << setprecision(4); cin >> N >> M; FOR(i, 0, M) { int u, v, d; cin >> u >> v >> d; u--; v--; if (u > v) swap(u, v); edge[i] = {u, v}; weight[i] = d; } cin >> Q; FOR(i, 0, Q) { int a, b, c; cin >> a >> b >> c; b--; if (a == 2) { quer[i] = {-1, b, c}; } else { quer[i] = {b, weight[b], c}; weight[b] = c; } } FORD(i, Q, 0) { if (quer[i][0] == -1) continue; int idx = quer[i][0]; weight[idx] = quer[i][1]; } for (int i = 0; i < Q; i += MAGIC) { FOR(j, 0, N) { dsu[j] = j; siz[j] = 1; } mentioned.reset(); inds.clear(); events.clear(); for (int j = i; j < i + MAGIC && j < Q; j++) { if (quer[j][0] == -1) continue; mentioned[quer[j][0]] = true; } FOR(j, 0, M) { if (mentioned[j]) { inds.PB(j); } else { events.PB({weight[j], 1, j}); } } for (int j = i; j < i + MAGIC && j < Q; j++) { if (quer[j][0] != -1) continue; events.PB({quer[j][2], 0, j}); } sort(ALL(events), greater<array<int, 3> >()); for (auto e : events) { int idx = e[2]; if (e[1] == 1) { merge(edge[idx].fi, edge[idx].se); } else { dos.clear(); szs.clear(); for (int x : inds) { tmp[x] = weight[x]; } FOR(j, i, idx) { if (quer[j][0] == -1) continue; tmp[quer[j][0]] = quer[j][2]; } for (int x : inds) { if (tmp[x] >= quer[idx][2]) { merge(edge[x].fi, edge[x].se); } } int u = quer[idx][1]; u = get(u); ans[idx] = siz[u]; rollback(); } } for (int j = i; j < i + MAGIC && j < Q; j++) { if (quer[j][0] == -1) continue; weight[quer[j][0]] = quer[j][2]; } } FOR(i, 0, Q) { if (quer[i][0] != -1) continue; 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...