Submission #550793

# Submission time Handle Problem Language Result Execution time Memory
550793 2022-04-19T08:25:43 Z inksamurai Sprinkler (JOI22_sprinkler) C++17
42 / 100
1450 ms 97432 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

int mod;
//snuke's mod int
// template <ll mod>
struct modint{
	ll x; // typedef long long ll;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};

signed main(){
_3xxEYjy;
	int n;
	cin>>n>>mod;
	vec(vi) adj(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u-=1;v-=1;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vi par;
	{
		rep(i,42){
			adj.pb({});
			if(i==0){
				adj[n].pb(0);
				adj[0].pb(n);
			}else{
				adj[n-1].pb(n);
				adj[n].pb(n-1);
			}
			n+=1;
		}
		par=vi(n,-1);
		auto dfs=[&](auto self,int v,int _par)->void{
			for(auto u:adj[v]){
				if(u==_par) continue;
				par[u]=v;
				self(self,u,v);
			}
		};dfs(dfs,n-1,-1);
		while(sz(a)<n){
			a.pb(0);
		}
	}

	using mint=modint;

	vec(vec(mint)) dp(n,vec(mint)(42,1));

	auto add=[&](int v,int d,int x){
		if(d==0){
			a[v]*=x%mod;
			a[v]%=mod;
			return;
		}
		while(1){
			if(v==-1) break;
			if(d==0){
				a[v]*=x;
				a[v]%=mod;
				break;
			}
			dp[v][d]*=mint(x);
			d-=1;
			v=par[v];
		}
	};

	auto get=[&](int v)->mint{
		int d=0;
		mint res=1;
		while(d<=40){
			res*=dp[v][d]*dp[v][d+1];
			v=par[v];
			if(v==-1) break;
			d+=1;
		}
		return res;
	};

	int q;
	cin>>q;
	rep(_,q){
		int t,v,x,d;
		cin>>t;
		if(t==1){
			cin>>v>>d>>x;
			v-=1;
			add(v,d,x);
		}else{
			cin>>v;
			v-=1;
			mint res=get(v);
			res*=mint(a[v]);
			print(res.x);
		}
	}	
//
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Incorrect 2 ms 724 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 753 ms 91848 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 753 ms 91848 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 945 ms 95108 KB Output is correct
3 Correct 1258 ms 92388 KB Output is correct
4 Correct 871 ms 93252 KB Output is correct
5 Correct 712 ms 88492 KB Output is correct
6 Correct 535 ms 88676 KB Output is correct
7 Correct 460 ms 88760 KB Output is correct
8 Correct 401 ms 89056 KB Output is correct
9 Correct 924 ms 93084 KB Output is correct
10 Correct 1259 ms 93296 KB Output is correct
11 Correct 808 ms 88788 KB Output is correct
12 Correct 1020 ms 87920 KB Output is correct
13 Correct 617 ms 88924 KB Output is correct
14 Correct 744 ms 89460 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1004 ms 95864 KB Output is correct
3 Correct 1376 ms 91560 KB Output is correct
4 Correct 1008 ms 93708 KB Output is correct
5 Correct 843 ms 90184 KB Output is correct
6 Correct 606 ms 90368 KB Output is correct
7 Correct 561 ms 90284 KB Output is correct
8 Correct 436 ms 90744 KB Output is correct
9 Correct 1068 ms 97432 KB Output is correct
10 Correct 1450 ms 94020 KB Output is correct
11 Correct 822 ms 91908 KB Output is correct
12 Correct 948 ms 88488 KB Output is correct
13 Correct 644 ms 89152 KB Output is correct
14 Correct 609 ms 89504 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Incorrect 2 ms 724 KB Output isn't correct
5 Halted 0 ms 0 KB -