#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> #include <unordered_set> #include <climits> #include <list> using namespace std; using ll = long long; using ul = unsigned long long; using db = long double; using pi = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; #define mp make_pair #define pb push_back #define eb emplace_back #define x first #define y second template<class T> using V = vector<T>; template<class T, size_t SZ> using AR = array<T,SZ>; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define each(a,x) for (auto& a: x) #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define per(i,a,b) for(int i = (b) - 1; i >= (a); i--) #ifdef LOCAL template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; } auto& operator<<(auto& o, auto a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) ; #endif template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<class T> int lwb(V<T>& a, const T& b) { return int(lower_bound(all(a),b)-begin(a)); } template<class T> int upb(V<T>& a, const T& b) { return int(upper_bound(all(a),b)-begin(a)); } template<class T> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); } const int MX = 2e5+10; // https://github.com/bqi343/cp-notebook/blob/master/Implementations/content/graphs%20(12)/Advanced/LCT.h typedef struct snode* sn; struct snode { //////// VARIABLES sn p, c[2]; // parent, children bool flip = 0; // subtree flipped or not int sz; // value in node, # nodes in current splay tree snode() { p = c[0] = c[1] = NULL; calc(); } friend int getSz(sn x) { return x?x->sz:0; } void prop() { // lazy prop if (!flip) return; swap(c[0],c[1]); flip = 0; F0R(i,2) if (c[i]) c[i]->flip ^= 1; } void calc() { // recalc vals F0R(i,2) if (c[i]) c[i]->prop(); sz = 1+getSz(c[0])+getSz(c[1]); } //////// SPLAY TREE OPERATIONS int dir() { if (!p) return -2; F0R(i,2) if (p->c[i] == this) return i; return -1; // p is path-parent pointer } // -> not in current splay tree // test if root of current splay tree bool isRoot() { return dir() < 0; } friend void setLink(sn x, sn y, int d) { if (y) y->p = x; if (d >= 0) x->c[d] = y; } void rot() { // assume p and p->p propagated assert(!isRoot()); int x = dir(); sn pa = p; setLink(pa->p, this, pa->dir()); setLink(pa, c[x^1], x); setLink(this, pa, x^1); pa->calc(); } void splay() { while (!isRoot() && !p->isRoot()) { p->p->prop(), p->prop(), prop(); dir() == p->dir() ? p->rot() : rot(); rot(); } if (!isRoot()) p->prop(), prop(), rot(); prop(); calc(); } sn fbo(int b) { // find by order prop(); int z = getSz(c[0]); // of splay tree if (b == z) { splay(); return this; } return b < z ? c[0]->fbo(b) : c[1] -> fbo(b-z-1); } //////// BASE OPERATIONS void access() { // bring this to top of tree, propagate for (sn v = this, pre = NULL; v; v = v->p) { v->splay(); // now switch virtual children v->c[1] = pre; v->calc(); pre = v; } splay(); assert(!c[1]); // right subtree is empty } void makeRoot() { access(); flip ^= 1; access(); assert(!c[0] && !c[1]); } //////// QUERIES friend sn lca(sn x, sn y) { if (x == y) return x; x->access(), y->access(); if (!x->p) return NULL; x->splay(); return x->p?:x; // y was below x in latter case } // access at y did not affect x -> not connected friend bool connected(sn x, sn y) { return lca(x,y); } // # nodes above int distRoot() { access(); return getSz(c[0]); } sn getRoot() { // get root of LCT component access(); sn a = this; while (a->c[0]) a = a->c[0], a->prop(); a->access(); return a; } sn getPar(int b) { // get b-th parent on path to root access(); b = getSz(c[0])-b; assert(b >= 0); return fbo(b); } // can also get min, max on path to root, etc //////// MODIFICATIONS friend void link(sn x, sn y, bool force = 1) { assert(!connected(x,y)); if (force) y->makeRoot(); // make x par of y else { y->access(); assert(!y->c[0]); } x->access(); setLink(y,x,0); y->calc(); } friend void cut(sn y) { // cut y from its parent y->access(); assert(y->c[0]); y->c[0]->p = NULL; y->c[0] = NULL; y->calc(); } friend void cut(sn x, sn y) { // if x, y adj in tree x->makeRoot(); y->access(); assert(y->c[0] == x && !x->c[0] && !x->c[1]); cut(y); } }; sn LCT[MX]; int n, m, q; const int N = 2e5+100; const int LOG = 20; struct Query { int type; int u, v, k; ll d; void get() { cin >> type; if (type == 1) { cin >> u >> v >> d; u--; v--; } else if (type == 2) { cin >> u >> v; u--; v--; } else if (type == 3) { cin >> u >> d >> k; u--; } else { cin >> u; u--; } } }; vector<Query> queries; // BEGIN SOLVE 1 int tim = 0; struct segment_tree { int base; vpi tree; // time of update, color void init(int _n) { base = 1; while (base <= _n) base *= 2; tree.resize(2 * base); } pi query(int idx) { idx += base; pi cur = tree[idx]; idx /= 2; while (idx) { ckmax(cur, tree[idx]); idx /= 2; } return cur; } void upd(int node, int left, int right, int l, int r, pi val) { if (left > r || right < l) return; if (left >= l && right <= r) { tree[node] = val; return; } int mid = (left + right) / 2; upd(2 * node, left, mid, l, r, val); upd(2 * node + 1, mid + 1, right, l, r, val); } void upd(int l, int r, int c) { pi val = {++tim, c}; upd(1, 0, base - 1, l, r, val); } }; // Centroid based on https://github.com/bqi343/cp-notebook/blob/master/Implementations/content/graphs%20(12)/Trees%20(10)/Centroid%20(10.3).h vector<pair<int, ll>> adj[N]; bool done[N]; int sub[N]; int cen[N]; int lev[N]; ll dist[LOG][N]; segment_tree stor[N]; vl dists[N]; void ae(int a, int b, ll c) { adj[a].eb(b, c); adj[b].eb(a, c); } void dfs(int x, int p) { sub[x] = 1; for (auto [y, _] : adj[x]) { if (!done[y] && y != p) { dfs(y, x); sub[x] += sub[y]; } } } int centroid(int x) { dfs(x, -1); for (int sz = sub[x];;) { pi mx = {0, 0}; for (auto [y, _] : adj[x]) { if (!done[y] && sub[y] < sub[x]) { ckmax(mx, {sub[y], y}); } } if (mx.x * 2 <= sz) return x; x = mx.y; } assert(false); return -1; } void gen_dist(int x, int p, int level, ll c, int CEN) { // debug(x, p, level, c, CEN); dist[level][x] = dist[level][p] + c; dists[CEN].pb(dist[level][x]); for (auto [y, nc] : adj[x]) { if (!done[y] && y != p) { gen_dist(y, x, level, nc, CEN); } } } void gen(int CEN, int _x) { // CEN = centroid above x int x = centroid(_x); done[x] = 1; cen[x] = CEN; sub[x] = sub[_x]; lev[x] = (CEN == -1 ? 0 : lev[CEN] + 1); dist[lev[x]][x] = 0; stor[x].init(sub[x]); dists[x].reserve(sub[x]); dists[x].pb(0); for (auto [y, c] : adj[x]) { if (!done[y]) { gen_dist(y, x, lev[x], c, x); } } for (auto [y, c] : adj[x]) { if (!done[y]) { gen(x, y); } } assert(sz(dists[x]) == sub[x]); sort(all(dists[x])); } void init() { fill(done, done + n, false); gen(-1, 0); } void upd(int x, ll y, int c) { int cur = x; per(i,0,lev[x] + 1) { int idx = upb(dists[cur], y - dist[i][x]); if (idx > 0) { stor[cur].upd(0, idx - 1, c); } cur = cen[cur]; } } int query_centro(int x) { int cur = x; pi ans = {-1, -1}; per(i,0,lev[x] + 1) { int idx = upb(dists[cur], dist[i][x]); if (idx > 0) { ckmax(ans, stor[cur].query(idx - 1)); } cur = cen[cur]; } return ans.y; } void solve_static() { init(); for (auto qry : queries) { assert(qry.type == 3 || qry.type == 4); if (qry.type == 3) { upd(qry.u, qry.d, qry.k); } else { int res = query_centro(qry.u); cout << res << '\n'; } } } // ==== end solve static // ==== begin brut vector<set<pi>> g; vi col; void dfs_brut(int u, ll l, int k, int p = -1) { if (l < 0) return; col[u] = k; for (auto [v, d] : g[u]) { if (v == p) continue; dfs_brut(v, l - d, k, u); } } void solve_brut() { for (auto qry : queries) { if (qry.type == 1) { g[qry.u].insert({qry.v, qry.d}); g[qry.v].insert({qry.u, qry.d}); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; auto ita = g[a].lower_bound({b,-1}); auto itb = g[b].lower_bound({a,-1}); assert(ita != g[a].end()); assert(itb != g[b].end()); assert(ita->x == b); assert(itb->x == a); g[a].erase(ita); g[b].erase(itb); } else if (qry.type == 3) { dfs_brut(qry.u, qry.d, qry.k); } else { cout << col[qry.u] << '\n'; } } } // end brut // begin lct const int B = 400; bool vis[N]; list<int> G[N]; using iter = list<int>::iterator; map<pi, pair<iter, iter>> Gmap; void dfs_lct(int u, int k) { vis[u] = true; col[u] = k; for (const auto& v : G[u]) { if (vis[v]) continue; dfs_lct(v, k); } } void solve_1e15() { rep(i,0,n) LCT[i] = new snode(); rep(i,0,n) { for (auto j : G[i]) { if (j < i) continue; link(LCT[i], LCT[j]); } } vpi future_queries; int fst_query = 0; int offset = 0; while (offset < q) { int limit = 0; int cnt_2 = 0; int cnt_4 = 0; while (limit + offset < q && cnt_2 < B && cnt_4 < B) { const auto &qry = queries[limit + offset]; if (qry.type == 4) cnt_4++; else if (qry.type == 2) cnt_2++; limit++; } rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 4) { future_queries.eb(qry.u, col[qry.u]); } } rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } else if (qry.type == 3) { int u = qry.u; int k = qry.k; rep(j,fst_query,sz(future_queries)) { auto &[v, res] = future_queries[j]; if (connected(LCT[u], LCT[v])) { res = k; } } } else { fst_query++; } } for (const auto &[_, res] : future_queries) { cout << res << '\n'; } future_queries.clear(); fst_query = 0; fill(vis, vis + n, false); // set<int> added_nodes; vi added_nodes; // od końca dfs na grafie per(i,0,limit) { if (i + offset >= q) continue; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); added_nodes.pb(a); added_nodes.pb(b); } else if (qry.type == 3) { int u = qry.u; int k = qry.k; if (!vis[u]) { dfs_lct(u, k); } for (auto x : added_nodes) { if (vis[x]) continue; if (connected(LCT[x], LCT[u])) { dfs_lct(x, k); } } } } added_nodes.clear(); rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } } offset += limit; } // oof } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; g.resize(n); col.resize(n); rep(i,0,m) { int a, b, d; cin >> a >> b >> d; a--; b--; g[a].insert({b, d}); g[b].insert({a, d}); if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); ae(a, b, d); // static solve } bool any_edge_changes = false; bool all_z_1e15 = true; constexpr ll MAXZ = 1e15; queries.resize(q); rep(i,0,q) { queries[i].get(); if (queries[i].type == 1 || queries[i].type == 2) { any_edge_changes = true; } if (queries[i].type == 3 && queries[i].d < MAXZ) { all_z_1e15 = false; } } if (!any_edge_changes && m == n - 1) { debug("solve static"); solve_static(); return 0; } if (all_z_1e15) { debug("solve 1e15"); solve_1e15(); return 0; } debug("solve brut"); solve_brut(); return 0; }
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> #include <unordered_set> #include <climits> #include <list> using namespace std; using ll = long long; using ul = unsigned long long; using db = long double; using pi = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; #define mp make_pair #define pb push_back #define eb emplace_back #define x first #define y second template<class T> using V = vector<T>; template<class T, size_t SZ> using AR = array<T,SZ>; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define each(a,x) for (auto& a: x) #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define per(i,a,b) for(int i = (b) - 1; i >= (a); i--) #ifdef LOCAL template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; } auto& operator<<(auto& o, auto a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) ; #endif template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<class T> int lwb(V<T>& a, const T& b) { return int(lower_bound(all(a),b)-begin(a)); } template<class T> int upb(V<T>& a, const T& b) { return int(upper_bound(all(a),b)-begin(a)); } template<class T> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); } const int MX = 2e5+10; // https://github.com/bqi343/cp-notebook/blob/master/Implementations/content/graphs%20(12)/Advanced/LCT.h typedef struct snode* sn; struct snode { //////// VARIABLES sn p, c[2]; // parent, children bool flip = 0; // subtree flipped or not int sz; // value in node, # nodes in current splay tree snode() { p = c[0] = c[1] = NULL; calc(); } friend int getSz(sn x) { return x?x->sz:0; } void prop() { // lazy prop if (!flip) return; swap(c[0],c[1]); flip = 0; F0R(i,2) if (c[i]) c[i]->flip ^= 1; } void calc() { // recalc vals F0R(i,2) if (c[i]) c[i]->prop(); sz = 1+getSz(c[0])+getSz(c[1]); } //////// SPLAY TREE OPERATIONS int dir() { if (!p) return -2; F0R(i,2) if (p->c[i] == this) return i; return -1; // p is path-parent pointer } // -> not in current splay tree // test if root of current splay tree bool isRoot() { return dir() < 0; } friend void setLink(sn x, sn y, int d) { if (y) y->p = x; if (d >= 0) x->c[d] = y; } void rot() { // assume p and p->p propagated assert(!isRoot()); int x = dir(); sn pa = p; setLink(pa->p, this, pa->dir()); setLink(pa, c[x^1], x); setLink(this, pa, x^1); pa->calc(); } void splay() { while (!isRoot() && !p->isRoot()) { p->p->prop(), p->prop(), prop(); dir() == p->dir() ? p->rot() : rot(); rot(); } if (!isRoot()) p->prop(), prop(), rot(); prop(); calc(); } sn fbo(int b) { // find by order prop(); int z = getSz(c[0]); // of splay tree if (b == z) { splay(); return this; } return b < z ? c[0]->fbo(b) : c[1] -> fbo(b-z-1); } //////// BASE OPERATIONS void access() { // bring this to top of tree, propagate for (sn v = this, pre = NULL; v; v = v->p) { v->splay(); // now switch virtual children v->c[1] = pre; v->calc(); pre = v; } splay(); assert(!c[1]); // right subtree is empty } void makeRoot() { access(); flip ^= 1; access(); assert(!c[0] && !c[1]); } //////// QUERIES friend sn lca(sn x, sn y) { if (x == y) return x; x->access(), y->access(); if (!x->p) return NULL; x->splay(); return x->p?:x; // y was below x in latter case } // access at y did not affect x -> not connected friend bool connected(sn x, sn y) { return lca(x,y); } // # nodes above int distRoot() { access(); return getSz(c[0]); } sn getRoot() { // get root of LCT component access(); sn a = this; while (a->c[0]) a = a->c[0], a->prop(); a->access(); return a; } sn getPar(int b) { // get b-th parent on path to root access(); b = getSz(c[0])-b; assert(b >= 0); return fbo(b); } // can also get min, max on path to root, etc //////// MODIFICATIONS friend void link(sn x, sn y, bool force = 1) { assert(!connected(x,y)); if (force) y->makeRoot(); // make x par of y else { y->access(); assert(!y->c[0]); } x->access(); setLink(y,x,0); y->calc(); } friend void cut(sn y) { // cut y from its parent y->access(); assert(y->c[0]); y->c[0]->p = NULL; y->c[0] = NULL; y->calc(); } friend void cut(sn x, sn y) { // if x, y adj in tree x->makeRoot(); y->access(); assert(y->c[0] == x && !x->c[0] && !x->c[1]); cut(y); } }; sn LCT[MX]; int n, m, q; const int N = 2e5+100; const int LOG = 20; struct Query { int type; int u, v, k; ll d; void get() { cin >> type; if (type == 1) { cin >> u >> v >> d; u--; v--; } else if (type == 2) { cin >> u >> v; u--; v--; } else if (type == 3) { cin >> u >> d >> k; u--; } else { cin >> u; u--; } } }; vector<Query> queries; // BEGIN SOLVE 1 int tim = 0; struct segment_tree { int base; vpi tree; // time of update, color void init(int _n) { base = 1; while (base <= _n) base *= 2; tree.resize(2 * base); } pi query(int idx) { idx += base; pi cur = tree[idx]; idx /= 2; while (idx) { ckmax(cur, tree[idx]); idx /= 2; } return cur; } void upd(int node, int left, int right, int l, int r, pi val) { if (left > r || right < l) return; if (left >= l && right <= r) { tree[node] = val; return; } int mid = (left + right) / 2; upd(2 * node, left, mid, l, r, val); upd(2 * node + 1, mid + 1, right, l, r, val); } void upd(int l, int r, int c) { pi val = {++tim, c}; upd(1, 0, base - 1, l, r, val); } }; // Centroid based on https://github.com/bqi343/cp-notebook/blob/master/Implementations/content/graphs%20(12)/Trees%20(10)/Centroid%20(10.3).h vector<pair<int, ll>> adj[N]; bool done[N]; int sub[N]; int cen[N]; int lev[N]; ll dist[LOG][N]; segment_tree stor[N]; vl dists[N]; void ae(int a, int b, ll c) { adj[a].eb(b, c); adj[b].eb(a, c); } void dfs(int x, int p) { sub[x] = 1; for (auto [y, _] : adj[x]) { if (!done[y] && y != p) { dfs(y, x); sub[x] += sub[y]; } } } int centroid(int x) { dfs(x, -1); for (int sz = sub[x];;) { pi mx = {0, 0}; for (auto [y, _] : adj[x]) { if (!done[y] && sub[y] < sub[x]) { ckmax(mx, {sub[y], y}); } } if (mx.x * 2 <= sz) return x; x = mx.y; } assert(false); return -1; } void gen_dist(int x, int p, int level, ll c, int CEN) { // debug(x, p, level, c, CEN); dist[level][x] = dist[level][p] + c; dists[CEN].pb(dist[level][x]); for (auto [y, nc] : adj[x]) { if (!done[y] && y != p) { gen_dist(y, x, level, nc, CEN); } } } void gen(int CEN, int _x) { // CEN = centroid above x int x = centroid(_x); done[x] = 1; cen[x] = CEN; sub[x] = sub[_x]; lev[x] = (CEN == -1 ? 0 : lev[CEN] + 1); dist[lev[x]][x] = 0; stor[x].init(sub[x]); dists[x].reserve(sub[x]); dists[x].pb(0); for (auto [y, c] : adj[x]) { if (!done[y]) { gen_dist(y, x, lev[x], c, x); } } for (auto [y, c] : adj[x]) { if (!done[y]) { gen(x, y); } } assert(sz(dists[x]) == sub[x]); sort(all(dists[x])); } void init() { fill(done, done + n, false); gen(-1, 0); } void upd(int x, ll y, int c) { int cur = x; per(i,0,lev[x] + 1) { int idx = upb(dists[cur], y - dist[i][x]); if (idx > 0) { stor[cur].upd(0, idx - 1, c); } cur = cen[cur]; } } int query_centro(int x) { int cur = x; pi ans = {-1, -1}; per(i,0,lev[x] + 1) { int idx = upb(dists[cur], dist[i][x]); if (idx > 0) { ckmax(ans, stor[cur].query(idx - 1)); } cur = cen[cur]; } return ans.y; } void solve_static() { init(); for (auto qry : queries) { assert(qry.type == 3 || qry.type == 4); if (qry.type == 3) { upd(qry.u, qry.d, qry.k); } else { int res = query_centro(qry.u); cout << res << '\n'; } } } // ==== end solve static // ==== begin brut vector<set<pi>> g; vi col; void dfs_brut(int u, ll l, int k, int p = -1) { if (l < 0) return; col[u] = k; for (auto [v, d] : g[u]) { if (v == p) continue; dfs_brut(v, l - d, k, u); } } void solve_brut() { for (auto qry : queries) { if (qry.type == 1) { g[qry.u].insert({qry.v, qry.d}); g[qry.v].insert({qry.u, qry.d}); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; auto ita = g[a].lower_bound({b,-1}); auto itb = g[b].lower_bound({a,-1}); assert(ita != g[a].end()); assert(itb != g[b].end()); assert(ita->x == b); assert(itb->x == a); g[a].erase(ita); g[b].erase(itb); } else if (qry.type == 3) { dfs_brut(qry.u, qry.d, qry.k); } else { cout << col[qry.u] << '\n'; } } } // end brut // begin lct const int B = 400; bool vis[N]; list<int> G[N]; using iter = list<int>::iterator; map<pi, pair<iter, iter>> Gmap; void dfs_lct(int u, int k) { vis[u] = true; col[u] = k; for (const auto& v : G[u]) { if (vis[v]) continue; dfs_lct(v, k); } } void solve_1e15() { rep(i,0,n) LCT[i] = new snode(); rep(i,0,n) { for (auto j : G[i]) { if (j < i) continue; link(LCT[i], LCT[j]); } } vpi future_queries; int fst_query = 0; int offset = 0; while (offset < q) { int limit = 0; int cnt_2 = 0; int cnt_4 = 0; while (limit + offset < q && cnt_2 < B && cnt_4 < B) { const auto &qry = queries[limit + offset]; if (qry.type == 4) cnt_4++; else if (qry.type == 2) cnt_2++; limit++; } rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 4) { future_queries.eb(qry.u, col[qry.u]); } } rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } else if (qry.type == 3) { int u = qry.u; int k = qry.k; rep(j,fst_query,sz(future_queries)) { auto &[v, res] = future_queries[j]; if (connected(LCT[u], LCT[v])) { res = k; } } } else { fst_query++; } } for (const auto &[_, res] : future_queries) { cout << res << '\n'; } future_queries.clear(); fst_query = 0; fill(vis, vis + n, false); // set<int> added_nodes; vi added_nodes; // od końca dfs na grafie per(i,0,limit) { if (i + offset >= q) continue; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); added_nodes.pb(a); added_nodes.pb(b); } else if (qry.type == 3) { int u = qry.u; int k = qry.k; if (!vis[u]) { dfs_lct(u, k); } for (auto x : added_nodes) { if (vis[x]) continue; if (connected(LCT[x], LCT[u])) { dfs_lct(x, k); } } } } added_nodes.clear(); rep(i,0,limit) { if (i + offset >= q) break; const auto &qry = queries[i + offset]; if (qry.type == 1) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); link(LCT[a], LCT[b]); } else if (qry.type == 2) { int a = qry.u; int b = qry.v; if (a > b) swap(a, b); // G[a].erase(b); // G[b].erase(a); auto [it1, it2] = Gmap[mp(a, b)]; G[a].erase(it1); G[b].erase(it2); cut(LCT[a], LCT[b]); } } offset += limit; } // oof } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; g.resize(n); col.resize(n); rep(i,0,m) { int a, b, d; cin >> a >> b >> d; a--; b--; g[a].insert({b, d}); g[b].insert({a, d}); if (a > b) swap(a, b); // G[a].insert(b); // G[b].insert(a); G[a].push_back(b); G[b].push_back(a); Gmap[mp(a, b)] = mp(prev(G[a].end()), prev(G[b].end())); ae(a, b, d); // static solve } bool any_edge_changes = false; bool all_z_1e15 = true; constexpr ll MAXZ = 1e15; queries.resize(q); rep(i,0,q) { queries[i].get(); if (queries[i].type == 1 || queries[i].type == 2) { any_edge_changes = true; } if (queries[i].type == 3 && queries[i].d < MAXZ) { all_z_1e15 = false; } } if (!any_edge_changes && m == n - 1) { debug("solve static"); solve_static(); return 0; } if (all_z_1e15) { debug("solve 1e15"); solve_1e15(); return 0; } debug("solve brut"); solve_brut(); return 0; } |