Submission #540315

#TimeUsernameProblemLanguageResultExecution timeMemory
540315LoboBridges (APIO19_bridges)C++17
100 / 100
2786 ms16324 KiB
#include<bits/stdc++.h> using namespace std; const long long inf = (long long) 1e18 + 10; const int inf1 = (int) 1e9 + 10; // #define int long long #define dbl long double #define endl '\n' #define sc second #define fr first #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() const int maxn = 5e4 + 10; const int B = 800; int n, m, q, ds[maxn], dsz[maxn], mark[maxn], a[2*maxn], b[2*maxn], d[2*maxn], ans[maxn]; int chg[2*maxn]; vector<pair<int,int>> g1[maxn], atts[2*maxn]; int find(int u) { if(ds[u] == u) return u; return find(ds[u]); } void join(int u, int v) { if(dsz[u] < dsz[v]) swap(u,v); dsz[u]+= dsz[v]; ds[v] = u; } stack<pair<int,int>> st; void rollback() { int u = st.top().fr; int v = st.top().sc; st.pop(); if(dsz[u] < dsz[v]) swap(u,v); ds[v] = v; dsz[u]-=dsz[v]; } void solve() { cin >> n >> m; for(int i = 1; i <= m; i++) { cin >> a[i] >> b[i] >> d[i]; } int q; cin >> q; for(int j = 1; j <= q; j+=B) { int Bq = min(B,q-j+1); for(int i = 1; i <= n; i++) { g1[i].clear(); } vector<pair<pair<int,int>,int>> qrr; for(int i = 1; i <= m; i++) { atts[i].clear(); atts[i].pb(mp(0,d[i])); } for(int i = 1; i <= Bq; i++) { int op; cin >> op; if(op == 1) { int id,val; cin >> id >> val; chg[id] = j; atts[id].pb(mp(i,val)); ans[i] = -1; } else { int id,val; cin >> id >> val; qrr.pb(mp(mp(val,-id),i)); ans[i] = 0; } } //coloco as arestas nao mudadas e as queries juntas e ordedo da maior pra menor //quando responto query com peso x ja adicionei todas as arestas nao mudadadas d[i] > x //para cada query eu passo por todas as att e mudo / adiciono do jeito certo vector<int> edgch; for(int i = 1; i <= m; i++) { if(chg[i] != j) { qrr.pb(mp(mp(d[i],i),0)); } else { edgch.pb(i); } } sort(all(qrr),greater<pair<pair<int,int>,int>>()); for(int i = 1; i <= n; i++) { ds[i] = i; dsz[i] = 1; mark[i] = 0; } for(auto X : qrr) { if(X.fr.sc > 0) { //adicionar uma aresta int id = X.fr.sc; int u = a[id]; int v = b[id]; // cout << " " << id << endl; u = find(u); v = find(v); if(u != v) join(u,v); } else { //sai de u com val int val = X.fr.fr; int u = -X.fr.sc; int idq = X.sc; // cout << " " << u << " " << val << " " << idq << endl; int cntrll = 0; for(auto id : edgch) { //com upperbound auto it = upper_bound(all(atts[id]),mp(idq,0)); it--; if(it->sc >= val) { //adiciona id int v1 = find(a[id]); int v2 = find(b[id]); if(v1 != v2) { st.push(mp(v1,v2)); join(v1,v2); cntrll++; } } } ans[idq] = dsz[find(u)]; while(cntrll--) rollback(); } } for(auto id : edgch) { d[id] = atts[id].back().sc; } for(int i = 1; i <= Bq; i++) { if(ans[i] != -1) cout << ans[i] << endl; } } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); int tt = 1; // cin >> tt; while(tt--) solve(); }
#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...