結果
問題 | No.2341 Triple Tree Query (Medium) |
ユーザー | 👑 potato167 |
提出日時 | 2023-06-02 23:50:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 584 ms / 5,000 ms |
コード長 | 12,476 bytes |
コンパイル時間 | 3,302 ms |
コンパイル使用メモリ | 255,372 KB |
実行使用メモリ | 64,524 KB |
最終ジャッジ日時 | 2024-06-09 02:22:36 |
合計ジャッジ時間 | 22,198 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 570 ms
48,360 KB |
testcase_03 | AC | 583 ms
48,576 KB |
testcase_04 | AC | 584 ms
48,212 KB |
testcase_05 | AC | 581 ms
48,684 KB |
testcase_06 | AC | 583 ms
48,616 KB |
testcase_07 | AC | 409 ms
59,508 KB |
testcase_08 | AC | 413 ms
57,176 KB |
testcase_09 | AC | 411 ms
64,524 KB |
testcase_10 | AC | 417 ms
60,136 KB |
testcase_11 | AC | 406 ms
62,820 KB |
testcase_12 | AC | 411 ms
56,284 KB |
testcase_13 | AC | 404 ms
58,932 KB |
testcase_14 | AC | 429 ms
57,840 KB |
testcase_15 | AC | 419 ms
62,968 KB |
testcase_16 | AC | 422 ms
54,588 KB |
testcase_17 | AC | 334 ms
41,896 KB |
testcase_18 | AC | 327 ms
41,852 KB |
testcase_19 | AC | 332 ms
41,932 KB |
testcase_20 | AC | 342 ms
41,940 KB |
testcase_21 | AC | 333 ms
41,928 KB |
testcase_22 | AC | 333 ms
41,804 KB |
testcase_23 | AC | 340 ms
41,812 KB |
testcase_24 | AC | 341 ms
41,888 KB |
testcase_25 | AC | 469 ms
42,772 KB |
testcase_26 | AC | 469 ms
42,932 KB |
testcase_27 | AC | 471 ms
42,860 KB |
testcase_28 | AC | 467 ms
42,676 KB |
testcase_29 | AC | 473 ms
42,744 KB |
testcase_30 | AC | 347 ms
41,948 KB |
testcase_31 | AC | 341 ms
41,904 KB |
testcase_32 | AC | 340 ms
41,932 KB |
testcase_33 | AC | 417 ms
43,544 KB |
testcase_34 | AC | 416 ms
43,548 KB |
testcase_35 | AC | 431 ms
44,188 KB |
testcase_36 | AC | 439 ms
44,160 KB |
testcase_37 | AC | 426 ms
43,836 KB |
ソースコード
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; ll ILL=2167167167167167167; const int INF=2100000000; const int mod=998244353; #define rep(i,a,b) for (ll i=a;i<b;i++) #define all(p) p.begin(),p.end() template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>; template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;} template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;} template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());} template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";} template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";} template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;} template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;} template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;} int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;} /* #include<vector> #include<cassert> #include<math.h> */ namespace po167{ struct LCA{ std::vector<int> deep; std::vector<std::vector<int>> parent; int n; int m; LCA(int root_node,std::vector<std::vector<int>> &G):n((int)G.size()),m((int)(2+std::log(n)/std::log(2))){ deep.resize(n),parent.assign(n,std::vector<int>(m)); for(int i=0;i<n;i++){ parent[i][0]=-1; deep[i]=-1; } deep[root_node]=0; parent[root_node][0]=-2; std::vector<int> order(n); order[0]=root_node; int look=1; for(int i=0;i<n;i++){ for(auto x:G[order[i]]){ if(deep[x]==-1){ deep[x]=deep[order[i]]+1; parent[x][0]=order[i]; order[look]=x; look++; } } } assert(look==n);//when G is not connected for(int i=0;i<n;i++) for(int j=1;j<m;j++){ int var=order[i]; if(parent[var][j-1]==-2) parent[var][j]=-2; else{ parent[var][j]=parent[parent[var][j-1]][j-1]; } } } int lca(int var_1,int var_2){ if(deep[var_1]>deep[var_2]){ std::swap(var_1,var_2); } int dif=deep[var_2]-deep[var_1]; int ind=0; while(dif){ if(dif&1){ var_2=parent[var_2][ind]; } ind++; dif>>=1; } if(var_1==var_2) return var_1; for(int i=m-1;i>=0;i--){ if(parent[var_1][i]!=parent[var_2][i]){ var_1=parent[var_1][i]; var_2=parent[var_2][i]; } } return parent[var_1][0]; } int back(int var_1,int dif){ for(int i=0;i<m;i++){ if(dif==0) break; if(dif&1){ var_1=parent[var_1][i]; } dif>>=1; } return var_1; } int distance(int var_1,int var_2){ int ascent=lca(var_1,var_2); return deep[var_1]+deep[var_2]-2*deep[ascent]; } }; } using po167::LCA; namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace atcoder; using lazy_S = pair<ll,ll>; using lazy_F = pair<ll,ll>; lazy_S lazy_op(lazy_S l, lazy_S r) { return lazy_S{ (l.first*r.first)%mod,(l.second*r.first+r.second)%mod }; } lazy_S lazy_e() { return lazy_S{1, 0}; } lazy_S mapping(lazy_F l, lazy_S r) { return lazy_op(r,l); } //l(r(x)) lazy_F composition(lazy_F l, lazy_F r) { return lazy_op(r,l); } lazy_F lazy_id(){return {1,0};} #define lazy_calc lazy_S,lazy_op,lazy_e,lazy_F,mapping,composition,lazy_id std::vector<std::vector<int>> tree_in(int N){ std::vector<std::vector<int>> G(N); for(int i=0;i<N-1;i++){ int a;int b; cin>>a>>b; a--,b--; G[a].push_back(b); G[b].push_back(a); } return G; } std::tuple<std::vector<int>,std::vector<int>,std::vector<int>> tree_order_pare_depth(std::vector<std::vector<int>> &G,int root){ int n=G.size(); std::vector<int> order={root},pare(n,-1),depth(n); pare[root]=-2; for(int i=0;i<n;i++){ int a=order[i]; for(auto x:G[a]){ if(pare[x]==-1){ pare[x]=a; depth[x]=depth[a]+1; order.push_back(x); } } } return {order,pare,depth}; } std::vector<int> tree_diameter_path(std::vector<std::vector<int>> &G){ int n=G.size(); auto r=(std::get<0>(tree_order_pare_depth(G,0))).at(n-1); std::vector<int> order,pare,depth,ans; tie(order,pare,depth)=tree_order_pare_depth(G,r); int ind=order[n-1]; while(ind!=-2){ ans.push_back(ind); ind=pare[ind]; } return ans; } ll jyo(ll x,ll y,ll z){ ll H=y; //ここから ll a=1,b=(x%z+z)%z,c=1; while(H>0){ a*=2; if(H%a!=0){ H-=a/2; c*=b; c%=z; } b*=b; b%=z; } //ここまで return c; } void solve(); // oddloop int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t=1; //cin>>t; rep(i,0,t) solve(); } void solve(){ int N,Q; cin>>N>>Q; auto G=tree_in(N); LCA lc(0,G); vector<int> order,pare,depth; tie(order,pare,depth)=tree_order_pare_depth(G,0); vector<int> dp(N); rep(i,0,N){ int a=order[N-i-1]; for(auto x:G[a]) if(pare[x]==a) dp[a]+=dp[x]; dp[a]++; } vector<ll> X(N); rep(i,0,N) cin>>X[i]; int ind=0; vector<int> L(N),R(N); vector<int> rev(N); ind++; vector<vector<pair<int,int>>> ran_path(N); vector<vector<pair<int,int>>> ran_ar(N); ran_path[0]={{0,1}}; auto f=[&](auto self,int v)->void{ L[v]=ind; vector<int> q; for(auto x:G[v]){ if(pare[x]==v){ ran_path[x]=ran_path[v]; q.push_back(x); } } if(q.empty()){ R[v]=ind; return; } sort(all(q),[&](int l,int r){return dp[l]>dp[r];}); rev[q[0]]=ind; int len=ran_path[q[0]].size(); if(ran_path[q[0]][len-1].second==ind){ ran_path[q[0]][len-1].second++; } else ran_path[q[0]].push_back({ind,ind+1}); ind++; self(self,q[0]); rep(i,1,(int)q.size()){ rev[q[i]]=ind; ran_path[q[i]].push_back({ind,ind+1}); ind++; } rep(i,1,(int)q.size()){ self(self,q[i]); } R[v]=ind; }; f(f,0); rep(i,0,N){ vector<int> q; for(auto x:G[i]) q.push_back(rev[x]); q.push_back(rev[i]); So(q); int l=q[0],r=q[0]+1; rep(j,1,1+(int)q.size()){ if(j==(int)q.size()||r!=q[j]){ ran_ar[i].push_back({l,r}); if(j!=(int)q.size()) l=q[j],r=l+1; } else r++; } } lazy_segtree<lazy_calc> seg(N); int V,K,C,D; lazy_F val; rep(i,0,Q){ int t; cin>>t; if(t==1){ cin>>V; V--; auto tmp=seg.get(rev[V]); cout<<(X[V]*tmp.first+tmp.second)%mod<<"\n"; } if(t==2){ cin>>V>>K>>C>>D; V--; val={C,D}; for(auto x:ran_ar[V]){ seg.apply(x.first,x.second,val); } } if(t==3){ cin>>V>>C>>D; V--; val={C,D}; seg.apply(L[V],R[V],val); seg.apply(rev[V],val); } if(t==4){ cin>>V>>K>>C>>D; val={C,D}; V--,K--; for(auto x:ran_path[V]){ seg.apply(x.first,x.second,val); } for(auto x:ran_path[K]){ seg.apply(x.first,x.second,val); } ll r=jyo(C,mod-2,mod); val={r,(mod-((D*r)%mod))%mod}; V=lc.lca(V,K); rep(rp,0,2){ for(auto x:ran_path[V]){ seg.apply(x.first,x.second,val); } if(V) V=pare[V]; else break; } } } }