답안 #550796

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
550796 2022-04-19T08:26:34 Z inksamurai Sprinkler (JOI22_sprinkler) C++17
100 / 100
1227 ms 99856 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);
	}
	vec(ll) 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 2 ms 824 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 821 ms 93468 KB Output is correct
3 Correct 564 ms 90440 KB Output is correct
4 Correct 757 ms 96164 KB Output is correct
5 Correct 747 ms 92240 KB Output is correct
6 Correct 554 ms 92116 KB Output is correct
7 Correct 483 ms 92516 KB Output is correct
8 Correct 443 ms 92852 KB Output is correct
9 Correct 843 ms 99856 KB Output is correct
10 Correct 461 ms 96036 KB Output is correct
11 Correct 758 ms 93916 KB Output is correct
12 Correct 456 ms 90436 KB Output is correct
13 Correct 349 ms 91032 KB Output is correct
14 Correct 291 ms 91112 KB Output is correct
15 Correct 299 ms 89676 KB Output is correct
16 Correct 301 ms 90992 KB Output is correct
17 Correct 297 ms 91504 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 821 ms 93468 KB Output is correct
3 Correct 564 ms 90440 KB Output is correct
4 Correct 757 ms 96164 KB Output is correct
5 Correct 747 ms 92240 KB Output is correct
6 Correct 554 ms 92116 KB Output is correct
7 Correct 483 ms 92516 KB Output is correct
8 Correct 443 ms 92852 KB Output is correct
9 Correct 843 ms 99856 KB Output is correct
10 Correct 461 ms 96036 KB Output is correct
11 Correct 758 ms 93916 KB Output is correct
12 Correct 456 ms 90436 KB Output is correct
13 Correct 349 ms 91032 KB Output is correct
14 Correct 291 ms 91112 KB Output is correct
15 Correct 299 ms 89676 KB Output is correct
16 Correct 301 ms 90992 KB Output is correct
17 Correct 297 ms 91504 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 791 ms 94004 KB Output is correct
25 Correct 422 ms 90396 KB Output is correct
26 Correct 691 ms 98004 KB Output is correct
27 Correct 602 ms 92184 KB Output is correct
28 Correct 479 ms 92320 KB Output is correct
29 Correct 436 ms 92308 KB Output is correct
30 Correct 411 ms 92920 KB Output is correct
31 Correct 892 ms 97984 KB Output is correct
32 Correct 487 ms 96168 KB Output is correct
33 Correct 741 ms 93940 KB Output is correct
34 Correct 452 ms 90440 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 324 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 944 ms 96776 KB Output is correct
3 Correct 1227 ms 94420 KB Output is correct
4 Correct 831 ms 94964 KB Output is correct
5 Correct 710 ms 90332 KB Output is correct
6 Correct 482 ms 90368 KB Output is correct
7 Correct 498 ms 90452 KB Output is correct
8 Correct 382 ms 90920 KB Output is correct
9 Correct 904 ms 95056 KB Output is correct
10 Correct 1209 ms 95476 KB Output is correct
11 Correct 722 ms 90780 KB Output is correct
12 Correct 820 ms 89784 KB Output is correct
13 Correct 513 ms 90132 KB Output is correct
14 Correct 537 ms 90072 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 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 927 ms 96636 KB Output is correct
3 Correct 1187 ms 92100 KB Output is correct
4 Correct 851 ms 94312 KB Output is correct
5 Correct 772 ms 90772 KB Output is correct
6 Correct 513 ms 90948 KB Output is correct
7 Correct 526 ms 90780 KB Output is correct
8 Correct 429 ms 91336 KB Output is correct
9 Correct 885 ms 98216 KB Output is correct
10 Correct 1185 ms 94780 KB Output is correct
11 Correct 768 ms 92540 KB Output is correct
12 Correct 874 ms 89044 KB Output is correct
13 Correct 539 ms 90020 KB Output is correct
14 Correct 532 ms 90324 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 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 2 ms 824 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 320 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 821 ms 93468 KB Output is correct
31 Correct 564 ms 90440 KB Output is correct
32 Correct 757 ms 96164 KB Output is correct
33 Correct 747 ms 92240 KB Output is correct
34 Correct 554 ms 92116 KB Output is correct
35 Correct 483 ms 92516 KB Output is correct
36 Correct 443 ms 92852 KB Output is correct
37 Correct 843 ms 99856 KB Output is correct
38 Correct 461 ms 96036 KB Output is correct
39 Correct 758 ms 93916 KB Output is correct
40 Correct 456 ms 90436 KB Output is correct
41 Correct 349 ms 91032 KB Output is correct
42 Correct 291 ms 91112 KB Output is correct
43 Correct 299 ms 89676 KB Output is correct
44 Correct 301 ms 90992 KB Output is correct
45 Correct 297 ms 91504 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 324 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 791 ms 94004 KB Output is correct
53 Correct 422 ms 90396 KB Output is correct
54 Correct 691 ms 98004 KB Output is correct
55 Correct 602 ms 92184 KB Output is correct
56 Correct 479 ms 92320 KB Output is correct
57 Correct 436 ms 92308 KB Output is correct
58 Correct 411 ms 92920 KB Output is correct
59 Correct 892 ms 97984 KB Output is correct
60 Correct 487 ms 96168 KB Output is correct
61 Correct 741 ms 93940 KB Output is correct
62 Correct 452 ms 90440 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 2 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 1 ms 324 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 2 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 1 ms 340 KB Output is correct
77 Correct 1 ms 320 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 944 ms 96776 KB Output is correct
80 Correct 1227 ms 94420 KB Output is correct
81 Correct 831 ms 94964 KB Output is correct
82 Correct 710 ms 90332 KB Output is correct
83 Correct 482 ms 90368 KB Output is correct
84 Correct 498 ms 90452 KB Output is correct
85 Correct 382 ms 90920 KB Output is correct
86 Correct 904 ms 95056 KB Output is correct
87 Correct 1209 ms 95476 KB Output is correct
88 Correct 722 ms 90780 KB Output is correct
89 Correct 820 ms 89784 KB Output is correct
90 Correct 513 ms 90132 KB Output is correct
91 Correct 537 ms 90072 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 320 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 1 ms 320 KB Output is correct
98 Correct 927 ms 96636 KB Output is correct
99 Correct 1187 ms 92100 KB Output is correct
100 Correct 851 ms 94312 KB Output is correct
101 Correct 772 ms 90772 KB Output is correct
102 Correct 513 ms 90948 KB Output is correct
103 Correct 526 ms 90780 KB Output is correct
104 Correct 429 ms 91336 KB Output is correct
105 Correct 885 ms 98216 KB Output is correct
106 Correct 1185 ms 94780 KB Output is correct
107 Correct 768 ms 92540 KB Output is correct
108 Correct 874 ms 89044 KB Output is correct
109 Correct 539 ms 90020 KB Output is correct
110 Correct 532 ms 90324 KB Output is correct
111 Correct 1 ms 340 KB Output is correct
112 Correct 1 ms 340 KB Output is correct
113 Correct 1 ms 340 KB Output is correct
114 Correct 1 ms 340 KB Output is correct
115 Correct 1 ms 340 KB Output is correct
116 Correct 734 ms 91300 KB Output is correct
117 Correct 900 ms 89720 KB Output is correct
118 Correct 863 ms 97256 KB Output is correct
119 Correct 739 ms 91712 KB Output is correct
120 Correct 538 ms 91472 KB Output is correct
121 Correct 527 ms 91860 KB Output is correct
122 Correct 452 ms 92200 KB Output is correct
123 Correct 868 ms 97924 KB Output is correct
124 Correct 1201 ms 93536 KB Output is correct
125 Correct 738 ms 92428 KB Output is correct
126 Correct 878 ms 89280 KB Output is correct
127 Correct 869 ms 89132 KB Output is correct
128 Correct 537 ms 89860 KB Output is correct
129 Correct 547 ms 88860 KB Output is correct