Submission #492459

#TimeUsernameProblemLanguageResultExecution timeMemory
492459MalheirosBridges (APIO19_bridges)C++17
13 / 100
3076 ms37540 KiB
#include <bits/stdc++.h> using namespace std; #define ld long double #define ll long long #define int ll #define FF first.first #define FS first.second #define SF second.first #define SS second.second #define PB push_back #define MP make_pair #define all(cont) cont.begin(),cont.end() #define rall(cont) cont.rbegin(), cont.rend() #define FOR(i, j) for(int i=0;i<j;i++) #define RFOR(i, j) for (int i=j;i>=0;i--) #define GO cin.tie(NULL); #define FAST ios_base::sync_with_stdio(false); #define prec(x) cout << fixed << setprecision(x) #define sz(x) (int)x.size() typedef pair<int,int> pii; typedef vector<int> VI; typedef vector<pii> VPII; typedef vector<VI> VVI; typedef priority_queue<int> max_heap; typedef priority_queue<pii> max_heapii; typedef priority_queue<int,VI,greater<int>> min_heap; typedef priority_queue<pii,VPII,greater<pii>> min_heapii; const long double PI = 3.14159265359; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll randint(ll a, ll b){return uniform_int_distribution<ll>(a, b)(rng);} #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template<typename ...Args> void logger(string vars, Args&&... values) { cout << vars << " = "; string delim = ""; (..., (cout << delim << values, delim = ", ")); cout<<endl; } void print(vector<int>v){ cout<<"["; FOR(i,v.size()){ cout<<v[i]; if(i+1!=v.size())cout<<", "; } cout<<"]"<<endl; } void print(pii p){ cout<<"{"<<p.first<<", "<<p.second<<"}"<<endl; } struct DSU{ int* arr; int* size; stack<vector<pair<int,int>>> rb; int cc; DSU(int n){ cc=n; arr = new int[n]; for (int i=0;i<n;i++){ arr[i]=-1; } rb.push(vector<pair<int,int>>()); } int find(int a){ while(arr[a]>=0)a=arr[a]; return a; } void uniao(int a,int b){ int paia=find(a); int paib=find(b); if (paia==paib)return; cc--; //paib é maior, entao liga em b int tam=arr[paia]+arr[paib]; if (arr[paia]>arr[paib]){ rb.top().push_back({paib,arr[paib]}); rb.top().push_back({paia,arr[paia]}); arr[paia]=paib; arr[paib]=tam; } else{ rb.top().push_back({paib,arr[paib]}); rb.top().push_back({paia,arr[paia]}); arr[paib]=paia; arr[paia]=tam; } } void persist(){ rb.push(vector<pair<int,int>>()); } void rollback(){ auto changes=rb.top(); rb.pop(); int n=changes.size(); cc+=n/2; for (int i=n-1;i>=0;i--){ arr[changes[i].first]=changes[i].second; } } }; const int maxn=1e5+10; const int magic=800; int ord[maxn]; int ans[maxn]; int n; vector<pair<int,pii>> bridges;//{weight,{a,b}}; vector<pair<int,pii>> updates[magic];//{tempo,{qual,peso}}; vector<pair<int,pii>> queries[magic];//{peso,{tempo,qual}}; void solve(DSU& dsu,int u,pair<int,pii> q){ int it=0; set<int> mudados; for(auto k:updates[u]){ mudados.insert(k.SF); } map<int,int> mudanca; while(it<sz(updates[u]) && updates[u][it].first<q.SF){ mudanca[updates[u][it].SF]=updates[u][it].SS; it++; } for(auto k:mudados){ if (mudanca.find(k)==mudanca.end()){ if (bridges[k].first>=q.first){ //deb(bridges[k].SS,bridges[k].SF); dsu.uniao(bridges[k].SS,bridges[k].SF); } } else{ if (mudanca[k]>=q.first){ //deb(bridges[k].SS,bridges[k].SF); dsu.uniao(bridges[k].SS,bridges[k].SF); } } } //deb(ord[q.SF]); ans[ord[q.SF]]=-dsu.arr[dsu.find(q.SS)]; } void solve(int u){ DSU dsu(n); //deb(n,dsu.cc); set<int> ss; FOR(i,sz(bridges))ss.insert(i); for(auto k:updates[u]) ss.erase(k.SF); set<pii,greater<pii>> unchanged; for(auto k:ss){ unchanged.insert({bridges[k].first,k}); } sort(rall(queries[u])); auto it=unchanged.begin(); for(auto q:queries[u]){ while(it!=unchanged.end()){ if (it->first>=q.first){ //deb(bridges[it->second].SF,bridges[it->second].SS); dsu.uniao(bridges[it->second].SF,bridges[it->second].SS); it++; } else break; } //cout<<"PERSISTINDO"<<endl; dsu.persist(); solve(dsu,u,q); //cout<<"ROLLBACK"<<endl; dsu.rollback(); } for(auto k:updates[u]){ bridges[k.SF].first=k.SS; } } signed main(){ GO FAST int m;cin>>n>>m; bridges=vector<pair<int,pii>>(m); FOR(i,m){ int a,b,c;cin>>a>>b>>c;a--;b--; bridges[i]={c,{a,b}}; } int q;cin>>q; int cont=0; FOR(i,q){ int a,b,c;cin>>a>>b>>c; b--; if (a==1){//update updates[i/magic].push_back({i,{b,c}}); } else{ queries[i/magic].push_back({c,{i,b}}); ord[i]=cont++; } } FOR(i,magic)solve(i); FOR(i,cont){ cout<<ans[i]<<'\n'; } }

Compilation message (stderr)

bridges.cpp: In function 'void print(std::vector<long long int>)':
bridges.cpp:16:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 | #define FOR(i, j) for(int i=0;i<j;i++)
......
   47 |  FOR(i,v.size()){
      |      ~~~~~~~~~~                 
bridges.cpp:47:2: note: in expansion of macro 'FOR'
   47 |  FOR(i,v.size()){
      |  ^~~
bridges.cpp:49:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   if(i+1!=v.size())cout<<", ";
      |      ~~~^~~~~~~~~~
#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...