結果
問題 |
No.2294 Union Path Query (Easy)
|
ユーザー |
|
提出日時 | 2025-02-27 07:19:40 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 8,161 bytes |
コンパイル時間 | 7,400 ms |
コンパイル使用メモリ | 333,312 KB |
実行使用メモリ | 814,656 KB |
最終ジャッジ日時 | 2025-02-27 07:19:54 |
合計ジャッジ時間 | 13,061 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | MLE * 1 -- * 45 |
ソースコード
#include<bits/stdc++.h> #include<atcoder/all> using namespace std; namespace my{ using ml=atcoder::modint998244353; auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;} auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();} #define eb emplace_back #define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll ij=n;ij-->0;) #define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step) #define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__)) #define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a) #define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);} using ll=long long; using ull=unsigned long long; using ulll=__uint128_t; using lll=__int128_t; istream&operator>>(istream&i,ulll&x){ull t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);} istream&operator>>(istream&i,lll&x){ll t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const lll&x){return o<<(x<0?"-":"")<<ulll(x>0?x:-x);} constexpr auto range(bool s,auto...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;if(s)r[2]*=-1;return r;} constexpr char newline=10; constexpr char space=32; constexpr auto schrodinger(bool p,char c){return string(p,c);} lll pow(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;} constexpr auto at2(auto x,auto i){return x>>i&1;} auto&rev(auto&a){ranges::reverse(a);return a;} template<class T>struct queue:std::queue<T>{ queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);} queue(const vector<T>&a){fe(a,e)this->emplace(e);} ll size()const{return std::queue<T>::size();} T pop(){T r=this->front();std::queue<T>::pop();return r;} friend ostream&operator<<(ostream&o,queue q){while(q.size())o<<q.pop()<<schroedinger(q.size()>0,space);return o;} }; template<class T>struct unordered_set:std::unordered_set<T>{ unordered_set(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);} unordered_set(const vector<T>&a){fe(a,e)this->emplace(e);} ll size()const{return std::unordered_set<T>::size();} }; template<ll k>auto pack_kth(const auto&...a){return get<k>(make_tuple(a...));} template<class T,ll n>auto pack_slice(const auto&...a){return[&]<size_t...I>(index_sequence<I...>){return array<T,n>{get<I>(forward_as_tuple(a...))...};}(make_index_sequence<n>{});} template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>; template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;} template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<schrodinger(&e!=&v.back(),vectorial<V>?newline:space);return o;} template<class V>struct vec; template<int rank,class T>struct tensor_helper{using type=vec<typename tensor_helper<rank-1,T>::type>;}; template<class T>struct tensor_helper<0,T>{using type=T;}; template<int rank,class T>using tensor=typename tensor_helper<rank,T>::type; template<class V>struct vec:vector<V>{ using vector<V>::vector; vec(const vector<V>&v){vector<V>::operator=(v);} template<class...A>requires(sizeof...(A)>=3)vec(A...a){const ll n=sizeof...(a)-1;auto t=pack_slice<ll,n>(a...);ll s[n];fo(i,n)s[i]=t[i];*this=make_vec(s,pack_kth<n>(a...));} template<class T,ll n,ll i=0>static auto make_vec(const ll(&s)[n],T x){if constexpr(i==n-1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}} vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;} vec operator^(const vec&u)const{return vec{*this}^=u;} vec&operator+=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]+=u[i];return v;} vec&operator-=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]-=u[i];return v;} vec operator+(const vec&u)const{return vec{*this}+=u;} vec operator-(const vec&u)const{return vec{*this}-=u;} vec&operator++(){fe(*this,e)++e;return*this;} vec&operator--(){fe(*this,e)--e;return*this;} vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;} vec&operator%=(auto M){vec&v=*this;fe(v,e)e%=M;return v;} vec operator%(auto M)const{return vec{*this}%=M;} ll size()const{return vector<V>::size();} auto rev()const{vec v=*this;ranges::reverse(v);return v;} }; template<class...A>requires(sizeof...(A)>=2)vec(A...a)->vec<tensor<sizeof...(a)-2,remove_reference_t<decltype(get<sizeof...(a)-1>(declval<tuple<A...>>()))>>>; vec(ll)->vec<ll>; void lin(auto&...a){(cin>>...>>a);} struct dsu{ ll n; vec<int>par_or_size; dsu(ll n):n(n),par_or_size(n,-1){} auto merge(ll a,ll b){ ll x=leader(a),y=leader(b); if(x==y)return tuple{x,a,y,b}; if(-par_or_size[x]<-par_or_size[y])swap(x,y),swap(a,b); par_or_size[x]+=par_or_size[y]; par_or_size[y]=x; return tuple{x,a,y,b}; } bool same(ll a,ll b){return leader(a)==leader(b);} ll leader(ll a){ assert(0<=a&&a<n); if(par_or_size[a]<0)return a; return par_or_size[a]=leader(par_or_size[a]); } ll size(ll a){return-par_or_size[leader(a)];} }; template<class WT>struct edge{ int from,to; WT wt; int id; edge()=default; edge(int from,int to,WT wt=1,int id=-1):from(from),to(to),wt(wt),id(id){} auto rev()const{auto e=*this;swap(e.from,e.to);return e;} auto operator<=>(const edge&e)const{return wt<=>e.wt;} friend ostream&operator<<(ostream&o,const edge&e){return o<<"(to "<<e.to<<", wt "<<e.wt<<",id "<<e.id<<")";} }; template<class WT>struct rooted_tree{ vec<vec<edge<WT>>>edges; vec<edge<WT>>rev_edge; ll r; rooted_tree(ll n,ll r):edges(n),rev_edge(n,{-1,-1,WT{},-1}),r(r){} friend ostream&operator<<(ostream&o,const rooted_tree&g){ fo(u,g.size()){ o<<"from "<<u<<": "; fo(j,g[u].size())o<<g[u][j]<<schrodinger(j!=g[u].size()-1,space); o<<newline; } return o; } decltype(auto)operator[](ll i){return edges[i];} decltype(auto)operator[](ll i)const{return edges[i];} ll size()const{return edges.size();} ll par(ll u)const{return u==r?-1:rev_edge[u].from;} void add_edge_and_set_rev_edge(const edge<WT>&e){ edges[e.from].eb(e); rev_edge[e.to]=e; } auto bfs_order(ll start)const{ vec<ll>ord; queue<ll>q{start}; while(q.size()){ ll u=q.pop(); ord.eb(u); fe(edges[u],e)q.emplace(e.to); } return ord; } auto&set_root(ll U){ if(par(U)==-1)return*this; ll pre=-1,cu=U; while(cu!=-1){ ll nx=par(cu); vec<edge<WT>>new_edges; new_edges.reserve(edges[cu].size()); if(nx!=-1){ new_edges.eb(rev_edge[cu].rev()); if(cu==U)rev_edge[cu]={-1,-1,WT{},-1}; } fe(edges[cu],e){ if(e.to==pre)rev_edge[cu]=e.rev(); else new_edges.eb(e); } swap(new_edges,edges[cu]); pre=cu; cu=nx; } r=U; return*this; } }; template<class WT>struct rooted_forest:rooted_tree<WT>{ unordered_set<ll>roots; rooted_forest(ll n):rooted_tree<WT>(n,-1){} auto&merge(const edge<WT>&e){ this->set_root(e.to); this->add_edge_and_set_rev_edge(e); return*this; } }; void pp(auto&&...a){ll n=sizeof...(a);((cout<<a<<schrodinger(--n>0,space)),...);cout<<newline;} single_testcase void solve(){ RD(int,N,X,Q); rooted_forest<int8_t>g(N); dsu uf(N); vec xor_dist(N,0); ll B=30; vec cnt(N,B,0); fo(Q){ LL(f);--f; if(f==0){ RD(int,u,w); auto[nl,from,ol,to]=uf.merge(u,X); g.merge(edge<int8_t>{(int)from,(int)to}); fe(g.bfs_order(to),v){ xor_dist[v]^=w^xor_dist[from]; fo(b,B)cnt[nl][b]+=at2(xor_dist[v],b); } }else if(f==1){ LL(u,v); if(!uf.same(u,v)){ pp(-1); continue; } int res=xor_dist[u]^xor_dist[v]; (X+=res)%=N; pp(res); }else if(f==2){ LL(u); ll l=uf.leader(u); ll sz=uf.size(l); ml res=0; fo(b,B)res+=ml{2}.pow(b)*cnt[l][b]*(sz-cnt[l][b]); pp(res); }else if(f==3){ LL(x); (X+=x)%=N; } } }}