結果

問題 No.2341 Triple Tree Query (Medium)
ユーザー 👑 potato167potato167
提出日時 2023-06-02 23:50:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 676 ms / 5,000 ms
コード長 12,476 bytes
コンパイル時間 4,400 ms
コンパイル使用メモリ 251,108 KB
実行使用メモリ 64,536 KB
最終ジャッジ日時 2023-08-28 06:39:12
合計ジャッジ時間 24,689 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 672 ms
48,380 KB
testcase_03 AC 669 ms
48,548 KB
testcase_04 AC 665 ms
47,972 KB
testcase_05 AC 673 ms
48,492 KB
testcase_06 AC 676 ms
48,816 KB
testcase_07 AC 499 ms
59,636 KB
testcase_08 AC 499 ms
56,892 KB
testcase_09 AC 494 ms
64,536 KB
testcase_10 AC 485 ms
59,932 KB
testcase_11 AC 486 ms
62,788 KB
testcase_12 AC 509 ms
56,048 KB
testcase_13 AC 503 ms
59,040 KB
testcase_14 AC 511 ms
58,040 KB
testcase_15 AC 503 ms
62,980 KB
testcase_16 AC 513 ms
54,400 KB
testcase_17 AC 391 ms
41,676 KB
testcase_18 AC 388 ms
41,908 KB
testcase_19 AC 387 ms
41,860 KB
testcase_20 AC 386 ms
41,868 KB
testcase_21 AC 385 ms
41,952 KB
testcase_22 AC 383 ms
41,856 KB
testcase_23 AC 380 ms
41,820 KB
testcase_24 AC 392 ms
41,636 KB
testcase_25 AC 556 ms
43,004 KB
testcase_26 AC 553 ms
42,712 KB
testcase_27 AC 554 ms
42,848 KB
testcase_28 AC 564 ms
42,620 KB
testcase_29 AC 558 ms
42,632 KB
testcase_30 AC 393 ms
41,696 KB
testcase_31 AC 388 ms
41,964 KB
testcase_32 AC 393 ms
41,852 KB
testcase_33 AC 498 ms
43,440 KB
testcase_34 AC 497 ms
43,456 KB
testcase_35 AC 501 ms
44,016 KB
testcase_36 AC 508 ms
44,132 KB
testcase_37 AC 509 ms
43,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
			}
		}
	}
}
0