Submission #1092608

#TimeUsernameProblemLanguageResultExecution timeMemory
1092608mispertionBridges (APIO19_bridges)C++17
59 / 100
3043 ms22468 KiB
#include<bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; #define int ll typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define getlast(s) (*s.rbegin()) #define debg cout << "OK\n" const ld PI = 3.1415926535; const int N = 1e5 + 1; const int M = 50 + 1; const int mod = 1e9+7; const int infi = INT_MAX; const ll infl = LLONG_MAX; const int P = 31; int mult(int a, int b) { return a * 1LL * b % mod; } int sum(int a, int b) { if (a + b < 0) return a + b + mod; if (a + b >= mod) return a + b - mod; return a + b; } ll binpow(ll a, ll n) { if (n == 0) return 1; if (n % 2 == 1) { return binpow(a, n - 1) * a % mod; } else { ll b = binpow(a, n / 2); return b * b % mod; } } const int B = 320; int n, m, q, U[N], V[N], W[N], ans[N]; pair<int, pii> qs[N]; struct dsu{ int p[N], sz[N]; vector<pair<pii, pii>> ver; dsu(int n){ for(int i = 1; i <= n; i++) p[i] = i, sz[i] = 1; } int getp(int v){ if(v == p[v]) return v; return getp(p[v]); } void uni(int a, int b){ a = getp(a); b = getp(b); if(a == b){ ver.pb({{-1, -1}, {-1, -1}}); return; } if(sz[a] > sz[b]) swap(a, b); ver.pb({{a, p[a]}, {b, sz[b]}}); p[a] = b; sz[b] += sz[a]; } void rollback(){ if(ver.back().F.F == -1) { ver.pop_back(); return; } p[ver.back().F.F] = ver.back().F.S; sz[ver.back().S.F] = ver.back().S.S; ver.pop_back(); } }; void solve(){ cin >> n >> m; for(int i = 1; i <= m; i++){ cin >> U[i] >> V[i] >> W[i]; } cin >> q; for(int i = 0; i < q; i++) cin >> qs[i].F >> qs[i].S.F >> qs[i].S.S; for(int bl = 0; bl < q / B; bl++){ vector<int> used(m + 1, 0); vector<pair<pii, int>> qss = {}; vector<int> ch = {}; for(int j = 0; j < B; j++){ int i = bl * B + j; if(qs[i].F == 1) used[qs[i].S.F] = 1; else qss.pb({{qs[i].S.S, 1}, i}); } for(int i = 1; i <= m; i++){ if(!used[i]) qss.pb({{W[i], 2}, i}); else used[i] = 0, ch.pb(i); } sort(all(qss)); reverse(all(qss)); dsu ds = dsu(n); for(auto e : qss){ if(e.F.S == 2){ ds.uni(U[e.S], V[e.S]); continue; } int cnt = 0; int i = e.S; for(int k = i - 1; k >= bl * B; k--){ if(qs[k].F == 1){ if(!used[qs[k].S.F]){ used[qs[k].S.F] = 1; if(qs[k].S.S >= e.F.F){ cnt++; ds.uni(U[qs[k].S.F], V[qs[k].S.F]); } } } } for(auto eg : ch){ if(!used[eg]){ if(W[eg] >= e.F.F){ cnt++; ds.uni(U[eg], V[eg]); } } } for(int k = i - 1; k >= bl * B; k--){ if(qs[k].F == 1){ if(used[qs[k].S.F]){ used[qs[k].S.F] = 0; } } } ans[e.S] = ds.sz[ds.getp(qs[e.S].S.F)]; while(cnt > 0) ds.rollback(), cnt--; } for(int j = 0; j < B; j++){ int i = bl * B + j; if(qs[i].F == 1) W[qs[i].S.F] = qs[i].S.S; } } if(q % B != 0){ int bl = q / B; vector<int> used(m + 1, 0); vector<pair<pii, int>> qss = {}; vector<int> ch = {}; for(int j = 0; j < q % B; j++){ int i = bl * B + j; if(qs[i].F == 1) used[qs[i].S.F] = 1; else qss.pb({{qs[i].S.S, 1}, i}); } for(int i = 1; i <= m; i++){ if(!used[i]) qss.pb({{W[i], 2}, i}); else used[i] = 0, ch.pb(i); } sort(all(qss)); reverse(all(qss)); dsu ds = dsu(n); for(auto e : qss){ if(e.F.S == 2){ ds.uni(U[e.S], V[e.S]); continue; } int cnt = 0; int i = e.S; for(int k = i - 1; k >= bl * B; k--){ if(qs[k].F == 1){ if(!used[qs[k].S.F]){ used[qs[k].S.F] = 1; if(qs[k].S.S >= e.F.F){ cnt++; ds.uni(U[qs[k].S.F], V[qs[k].S.F]); } } } } for(auto eg : ch){ if(!used[eg]){ if(W[eg] >= e.F.F){ cnt++; ds.uni(U[eg], V[eg]); } } } for(int k = i - 1; k >= bl * B; k--){ if(qs[k].F == 1){ if(used[qs[k].S.F]){ used[qs[k].S.F] = 0; } } } ans[e.S] = ds.sz[ds.getp(qs[e.S].S.F)]; while(cnt > 0) ds.rollback(), cnt--; } } for(int i = 0; i < q; i++){ if(qs[i].F == 2) cout << ans[i] << '\n'; } } signed main() { mispertion; int t = 1; //cin >> t; while(t--){ solve(); } 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...