#pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; ll ops=0; template<typename V, bool amortized> struct Tree { typedef pair<int,V> E; vector<vector<E>> adj; vector<vector<E>> toadd,torem; Tree(){} Tree(int n) : adj(n) { if constexpr (amortized) { toadd.resize(n); torem.resize(n); } } void addE(int u, int v, V w) { if constexpr (!amortized) { ops+=adj[u].size(); auto it = lower_bound(all(adj[u]),E{v,w}); adj[u].insert(it,{v,w}); it = lower_bound(all(adj[v]),E{u,w}); adj[v].insert(it,{u,w}); } else { ops++; toadd[u].push_back({v,w}); toadd[v].push_back({u,w}); } } vector<E>& operator[](int i) { return adj[i]; } V weight(int u,int v) const { auto it = lower_bound(all(adj[u]),E{v,-1}); ops++; if(it!=adj[u].end() and it->first==v) { return it->second; } return -1; } void remE(int u, int v) { auto it = lower_bound(all(adj[u]),E{v,-1}); if constexpr (amortized) torem[u].push_back(*it); else adj[u].erase(it), ops+=adj[u].size(); it = lower_bound(all(adj[v]),E{u,-1}); if constexpr (amortized) torem[v].push_back(*it); else adj[v].erase(it),ops+=adj[v].size(); } void amortizerebuild() { if constexpr (!amortized) return; int n = adj.size(); for(int i=0;i<n;++i) { vector<E> nw; assert(torem[i].empty() or toadd[i].empty()); if(!toadd[i].empty()) { ops+=adj[i].size()+torem[i].size()+toadd[i].size(); sort(all(toadd[i])); nw.reserve(adj[i].size()+toadd[i].size()); merge(all(adj[i]),all(toadd[i]),back_inserter(nw)); swap(adj[i],nw); } else if(!torem[i].empty()) { ops+=adj[i].size()+torem[i].size()+toadd[i].size(); nw.reserve(adj[i].size()); sort(all(torem[i])); set_difference(all(adj[i]),all(torem[i]), back_inserter(nw)); swap(adj[i],nw); } } torem.assign(n,{}); toadd.assign(n,{}); } }; vi virtid; struct U { array<int,2> col; ll z; }; vector<vector<U> > upds; struct virtualtree { // gives weighted subtree that connects all the nodes in vv in O(|vv| log(n)) Tree<ll,false> adj; vi v; void addU(int root,array<int,2> col,ll z) { auto dfs = [&](auto&& self, int at, int from) -> void { ops++; upds[at].push_back({col,z}); for(auto [to,w] : adj[at]) if(to!=from) { z-=w; if(z>=0) self(self,to,at); z+=w; } }; dfs(dfs,root,-1); } virtualtree(vi& vv) : adj(vv.size()),v(vv) { for(int i=0;i<int(v.size());++i) { virtid[v[i]]=i; } } virtualtree(){} } vt; struct LCA { vi par,myspec; vector<ll> dep; Tree<int,true> adj; int root=0; LCA(int n) : par(n),myspec(n,-1),dep(n),adj(n) {} void addE(int u, int v, int w) { adj.addE(u,v,w); } void remE(int u,int v) { adj.remE(u,v); } int cnt=0; vi st; void dfs(int s) { st.clear(); st.push_back(s); for(int i=0;i<st.size();++i) { int at = st[i]; myspec[at]=virtid[at]; for(auto [to,w] : adj[at]) if(to!=par[at]) { par[to]=at; dep[to]=dep[at]+w; st.push_back(to); } } for(int i=st.size()-1;i>=0;--i) { int at= st[i]; int& mine = myspec[at]; for(auto [to,w] : adj[at]) if(to!=par[at]) { int ch = myspec[to]; if(ch!=-1) { if(mine==-1) { mine =ch; } else { if(virtid[at]==-1) { virtid[at]=vt.v.size(); vt.v.push_back(at); vt.adj.adj.push_back({}); vt.adj.addE(virtid[at],mine, dep[vt.v[mine]]-dep[at]); mine = virtid[at]; } vt.adj.addE(virtid[at],ch,dep[vt.v[ch]]-dep[at]); } } } } } void init() { cnt=0; fill(all(dep),-1); for(int i=0;i<(int)dep.size();++i)if(dep[i]==-1) { dep[i]=0; par[i]=i; dfs(i); } } }; std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);} vector<bool> on; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto t0 = chrono::steady_clock::now(); int n=2e5,m=2e5-1,q=2e5; cin >> n >> m >> q; LCA lca(n); virtid.resize(n,-1); on.resize(n,1); for(int i=0;i<m;++i) { int u=i+1,v=i+2,w=rnd(1,int(1e9)); cin >> u >> v >> w; --u,--v; lca.addE(u,v,w); } lca.adj.amortizerebuild(); struct Q { int type; int u,v,d; ll z; void read() { type = rnd(1,4); cin >> type; if(type==1) { d = rnd(1,oo); cin >> u >> v >> d; --u,--v; if(u>v) swap(u,v); } else if(type==2) { cin >> u >> v; --u,--v; if(u>v) swap(u,v); } else if(type==3) { cin >> u >> z >> d; --u; } else { cin >> u; --u; } } }; vector<Q> qs(q); vector<array<int,2>> cols(n,{-1,0}); for(auto& myq : qs) { myq.read(); } // O(n*(q/B) + B*q) // sqrt(n)*q // const int B = ceil(pow(double(n)*4,0.5)); ll ops2=0; for(int l=0,r;l<q;l=r) { r = q; vi spec; vector<array<int,3>> rollback; const int LIM = 80*n ; int ans=0; int updates=0; set<int> specs; for(int i=l;i<q;++i) { if(ans+specs.size()*updates>LIM) { r=i; break; } auto& myq = qs[i]; spec.push_back(myq.u); specs.insert(myq.u); if(myq.type<=2) { spec.push_back(myq.v); specs.insert(myq.v); updates++; // does an insertion or deletion inside that. } if(myq.type==2) { auto w = lca.adj.weight(myq.u,myq.v); if(w!=-1) { rollback.push_back({myq.u,myq.v,w}); } } if(myq.type==3) { updates+=4; } } sort(all(spec)); spec.erase(unique(all(spec)),spec.end()); sort(all(rollback)); rollback.erase(unique(all(rollback)),rollback.end()); for(auto [u,v,w] : rollback) { lca.remE(u,v); } lca.adj.amortizerebuild(); vt = virtualtree(spec); lca.init(); // initialize tree with all special edges removed. upds.assign(vt.v.size(),{}); set<array<int,3>> toadd; auto addVT = [&](int u, int v, int w) { vt.adj.addE(virtid[u],virtid[v],w); toadd.insert({u,v,w}); }; auto delVT = [&](int u, int v) { if(u>v) swap(u,v); auto it = toadd.lower_bound(array<int,3>{u,v,-1}); assert(it!=toadd.end()); toadd.erase(it); vt.adj.remE(virtid[u],virtid[v]); }; for(auto [u,v,w] : rollback) { addVT(u,v,w); } // answer queries. for(int i=l;i<r;++i) { auto& myq = qs[i]; if(myq.type==1) { addVT(myq.u,myq.v,myq.d); } else if(myq.type==2) { delVT(myq.u,myq.v); } else if(myq.type==3) { // do DFS in virtual tree, and add it to all of those. vt.addU(virtid[myq.u],{i,myq.d},myq.z); } else { auto res = cols[myq.u]; auto& myu = upds[virtid[myq.u]]; if(!myu.empty()) { res = max(res,myu.back().col); } cout << res[1] << '\n'; } } // rebuild the data structure vector<pi> st; st.reserve(n); for(int i=0;i<(int)upds.size();++i) { if(!upds[i].empty()) { vector<U>& us = upds[i]; int k = us.size(); { vector<U> nus; nus.reserve(us.size()); ll maxz = -1; for(int i=k-1;i>=0;--i) { ops2++; if(us[i].z>maxz) { maxz=us[i].z; nus.push_back(us[i]); } } us=nus; k=us.size(); } st = {{vt.v[i],0}}; lca.par[vt.v[i]]=-1; lca.dep[vt.v[i]]=0; while(!st.empty()) { auto [at,id] = st.back(); st.pop_back(); int lo =id,hi = k; ++ops; auto d = lca.dep[at]; while(lo<hi) { ++ops2; auto mid = (lo+hi)/2; if(us[mid].z>=d) { hi = mid; } else lo = mid+1; } if(lo==k) continue; cols[at]=max(cols[at],us[lo].col); int from = lca.par[at]; for(auto [to,w] : lca.adj[at]) if(to!=from and virtid[to]==-1) { ++ops; lca.dep[to]=d+w; lca.par[to]=at; st.push_back({to,lo}); } } } } // need to bring lca back to original state! for(auto [u,v,w] : toadd) { lca.addE(u,v,w); } lca.adj.amortizerebuild(); for(auto& u : vt.v) { virtid[u]=-1; } } cerr << ops << endl; cerr << ops2 << endl; auto t1= chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " seconds\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | #pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; ll ops=0; template<typename V, bool amortized> struct Tree { typedef pair<int,V> E; vector<vector<E>> adj; vector<vector<E>> toadd,torem; Tree(){} Tree(int n) : adj(n) { if constexpr (amortized) { toadd.resize(n); torem.resize(n); } } void addE(int u, int v, V w) { if constexpr (!amortized) { ops+=adj[u].size(); auto it = lower_bound(all(adj[u]),E{v,w}); adj[u].insert(it,{v,w}); it = lower_bound(all(adj[v]),E{u,w}); adj[v].insert(it,{u,w}); } else { ops++; toadd[u].push_back({v,w}); toadd[v].push_back({u,w}); } } vector<E>& operator[](int i) { return adj[i]; } V weight(int u,int v) const { auto it = lower_bound(all(adj[u]),E{v,-1}); ops++; if(it!=adj[u].end() and it->first==v) { return it->second; } return -1; } void remE(int u, int v) { auto it = lower_bound(all(adj[u]),E{v,-1}); if constexpr (amortized) torem[u].push_back(*it); else adj[u].erase(it), ops+=adj[u].size(); it = lower_bound(all(adj[v]),E{u,-1}); if constexpr (amortized) torem[v].push_back(*it); else adj[v].erase(it),ops+=adj[v].size(); } void amortizerebuild() { if constexpr (!amortized) return; int n = adj.size(); for(int i=0;i<n;++i) { vector<E> nw; assert(torem[i].empty() or toadd[i].empty()); if(!toadd[i].empty()) { ops+=adj[i].size()+torem[i].size()+toadd[i].size(); sort(all(toadd[i])); nw.reserve(adj[i].size()+toadd[i].size()); merge(all(adj[i]),all(toadd[i]),back_inserter(nw)); swap(adj[i],nw); } else if(!torem[i].empty()) { ops+=adj[i].size()+torem[i].size()+toadd[i].size(); nw.reserve(adj[i].size()); sort(all(torem[i])); set_difference(all(adj[i]),all(torem[i]), back_inserter(nw)); swap(adj[i],nw); } } torem.assign(n,{}); toadd.assign(n,{}); } }; vi virtid; struct U { array<int,2> col; ll z; }; vector<vector<U> > upds; struct virtualtree { // gives weighted subtree that connects all the nodes in vv in O(|vv| log(n)) Tree<ll,false> adj; vi v; void addU(int root,array<int,2> col,ll z) { auto dfs = [&](auto&& self, int at, int from) -> void { ops++; upds[at].push_back({col,z}); for(auto [to,w] : adj[at]) if(to!=from) { z-=w; if(z>=0) self(self,to,at); z+=w; } }; dfs(dfs,root,-1); } virtualtree(vi& vv) : adj(vv.size()),v(vv) { for(int i=0;i<int(v.size());++i) { virtid[v[i]]=i; } } virtualtree(){} } vt; struct LCA { vi par,myspec; vector<ll> dep; Tree<int,true> adj; int root=0; LCA(int n) : par(n),myspec(n,-1),dep(n),adj(n) {} void addE(int u, int v, int w) { adj.addE(u,v,w); } void remE(int u,int v) { adj.remE(u,v); } int cnt=0; vi st; void dfs(int s) { st.clear(); st.push_back(s); for(int i=0;i<st.size();++i) { int at = st[i]; myspec[at]=virtid[at]; for(auto [to,w] : adj[at]) if(to!=par[at]) { par[to]=at; dep[to]=dep[at]+w; st.push_back(to); } } for(int i=st.size()-1;i>=0;--i) { int at= st[i]; int& mine = myspec[at]; for(auto [to,w] : adj[at]) if(to!=par[at]) { int ch = myspec[to]; if(ch!=-1) { if(mine==-1) { mine =ch; } else { if(virtid[at]==-1) { virtid[at]=vt.v.size(); vt.v.push_back(at); vt.adj.adj.push_back({}); vt.adj.addE(virtid[at],mine, dep[vt.v[mine]]-dep[at]); mine = virtid[at]; } vt.adj.addE(virtid[at],ch,dep[vt.v[ch]]-dep[at]); } } } } } void init() { cnt=0; fill(all(dep),-1); for(int i=0;i<(int)dep.size();++i)if(dep[i]==-1) { dep[i]=0; par[i]=i; dfs(i); } } }; std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);} vector<bool> on; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto t0 = chrono::steady_clock::now(); int n=2e5,m=2e5-1,q=2e5; cin >> n >> m >> q; LCA lca(n); virtid.resize(n,-1); on.resize(n,1); for(int i=0;i<m;++i) { int u=i+1,v=i+2,w=rnd(1,int(1e9)); cin >> u >> v >> w; --u,--v; lca.addE(u,v,w); } lca.adj.amortizerebuild(); struct Q { int type; int u,v,d; ll z; void read() { type = rnd(1,4); cin >> type; if(type==1) { d = rnd(1,oo); cin >> u >> v >> d; --u,--v; if(u>v) swap(u,v); } else if(type==2) { cin >> u >> v; --u,--v; if(u>v) swap(u,v); } else if(type==3) { cin >> u >> z >> d; --u; } else { cin >> u; --u; } } }; vector<Q> qs(q); vector<array<int,2>> cols(n,{-1,0}); for(auto& myq : qs) { myq.read(); } // O(n*(q/B) + B*q) // sqrt(n)*q // const int B = ceil(pow(double(n)*4,0.5)); ll ops2=0; for(int l=0,r;l<q;l=r) { r = q; vi spec; vector<array<int,3>> rollback; const int LIM = 80*n ; int ans=0; int updates=0; set<int> specs; for(int i=l;i<q;++i) { if(ans+specs.size()*updates>LIM) { r=i; break; } auto& myq = qs[i]; spec.push_back(myq.u); specs.insert(myq.u); if(myq.type<=2) { spec.push_back(myq.v); specs.insert(myq.v); updates++; // does an insertion or deletion inside that. } if(myq.type==2) { auto w = lca.adj.weight(myq.u,myq.v); if(w!=-1) { rollback.push_back({myq.u,myq.v,w}); } } if(myq.type==3) { updates+=4; } } sort(all(spec)); spec.erase(unique(all(spec)),spec.end()); sort(all(rollback)); rollback.erase(unique(all(rollback)),rollback.end()); for(auto [u,v,w] : rollback) { lca.remE(u,v); } lca.adj.amortizerebuild(); vt = virtualtree(spec); lca.init(); // initialize tree with all special edges removed. upds.assign(vt.v.size(),{}); set<array<int,3>> toadd; auto addVT = [&](int u, int v, int w) { vt.adj.addE(virtid[u],virtid[v],w); toadd.insert({u,v,w}); }; auto delVT = [&](int u, int v) { if(u>v) swap(u,v); auto it = toadd.lower_bound(array<int,3>{u,v,-1}); assert(it!=toadd.end()); toadd.erase(it); vt.adj.remE(virtid[u],virtid[v]); }; for(auto [u,v,w] : rollback) { addVT(u,v,w); } // answer queries. for(int i=l;i<r;++i) { auto& myq = qs[i]; if(myq.type==1) { addVT(myq.u,myq.v,myq.d); } else if(myq.type==2) { delVT(myq.u,myq.v); } else if(myq.type==3) { // do DFS in virtual tree, and add it to all of those. vt.addU(virtid[myq.u],{i,myq.d},myq.z); } else { auto res = cols[myq.u]; auto& myu = upds[virtid[myq.u]]; if(!myu.empty()) { res = max(res,myu.back().col); } cout << res[1] << '\n'; } } // rebuild the data structure vector<pi> st; st.reserve(n); for(int i=0;i<(int)upds.size();++i) { if(!upds[i].empty()) { vector<U>& us = upds[i]; int k = us.size(); { vector<U> nus; nus.reserve(us.size()); ll maxz = -1; for(int i=k-1;i>=0;--i) { ops2++; if(us[i].z>maxz) { maxz=us[i].z; nus.push_back(us[i]); } } us=nus; k=us.size(); } st = {{vt.v[i],0}}; lca.par[vt.v[i]]=-1; lca.dep[vt.v[i]]=0; while(!st.empty()) { auto [at,id] = st.back(); st.pop_back(); int lo =id,hi = k; ++ops; auto d = lca.dep[at]; while(lo<hi) { ++ops2; auto mid = (lo+hi)/2; if(us[mid].z>=d) { hi = mid; } else lo = mid+1; } if(lo==k) continue; cols[at]=max(cols[at],us[lo].col); int from = lca.par[at]; for(auto [to,w] : lca.adj[at]) if(to!=from and virtid[to]==-1) { ++ops; lca.dep[to]=d+w; lca.par[to]=at; st.push_back({to,lo}); } } } } // need to bring lca back to original state! for(auto [u,v,w] : toadd) { lca.addE(u,v,w); } lca.adj.amortizerebuild(); for(auto& u : vt.v) { virtid[u]=-1; } } cerr << ops << endl; cerr << ops2 << endl; auto t1= chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " seconds\n"; } |