結果

問題 No.2340 Triple Tree Query (Easy)
ユーザー 👑 potato167potato167
提出日時 2023-06-02 23:41:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 5,000 ms
コード長 9,887 bytes
コンパイル時間 4,132 ms
コンパイル使用メモリ 257,872 KB
実行使用メモリ 65,236 KB
最終ジャッジ日時 2024-06-09 02:08:28
合計ジャッジ時間 16,516 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 7 ms
5,376 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 7 ms
5,376 KB
testcase_04 AC 7 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 332 ms
30,532 KB
testcase_07 AC 295 ms
30,528 KB
testcase_08 AC 305 ms
30,404 KB
testcase_09 AC 300 ms
30,408 KB
testcase_10 AC 305 ms
30,540 KB
testcase_11 AC 295 ms
30,436 KB
testcase_12 AC 308 ms
30,532 KB
testcase_13 AC 299 ms
30,660 KB
testcase_14 AC 293 ms
30,532 KB
testcase_15 AC 300 ms
30,408 KB
testcase_16 AC 338 ms
65,236 KB
testcase_17 AC 323 ms
53,976 KB
testcase_18 AC 315 ms
51,800 KB
testcase_19 AC 343 ms
62,040 KB
testcase_20 AC 316 ms
51,160 KB
testcase_21 AC 257 ms
30,412 KB
testcase_22 AC 257 ms
30,356 KB
testcase_23 AC 254 ms
30,408 KB
testcase_24 AC 287 ms
31,448 KB
testcase_25 AC 286 ms
30,680 KB
testcase_26 AC 286 ms
30,808 KB
testcase_27 AC 285 ms
30,876 KB
testcase_28 AC 290 ms
30,936 KB
testcase_29 AC 256 ms
30,504 KB
testcase_30 AC 246 ms
30,392 KB
testcase_31 AC 257 ms
30,560 KB
testcase_32 AC 292 ms
30,708 KB
testcase_33 AC 292 ms
30,712 KB
testcase_34 AC 285 ms
30,556 KB
testcase_35 AC 293 ms
30,588 KB
testcase_36 AC 292 ms
30,576 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;}


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

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);
	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++;
	auto f=[&](auto self,int v)->void{
		L[v]=ind;
		vector<int> q;
		for(auto x:G[v]){
			if(pare[x]==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;
		ind++;
		self(self,q[0]);
		rep(i,1,(int)q.size()){
			rev[q[i]]=ind;
			ind++;
		}
		rep(i,1,(int)q.size()){
			self(self,q[i]);
		}
		R[v]=ind;
	};
	vector<set<pair<int,int>>> ran_path(N);
	vector<vector<pair<int,int>>> ran_ar(N);
	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);
		}
	}
}
0