結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | t98slider |
提出日時 | 2023-03-21 10:54:19 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 572 ms / 7,000 ms |
コード長 | 5,210 bytes |
コンパイル時間 | 4,539 ms |
コンパイル使用メモリ | 257,096 KB |
実行使用メモリ | 42,424 KB |
最終ジャッジ日時 | 2024-11-23 04:54:03 |
合計ジャッジ時間 | 24,359 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 22 ms
21,200 KB |
testcase_02 | AC | 21 ms
21,204 KB |
testcase_03 | AC | 23 ms
21,196 KB |
testcase_04 | AC | 220 ms
42,296 KB |
testcase_05 | AC | 219 ms
42,296 KB |
testcase_06 | AC | 298 ms
42,168 KB |
testcase_07 | AC | 349 ms
42,296 KB |
testcase_08 | AC | 345 ms
42,296 KB |
testcase_09 | AC | 314 ms
35,568 KB |
testcase_10 | AC | 344 ms
35,456 KB |
testcase_11 | AC | 352 ms
35,584 KB |
testcase_12 | AC | 348 ms
34,508 KB |
testcase_13 | AC | 195 ms
27,360 KB |
testcase_14 | AC | 177 ms
26,880 KB |
testcase_15 | AC | 347 ms
42,296 KB |
testcase_16 | AC | 356 ms
38,400 KB |
testcase_17 | AC | 311 ms
30,464 KB |
testcase_18 | AC | 551 ms
42,296 KB |
testcase_19 | AC | 524 ms
34,304 KB |
testcase_20 | AC | 551 ms
42,296 KB |
testcase_21 | AC | 568 ms
42,328 KB |
testcase_22 | AC | 572 ms
42,424 KB |
testcase_23 | AC | 251 ms
42,292 KB |
testcase_24 | AC | 263 ms
34,432 KB |
testcase_25 | AC | 223 ms
42,296 KB |
testcase_26 | AC | 225 ms
42,296 KB |
testcase_27 | AC | 298 ms
42,296 KB |
testcase_28 | AC | 295 ms
42,292 KB |
testcase_29 | AC | 247 ms
42,296 KB |
testcase_30 | AC | 254 ms
42,368 KB |
testcase_31 | AC | 252 ms
42,296 KB |
testcase_32 | AC | 292 ms
42,328 KB |
testcase_33 | AC | 369 ms
42,316 KB |
testcase_34 | AC | 220 ms
42,340 KB |
testcase_35 | AC | 250 ms
42,368 KB |
testcase_36 | AC | 509 ms
42,164 KB |
testcase_37 | AC | 353 ms
36,796 KB |
testcase_38 | AC | 316 ms
36,864 KB |
testcase_39 | AC | 312 ms
36,864 KB |
testcase_40 | AC | 316 ms
36,736 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 300 ms
42,352 KB |
testcase_45 | AC | 306 ms
42,296 KB |
testcase_46 | AC | 330 ms
42,296 KB |
testcase_47 | AC | 365 ms
42,300 KB |
testcase_48 | AC | 336 ms
42,364 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using ll = long long; template <class S, S (*op)(S, S), S (*e)()> class LinkCutTree { struct Node { Node *l, *r, *p; S key, sum; bool rev; explicit Node(const S &v) : key(v), sum(v), rev(false), l(nullptr), r(nullptr), p(nullptr) {} bool is_root() const { return not p or (p->l != this and p->r != this); } }; void update(Node* t) { t->sum = t->key; if(t->l) t->sum = op(t->l->sum, t->sum); if(t->r) t->sum = op(t->sum, t->r->sum); } void rotate_right(Node* t) { Node *x = t->p, *y = x->p; if((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void rotate_left(Node* t) { Node *x = t->p, *y = x->p; if((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void toggle(Node* t) { std::swap(t->l, t->r); //t->sum = s(t->sum); t->rev ^= true; } void push(Node* t) { if(t->rev) { if(t->l) toggle(t->l); if(t->r) toggle(t->r); t->rev = false; } } void splay(Node *t) { push(t); while(not t->is_root()) { Node* q = t->p; if(q->is_root()) { push(q), push(t); q->l == t ? rotate_right(t) : rotate_left(t); } else { Node* r = q->p; push(r), push(q), push(t); if(r->l == q) { if(q->l == t) rotate_right(q), rotate_right(t); else rotate_left(t), rotate_right(t); } else { if(q->r == t) rotate_left(q), rotate_left(t); else rotate_right(t), rotate_left(t); } } } } std::vector<Node*> Node_info; public: LinkCutTree(int n) : LinkCutTree(std::vector<S>(n, e())) {} LinkCutTree(const std::vector<S> &vec) : Node_info(vec.size()){ for(int i = 0; i < vec.size(); i++) { Node_info[i] = new Node(vec[i]); } } Node* expose(int v) { Node* t = Node_info[v]; Node* rp = nullptr; for(Node* cur = t; cur; cur = cur->p) { splay(cur); cur->r = rp; update(cur); rp = cur; } splay(t); return rp; } void evert(int v) { expose(v); toggle(Node_info[v]); push(Node_info[v]); } void link(int _child, int _parent) { Node *child = Node_info[_child], *parent = Node_info[_parent]; evert(_child); expose(_parent); child->p = parent; parent->r = child; update(parent); } void cut(int _child) { Node *child = Node_info[_child]; expose(_child); auto *parent = child->l; child->l = nullptr; parent->p = nullptr; } bool is_connected(int u, int v) { expose(u), expose(v); return Node_info[u] == Node_info[v] or Node_info[u]->p; } const S &query(int u, int v) { evert(u); expose(v); return Node_info[v]->sum; } void set(int p, S v) { expose(p); Node_info[p]->key = v; update(Node_info[p]); } }; ll op(ll lhs, ll rhs){return lhs + rhs;} ll e(){return 0;} int main() { ios::sync_with_stdio(false); cin.tie(0); int N, Q; ll X; cin >> N >> X >> Q; LinkCutTree<ll, op, e> LCT(N + Q); atcoder::dsu uf(N); vector<ll> dia(N); vector<array<int,2>> sv(N); for(int i = 0; i < N; i++) sv[i] = {i, i}; auto update_diameter = [&](int x, int y){ ll mx; int u, v; if(dia[x] >= dia[y]) mx = dia[x], u = sv[x][0], v = sv[x][1]; else mx = dia[y], u = sv[y][0], v = sv[y][1]; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ ll d = LCT.query(sv[x][i], sv[y][j]); if(d > mx) mx = d, u = sv[x][i], v = sv[y][j]; } } int r = uf.merge(x, y); dia[r] = mx; sv[r] = {u, v}; }; int type, v, u, w; for(int i = 0; i < Q; i++){ cin >> type; if(type == 1){ cin >> v >> w; LCT.link(v, i + N); LCT.link(X, i + N); LCT.set(i + N, w); update_diameter(uf.leader(v), uf.leader(X)); }else if(type == 2){ cin >> v >> u; if(!uf.same(v, u)){ cout << -1 << '\n'; continue; } ll d = LCT.query(v, u); (X += d) %= N; cout << d << '\n'; }else if(type == 3){ cin >> v; cout << dia[uf.leader(v)] << '\n'; }else{ cin >> v; X += v; if(X >= N) X -= N; } } }