#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MAX_N = 200'005; constexpr int MAX_NODES = MAX_N*8; constexpr int MAX_LEVEL = 20; constexpr int ADD_EDGE = 1; constexpr int DEL_EDGE = 2; constexpr int BALL = 3; constexpr int QUERY = 4; struct Edge { int u, v, w, begin, end; }; struct Ball { int v, k; ll d; }; using Image = array<pair<int, ll>, 2>; vector<ll> radius[MAX_NODES]; // vertex -> ball radius vector<Image> images[MAX_NODES]; // parent vertex -> image in current vector<Ball> balls; vector<Edge> edges[MAX_LEVEL]; vector<pair<int, ll>> adj[MAX_LEVEL][MAX_N]; Image mapImage(const vector<Image>& img, pair<int, ll> up) { if (up.x == -1) return {up, up}; Image t = img[up.x]; each(p, t) p.y += up.y; return t; } Image mapImage(vector<Image>& img, Image up) { Image A = mapImage(img, up[0]); Image B = mapImage(img, up[1]); if (A[0].x == -1) return B; if (B[0].x != -1) each(p, B) { bool neu = 1; each(q, A) { if (q.x == p.x) { q.y = min(q.y, p.y); neu = 0; } } if (neu) { assert(A[0] == A[1]); A[1] = p; } } return A; } bool isCovered(int i, Image v) { each(p, v) if (p.x != -1 && radius[i][p.x] >= p.y) return 1; return 0; } int query(int lim, Image v, int i, int begin, int end) { if (begin >= lim) return -1; v = mapImage(images[i], v); if (lim < end) { int mid = (begin+end) / 2; int j = query(lim, v, i*2+1, mid, end); if (j != -1) return j; return query(lim, v, i*2, begin, mid); } if (!isCovered(i, v)) return -1; while (end-begin > 1) { int mid = (begin+end) / 2; Image vr = mapImage(images[i*2+1], v); if (isCovered(i*2+1, vr)) { v = vr; i = i*2+1; begin = mid; } else { v = mapImage(images[i*2], v); i = i*2; end = mid; } } return begin; } int query(int lim, int v) { return query(lim, { make_pair(v, 0), make_pair(v, 0) }, 1, 0, sz(balls)); } void makeImages(int v, int p, int s, ll d, int L, vector<Image>& img) { int x = (img[v][0].x != -1); img[v][x] = {s, d}; each(e, adj[L-1][v]) if (e.x != p && img[e.x][0].y != 0) { makeImages(e.x, v, s, d+e.y, L, img); } } pair<int, ll> compress(int v, int p, int L, vector<Image>& img, int& n2) { pair<int, ll> desc[sz(adj[L-1][v])+1]; int numDesc = 0; each(e, adj[L-1][v]) if (e.x != p) { desc[numDesc] = compress(e.x, v, L, img, n2); if (desc[numDesc].x != -1) { desc[numDesc++].y += e.y; } } if (img[v][0].y != 0) { if (numDesc == 0) return {-1, -1}; if (numDesc == 1) return desc[0]; } int cv = n2++; adj[L][cv].clear(); each(t, img[v]) t = {cv, 0}; rep(i, 0, numDesc) { auto [cu, d] = desc[i]; adj[L][cu].pb({cv, d}); adj[L][cv].pb({cu, d}); } return {cv, 0}; } void build(int begin, int end, int i, int L, int n) { int savedDegrees[n]; rep(v, 0, n) savedDegrees[v] = sz(adj[L-1][v]); auto &img = images[i]; img.resize(n, { make_pair(-1, -1LL), make_pair(-1, -1LL) }); edges[L].clear(); rep(j, begin, end) { img[balls[j].v][0].y = 0; } each(e, edges[L-1]) { if (e.begin <= begin && end <= e.end) { adj[L-1][e.u].pb({e.v, e.w}); adj[L-1][e.v].pb({e.u, e.w}); } else if (e.begin < end && begin < e.end) { edges[L].pb(e); img[e.u][0].y = img[e.v][0].y = 0; } } int n2 = 0; rep(v, 0, n) if (img[v][0].x == -1 && img[v][0].y == 0) compress(v, -1, L, img, n2); rep(v, 0, n) if (img[v][0].y == 0) makeImages(v, -1, img[v][0].x, 0, L, img); rep(v, 0, n) adj[L-1][v].resize(savedDegrees[v]); rep(j, begin, end) { balls[j].v = img[balls[j].v][0].x; } each(e, edges[L]) { e.u = img[e.u][0].x; e.v = img[e.v][0].x; } if (end-begin == 1) { assert(n2 == 1); radius[i] = {balls[begin].d}; } else { int mid = (begin+end) / 2; radius[i].resize(n2, -1); build(begin, mid, i*2, L+1, n2); build(mid, end, i*2+1, L+1, n2); } rep(v, 0, n) { each(p, img[v]) if (p.x != -1) { radius[i/2][v] = max(radius[i/2][v], radius[i][p.x]-p.y); } } } void run() { int n, m, q; cin >> n >> m >> q; edges[0].resize(m); vector<pii> queries; map<pii, int> edgeMap; rep(i, 0, m) { auto &e = edges[0][i]; cin >> e.u >> e.v >> e.w; e.u--; e.v--; if (e.u > e.v) swap(e.u, e.v); edgeMap[{e.u, e.v}] = i; e.begin = 0; e.end = -1; } rep(i, 0, q) { int t; cin >> t; if (t == ADD_EDGE) { int u, v, w; cin >> u >> v >> w; u--; v--; if (u > v) swap(u, v); edges[0].pb({u, v, w, sz(balls), -1}); edgeMap[{u, v}] = sz(edges[0])-1; } else if (t == DEL_EDGE) { int u, v; cin >> u >> v; u--; v--; if (u > v) swap(u, v); edges[0][edgeMap[{u, v}]].end = sz(balls); edgeMap.erase({u, v}); } else if (t == BALL) { Ball e; cin >> e.v >> e.d >> e.k; e.v--; balls.pb(e); } else if (t == QUERY) { int v; cin >> v; v--; queries.pb({v, sz(balls)}); } else { assert(0); } } if (balls.empty()) { rep(i, 0, sz(queries)) cout << "0\n"; return; } each(e, edgeMap) { edges[0][e.y].end = sz(balls); } radius[0].resize(n); build(0, sz(balls), 1, 1, n); each(e, queries) { int j = query(e.y, e.x); int ans = (j != -1 ? balls[j].k : 0); cout << ans << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MAX_N = 200'005; constexpr int MAX_NODES = MAX_N*8; constexpr int MAX_LEVEL = 20; constexpr int ADD_EDGE = 1; constexpr int DEL_EDGE = 2; constexpr int BALL = 3; constexpr int QUERY = 4; struct Edge { int u, v, w, begin, end; }; struct Ball { int v, k; ll d; }; using Image = array<pair<int, ll>, 2>; vector<ll> radius[MAX_NODES]; // vertex -> ball radius vector<Image> images[MAX_NODES]; // parent vertex -> image in current vector<Ball> balls; vector<Edge> edges[MAX_LEVEL]; vector<pair<int, ll>> adj[MAX_LEVEL][MAX_N]; Image mapImage(const vector<Image>& img, pair<int, ll> up) { if (up.x == -1) return {up, up}; Image t = img[up.x]; each(p, t) p.y += up.y; return t; } Image mapImage(vector<Image>& img, Image up) { Image A = mapImage(img, up[0]); Image B = mapImage(img, up[1]); if (A[0].x == -1) return B; if (B[0].x != -1) each(p, B) { bool neu = 1; each(q, A) { if (q.x == p.x) { q.y = min(q.y, p.y); neu = 0; } } if (neu) { assert(A[0] == A[1]); A[1] = p; } } return A; } bool isCovered(int i, Image v) { each(p, v) if (p.x != -1 && radius[i][p.x] >= p.y) return 1; return 0; } int query(int lim, Image v, int i, int begin, int end) { if (begin >= lim) return -1; v = mapImage(images[i], v); if (lim < end) { int mid = (begin+end) / 2; int j = query(lim, v, i*2+1, mid, end); if (j != -1) return j; return query(lim, v, i*2, begin, mid); } if (!isCovered(i, v)) return -1; while (end-begin > 1) { int mid = (begin+end) / 2; Image vr = mapImage(images[i*2+1], v); if (isCovered(i*2+1, vr)) { v = vr; i = i*2+1; begin = mid; } else { v = mapImage(images[i*2], v); i = i*2; end = mid; } } return begin; } int query(int lim, int v) { return query(lim, { make_pair(v, 0), make_pair(v, 0) }, 1, 0, sz(balls)); } void makeImages(int v, int p, int s, ll d, int L, vector<Image>& img) { int x = (img[v][0].x != -1); img[v][x] = {s, d}; each(e, adj[L-1][v]) if (e.x != p && img[e.x][0].y != 0) { makeImages(e.x, v, s, d+e.y, L, img); } } pair<int, ll> compress(int v, int p, int L, vector<Image>& img, int& n2) { pair<int, ll> desc[sz(adj[L-1][v])+1]; int numDesc = 0; each(e, adj[L-1][v]) if (e.x != p) { desc[numDesc] = compress(e.x, v, L, img, n2); if (desc[numDesc].x != -1) { desc[numDesc++].y += e.y; } } if (img[v][0].y != 0) { if (numDesc == 0) return {-1, -1}; if (numDesc == 1) return desc[0]; } int cv = n2++; adj[L][cv].clear(); each(t, img[v]) t = {cv, 0}; rep(i, 0, numDesc) { auto [cu, d] = desc[i]; adj[L][cu].pb({cv, d}); adj[L][cv].pb({cu, d}); } return {cv, 0}; } void build(int begin, int end, int i, int L, int n) { int savedDegrees[n]; rep(v, 0, n) savedDegrees[v] = sz(adj[L-1][v]); auto &img = images[i]; img.resize(n, { make_pair(-1, -1LL), make_pair(-1, -1LL) }); edges[L].clear(); rep(j, begin, end) { img[balls[j].v][0].y = 0; } each(e, edges[L-1]) { if (e.begin <= begin && end <= e.end) { adj[L-1][e.u].pb({e.v, e.w}); adj[L-1][e.v].pb({e.u, e.w}); } else if (e.begin < end && begin < e.end) { edges[L].pb(e); img[e.u][0].y = img[e.v][0].y = 0; } } int n2 = 0; rep(v, 0, n) if (img[v][0].x == -1 && img[v][0].y == 0) compress(v, -1, L, img, n2); rep(v, 0, n) if (img[v][0].y == 0) makeImages(v, -1, img[v][0].x, 0, L, img); rep(v, 0, n) adj[L-1][v].resize(savedDegrees[v]); rep(j, begin, end) { balls[j].v = img[balls[j].v][0].x; } each(e, edges[L]) { e.u = img[e.u][0].x; e.v = img[e.v][0].x; } if (end-begin == 1) { assert(n2 == 1); radius[i] = {balls[begin].d}; } else { int mid = (begin+end) / 2; radius[i].resize(n2, -1); build(begin, mid, i*2, L+1, n2); build(mid, end, i*2+1, L+1, n2); } rep(v, 0, n) { each(p, img[v]) if (p.x != -1) { radius[i/2][v] = max(radius[i/2][v], radius[i][p.x]-p.y); } } } void run() { int n, m, q; cin >> n >> m >> q; edges[0].resize(m); vector<pii> queries; map<pii, int> edgeMap; rep(i, 0, m) { auto &e = edges[0][i]; cin >> e.u >> e.v >> e.w; e.u--; e.v--; if (e.u > e.v) swap(e.u, e.v); edgeMap[{e.u, e.v}] = i; e.begin = 0; e.end = -1; } rep(i, 0, q) { int t; cin >> t; if (t == ADD_EDGE) { int u, v, w; cin >> u >> v >> w; u--; v--; if (u > v) swap(u, v); edges[0].pb({u, v, w, sz(balls), -1}); edgeMap[{u, v}] = sz(edges[0])-1; } else if (t == DEL_EDGE) { int u, v; cin >> u >> v; u--; v--; if (u > v) swap(u, v); edges[0][edgeMap[{u, v}]].end = sz(balls); edgeMap.erase({u, v}); } else if (t == BALL) { Ball e; cin >> e.v >> e.d >> e.k; e.v--; balls.pb(e); } else if (t == QUERY) { int v; cin >> v; v--; queries.pb({v, sz(balls)}); } else { assert(0); } } if (balls.empty()) { rep(i, 0, sz(queries)) cout << "0\n"; return; } each(e, edgeMap) { edges[0][e.y].end = sz(balls); } radius[0].resize(n); build(0, sz(balls), 1, 1, n); each(e, queries) { int j = query(e.y, e.x); int ans = (j != -1 ? balls[j].k : 0); cout << ans << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |