結果

問題 No.1054 Union add query
ユーザー noya2noya2
提出日時 2022-09-22 01:36:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 894 ms / 2,000 ms
コード長 11,098 bytes
コンパイル時間 5,089 ms
コンパイル使用メモリ 288,168 KB
実行使用メモリ 187,884 KB
最終ジャッジ日時 2024-06-01 17:41:05
合計ジャッジ時間 10,704 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,948 KB
testcase_03 AC 419 ms
68,972 KB
testcase_04 AC 894 ms
187,884 KB
testcase_05 AC 327 ms
50,060 KB
testcase_06 AC 444 ms
125,664 KB
testcase_07 AC 401 ms
125,652 KB
testcase_08 AC 464 ms
125,616 KB
testcase_09 AC 694 ms
184,312 KB
testcase_10 AC 567 ms
184,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "c.cpp"
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,n,m) for (int i = m; i < int(n); ++i)
#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using pdd = pair<long double, long double>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using ppi = pair<P,int>;
using pip = pair<int,P>;
const int INF = 1001001007;
const long long mod1 = 1000000007LL;
const long long mod2 = 998244353LL;
const ll inf = 2e18;
const ld pi = 3.14159265358979323;
const ld eps = 1e-7;
template<typename T>void o(T a);
template<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}
template<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}
template<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}
template<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}
void revs(string &s) {reverse(s.begin(),s.end());}
template<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}
template<typename T>T cel(T a,T b){return (a+b-1)/b;}
template<typename T1, typename T2>void print(pair<T1,T2> a);
template<typename T>void print(vector<T> v);
template<typename T>void print(vector<vector<T>> v);
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
void print(unsigned int a){ printf("%u", a); }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
void o(){cout<<"!?"<<endl;}
template<typename T>void o(T a){cout<<a<<endl;}
template<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}
template<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}
template<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}
void yes(){out("Yes");}
void no (){out("No");}
void yn (bool t){if(t)yes();else no();}
vector<int> dx = {0,1,0,-1,1,1,-1,-1};
vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}

using mint = modint998244353;
void out(mint a){out(a.val());}
void out(vector<mint> a){vector<ll> b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);}
void out(vector<vector<mint>> a){for (auto v : a) out(v);}
istream &operator>>(istream &is,vector<mint> &v){for(auto &e:v){ll _x;is>>_x;e=_x;}return is;}

#line 2 "UFtree.hpp"

#line 4 "UFtree.hpp"

namespace noya2 {

struct UFtree{
    int n, m;
    std::vector<int> rot, par;
    UFtree (int _n = 0) : n(_n) {init();}
    int parent(int u){
        if (rot[u] < 0) return u;
        return rot[u] = parent(rot[u]);
    }
    bool same(int u, int v){
        return parent(u) == parent(v);
    }
    int merge(int u, int v){
        if (same(u,v)) return -1;
        u = parent(u);
        v = parent(v);
        rot[u] = n+m;
        rot[v] = n+m;
        par[u] = n+m;
        par[v] = n+m;
        return m++;
    }
    void init(){
        rot.resize(n+n-1,-1);
        par.resize(n+n-1,-1);
        m = 0;
    }
};

} // namespace noya2
#line 2 "Tree.hpp"

namespace noya2{
using ll = long long;
using P = pair<int,int>;

struct edge{
    int to, idx;
    ll cost;
    edge (int _to = -1, ll _cost = 1, int _idx = -1) : to(_to), cost(_cost), idx(_idx) {}
};

struct Tree{
    Tree (int _n = 0, int _root = 0) : n(_n), root(_root) {
        assert(0 <= _root && _root < n);
        initialize();
    }
    int add_edge(int u, int v, ll cost = 1){
        int res = edges.size();
        vs[u].emplace_back(edge(v,cost,res));
        vs[v].emplace_back(edge(u,cost,res));
        edges.emplace_back(P(u,v));
        return res;
    }
    void build(){
        dfs_init(root);
        int t = 0;
        dfs_hld(root,t);
    }
    int lca(int u, int v){
        while (nxt[u] != nxt[v]){
            if (down[u] < down[v]) swap(u,v);
            u = par[nxt[u]];
        }
        return depth[u] < depth[v] ? u : v;
    }
    vector<int> point_HLD(){
        return hld_order;
    }
    vector<int> edge_HLD(){
        vector<int> res(n,-1);
        for (int i = 1; i < n; i++){
            res[i] = par_edge_idx[hld_order[i]];
        }
        return res;
    }
    P getidx(int v){return P(down[v],up[v]);}
    vector<int> child(int v){
        vector<int> res;
        for (edge x : vs[v]) if (x.to != par[v]) res.emplace_back(x.to);
        return res;
    }
    int parent(int v){return par[v];}
    int deepth(int v){return depth[v];}

    template<typename F>
    void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right).
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            s > t ? f(t,s) : f(s,t);
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            s > t ? f(t,s) : f(s,t);
        }
    }
    template<typename F>
    void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u]
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            f(s,t); // le > ri ok
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            f(s,t); // le > ri ok
        }
    }
    template<typename F>
    void subtree_query(int v, bool vertex, const F &f){
        f(down[v] + (vertex ? 0 : 1), up[v]);
    }
  private:
    int n;
    int root;
    vector<P> edges;
    vector<vector<edge>> vs;
    vector<int> size, par, depth, up, down, nxt, hld_order; // nxt[i] : most shallow vertex in connected component of vertex i
    vector<int> par_edge_idx; // index of the edge to par
    void initialize(){
        vs.resize(n);
        size.resize(n,0);
        par.resize(n,root);
        depth.resize(n,0);
        up.resize(n,-1);
        down.resize(n,-1);
        nxt.resize(n,root);
        hld_order.resize(n,-1);
        par_edge_idx.resize(n,-1);
    }
    void dfs_init(int cur){
        size[cur] = 1;
        for (edge &e : vs[cur]){
            if (e.to == par[cur]){
                if (vs[cur].size() >= 2 && e.to == vs[cur][0].to){
                    swap(vs[cur][0],vs[cur][1]); // if cur is not leaf, vs[cur][0] is not cur's parent
                }
                else continue;
            }
            par[e.to] = cur;
            par_edge_idx[e.to] = e.idx;
            depth[e.to] = depth[cur] + 1;
            dfs_init(e.to);
            size[cur] += size[e.to];
            if (size[e.to] > size[vs[cur][0].to]){
                swap(e,vs[cur][0]); // to maximize vs[cur][0]'s subtree_size
            }
        }
    }
    void dfs_hld(int cur, int &tnow){
        down[cur] = tnow++; // down[0,...,n-1] is permutation of 0,...,n-1
        hld_order[down[cur]] = cur; // hld_order[i] is ith vertex visited on Euler tour 
        for (edge e : vs[cur]){
            if (e.to == par[cur]) continue;
            nxt[e.to] = (e.to == vs[cur][0].to ? nxt[cur] : e.to);
            dfs_hld(e.to,tnow);
        }
        up[cur] = tnow; // up[0,...,n-1] is NOT permutation, up[*] <= n
    }
    vector<P> ascend(int u, int v) const { // [u,v), depth[u] > depth[v]
        vector<P> res;
        while (nxt[u] != nxt[v]){
            res.emplace_back(P(down[u],down[nxt[u]])); // [s1,t1], [s2,t2], ...
            u = par[nxt[u]];
        }
        if (u != v) res.emplace_back(P(down[u],down[v]+1)); // [s,t). v is not in the range (down[] is ordered opposite direction of depth)
        return res;
    }
    vector<P> descend(int u, int v) const { // (u,v], depth[u] < depth[v]
        if (u == v) return {};
        if (nxt[u] == nxt[v]){
            return {P(down[u]+1,down[v])}; // (s,t]. u is not in the range
        }
        vector<P> res = descend(u,par[nxt[v]]);
        res.emplace_back(P(down[nxt[v]],down[v])); // [s1,t1], [s2,t2], ...
        return res;
    }
};

} //namespace noya2
#line 77 "c.cpp"
using namespace noya2;

ll op(ll a, ll b){return max(a,b);}
ll e(){return -inf;}

ll mapping(ll f, ll x){return f + x;}
ll composition(ll f, ll g){return f + g;}
ll id(){return 0LL;}

void solve(){
    int n, q; cin >> n >> q;
    vector<vector<int>> query(q,vector<int>(3)); cin >> query;
    UFtree d(n);
    rep(i,q){
        query[i][1]--;
        if (query[i][0] == 3) continue;
        if (query[i][0] == 1){
            query[i][2]--;
            d.merge(query[i][1],query[i][2]);
        }
        else {
            query[i][1] = d.parent(query[i][1]);
        }
    }
    Tree g(n+n,n+n-1);
    rep(i,n+n-1){
        if (d.par[i] > 0) g.add_edge(i,d.par[i]);
        else g.add_edge(i,n+n-1);
    }
    g.build();
    auto v = g.point_HLD();
    lazy_segtree<ll,op,e,ll,mapping,composition,id> seg(n+n);
    rep(i,n+n) seg.set(i,0);
    ll val = 0;
    auto f = [&](int l, int r){
        seg.apply(l,r,val);
    };
    rep(i,q){
        if (query[i][0] == 1) continue;
        if (query[i][0] == 2){
            val = query[i][2];
            g.subtree_query(query[i][1],true,f);
        }
        else {
            out(seg.get(g.getidx(query[i][1]).first));
        }
    }
}

int main(){
    fast_io();
    int t = 1; //cin >> t;
    while(t--) solve();
}
0