#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 2e5 + 5; vector<pii> adj[nax]; int n, m, q; int timer = 0; int pre[nax]; int post[nax]; const int LOG = 20; int up[nax][LOG]; int prefDist[nax]; void dfs(int v, int p, int cost){ up[v][0] = p; for(int i=1;i<LOG;i++){ int kt = up[v][i - 1]; up[v][i] = up[kt][i - 1]; } pre[v] = ++timer; prefDist[v] = prefDist[p] + cost; for(pii go : adj[v]){ if(go.st != p) dfs(go.st, v, go.nd); } post[v] = ++timer; } bool anc(int x, int y){ return (pre[x] <= pre[y] && post[x] >= post[y]); } int lca(int x, int y){ if(anc(x, y)) return x; if(anc(y, x)) return y; for(int i=LOG-1;i>=0;i--){ if(!anc(up[x][i], y)) x = up[x][i]; } return up[x][0]; } int dist(int x, int y){ int d = lca(x, y); return prefDist[x] + prefDist[y] - prefDist[d] * 2; } bool blocked[nax]; int par[nax]; int sz[nax]; void recalc_sz(int v, int p){ sz[v] = 1; for(pii go : adj[v]){ int kt = go.st; if(!blocked[kt] && kt != p){ recalc_sz(kt, v); sz[v] += sz[kt]; } } } int get_centroid(int v, int p, int total){ for(pii go : adj[v]){ int kt = go.st; if(blocked[kt] || kt == p) continue; if(sz[kt] > total / 2) return get_centroid(kt, v, total); } return v; } void calc_centroid(int v, int p){ recalc_sz(v, p); int w = get_centroid(v, v, sz[v]); blocked[w] = true; par[w] = p; for(pii go : adj[w]){ int kt = go.st; if(!blocked[kt]){ calc_centroid(kt, w); } } } vector<tuple<int, int, int> > colors[nax]; // promien, kolor, czas void pu(int who, int col, int ray, int tm){ while(colors[who].size()){ auto [r, c, t] = colors[who].back(); if(r <= ray){ colors[who].pop_back(); } else break; } colors[who].pb(make_tuple(ray, col, tm)); } vector<tuple<int, int, int>> kraw; vector<tuple<int, int, int, int>> zapy; bool latwy = true; void solve(){ dfs(1, 1, 0); calc_centroid(1, -1); for(int i=1;i<=q;i++){ auto [tp, vv, di, col] = zapy[i - 1]; if(tp == 3){ //int v, d, c; cin >> v >> d >> c; int v = vv; int d = di; int c = col; int cur = v; while(1){ int odl = dist(v, cur); if(odl <= d){ pu(cur, c, d - odl, i); } cur = par[cur]; if(cur == -1) break; } } else{ //int v; cin >> v; int v = vv; int cur = v; int color = 0; int latest = 0; while(1){ if(colors[cur].size()){ int want = dist(cur, v); int lo = 0; int hi = (int)colors[cur].size() - 1; auto [r, _, __] = colors[cur][0]; if(r >= want){ while(lo + 1 < hi){ int mid = (lo + hi) / 2; auto [r, _, __] = colors[cur][mid]; if(r >= want) lo = mid; else hi = mid; } int bt = lo; for(int mid=lo;mid<=hi;mid++){ auto [r, _, __] = colors[cur][mid]; if(r >= want) bt = mid; } auto [ray, col, tm] = colors[cur][bt]; if(tm > latest){ color = col; latest = tm; } } } cur = par[cur]; if(cur == -1) break; } cout << color << "\n"; } } } int kolorek[nax]; map<pii, int> edgeLen; set<pii> edges[nax]; bool vis[nax]; void add_edge(int x, int y, int d){ if(x > y) swap(x, y); edgeLen[{x, y}] = d; edges[x].insert({y, d}); edges[y].insert({x, d}); } void del_edge(int x, int y){ if(x > y) swap(x, y); int d = edgeLen[{x, y}]; edgeLen.erase({x, y}); edges[x].erase({y, d}); edges[y].erase({x, d}); } void smaruj(int v, int z, int k){ kolorek[v] = k; vis[v] = true; for(auto [y, d] : edges[v]){ if(!vis[y] && d <= z) smaruj(y, z - d, k); } } void read_input(){ cin >> n >> m >> q; rep(i, 1, m){ int x, y, d; cin >> x >> y >> d; kraw.pb(mt(x, y, d)); } latwy = true; rep(i, 1, q){ int tp; cin >> tp; if(tp <= 2) latwy = false; if(tp == 1){ int x, y, d; cin >> x >> y >> d; zapy.pb(mt(tp, x, y, d)); } else if(tp == 2){ int x, y; cin >> x >> y; zapy.pb(mt(tp, x, y, -1)); } else if(tp == 3){ int v, dist, col; cin >> v >> dist >> col; zapy.pb(mt(tp, v, dist, col)); } else if(tp == 4){ int x; cin >> x; zapy.pb(mt(tp, x, -1, -1)); } } if(latwy){ for(auto [x, y, d] : kraw){ adj[x].pb(mp(y, d)); adj[y].pb(mp(x, d)); } solve(); return; } // else brucik :))))))) rep(i, 1, m){ auto [x, y, d] = kraw[i - 1]; add_edge(x, y, d); } for(auto [tp, x, y, z] : zapy){ if(tp == 1){ add_edge(x, y, z); } else if(tp == 2){ del_edge(x, y); } else if(tp == 3){ rep(i, 1, n) vis[i] = false; smaruj(x, y, z); } else{ cout << kolorek[x] << "\n"; } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; //cin >> tt; rep(te, 1, tt) read_input(); 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 2e5 + 5; vector<pii> adj[nax]; int n, m, q; int timer = 0; int pre[nax]; int post[nax]; const int LOG = 20; int up[nax][LOG]; int prefDist[nax]; void dfs(int v, int p, int cost){ up[v][0] = p; for(int i=1;i<LOG;i++){ int kt = up[v][i - 1]; up[v][i] = up[kt][i - 1]; } pre[v] = ++timer; prefDist[v] = prefDist[p] + cost; for(pii go : adj[v]){ if(go.st != p) dfs(go.st, v, go.nd); } post[v] = ++timer; } bool anc(int x, int y){ return (pre[x] <= pre[y] && post[x] >= post[y]); } int lca(int x, int y){ if(anc(x, y)) return x; if(anc(y, x)) return y; for(int i=LOG-1;i>=0;i--){ if(!anc(up[x][i], y)) x = up[x][i]; } return up[x][0]; } int dist(int x, int y){ int d = lca(x, y); return prefDist[x] + prefDist[y] - prefDist[d] * 2; } bool blocked[nax]; int par[nax]; int sz[nax]; void recalc_sz(int v, int p){ sz[v] = 1; for(pii go : adj[v]){ int kt = go.st; if(!blocked[kt] && kt != p){ recalc_sz(kt, v); sz[v] += sz[kt]; } } } int get_centroid(int v, int p, int total){ for(pii go : adj[v]){ int kt = go.st; if(blocked[kt] || kt == p) continue; if(sz[kt] > total / 2) return get_centroid(kt, v, total); } return v; } void calc_centroid(int v, int p){ recalc_sz(v, p); int w = get_centroid(v, v, sz[v]); blocked[w] = true; par[w] = p; for(pii go : adj[w]){ int kt = go.st; if(!blocked[kt]){ calc_centroid(kt, w); } } } vector<tuple<int, int, int> > colors[nax]; // promien, kolor, czas void pu(int who, int col, int ray, int tm){ while(colors[who].size()){ auto [r, c, t] = colors[who].back(); if(r <= ray){ colors[who].pop_back(); } else break; } colors[who].pb(make_tuple(ray, col, tm)); } vector<tuple<int, int, int>> kraw; vector<tuple<int, int, int, int>> zapy; bool latwy = true; void solve(){ dfs(1, 1, 0); calc_centroid(1, -1); for(int i=1;i<=q;i++){ auto [tp, vv, di, col] = zapy[i - 1]; if(tp == 3){ //int v, d, c; cin >> v >> d >> c; int v = vv; int d = di; int c = col; int cur = v; while(1){ int odl = dist(v, cur); if(odl <= d){ pu(cur, c, d - odl, i); } cur = par[cur]; if(cur == -1) break; } } else{ //int v; cin >> v; int v = vv; int cur = v; int color = 0; int latest = 0; while(1){ if(colors[cur].size()){ int want = dist(cur, v); int lo = 0; int hi = (int)colors[cur].size() - 1; auto [r, _, __] = colors[cur][0]; if(r >= want){ while(lo + 1 < hi){ int mid = (lo + hi) / 2; auto [r, _, __] = colors[cur][mid]; if(r >= want) lo = mid; else hi = mid; } int bt = lo; for(int mid=lo;mid<=hi;mid++){ auto [r, _, __] = colors[cur][mid]; if(r >= want) bt = mid; } auto [ray, col, tm] = colors[cur][bt]; if(tm > latest){ color = col; latest = tm; } } } cur = par[cur]; if(cur == -1) break; } cout << color << "\n"; } } } int kolorek[nax]; map<pii, int> edgeLen; set<pii> edges[nax]; bool vis[nax]; void add_edge(int x, int y, int d){ if(x > y) swap(x, y); edgeLen[{x, y}] = d; edges[x].insert({y, d}); edges[y].insert({x, d}); } void del_edge(int x, int y){ if(x > y) swap(x, y); int d = edgeLen[{x, y}]; edgeLen.erase({x, y}); edges[x].erase({y, d}); edges[y].erase({x, d}); } void smaruj(int v, int z, int k){ kolorek[v] = k; vis[v] = true; for(auto [y, d] : edges[v]){ if(!vis[y] && d <= z) smaruj(y, z - d, k); } } void read_input(){ cin >> n >> m >> q; rep(i, 1, m){ int x, y, d; cin >> x >> y >> d; kraw.pb(mt(x, y, d)); } latwy = true; rep(i, 1, q){ int tp; cin >> tp; if(tp <= 2) latwy = false; if(tp == 1){ int x, y, d; cin >> x >> y >> d; zapy.pb(mt(tp, x, y, d)); } else if(tp == 2){ int x, y; cin >> x >> y; zapy.pb(mt(tp, x, y, -1)); } else if(tp == 3){ int v, dist, col; cin >> v >> dist >> col; zapy.pb(mt(tp, v, dist, col)); } else if(tp == 4){ int x; cin >> x; zapy.pb(mt(tp, x, -1, -1)); } } if(latwy){ for(auto [x, y, d] : kraw){ adj[x].pb(mp(y, d)); adj[y].pb(mp(x, d)); } solve(); return; } // else brucik :))))))) rep(i, 1, m){ auto [x, y, d] = kraw[i - 1]; add_edge(x, y, d); } for(auto [tp, x, y, z] : zapy){ if(tp == 1){ add_edge(x, y, z); } else if(tp == 2){ del_edge(x, y); } else if(tp == 3){ rep(i, 1, n) vis[i] = false; smaruj(x, y, z); } else{ cout << kolorek[x] << "\n"; } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; //cin >> tt; rep(te, 1, tt) read_input(); return 0; } |