#include <bits/stdc++.h>
bool home = 1;
using namespace std;
typedef long long ll;
const int N=200000+7;
const int K=20;
int n;
int q;
int mod;
int h[N];
vector<int> g[N];
pair<int, int> rmq[K][2*N];
int lg[2*N];
int par[N];
int dep[N];
int first_euler[N];
int last_euler[N];
vector<int> la[N];
int top;
int verts[N];
int pos[N];
void build(int a,int p=-1){
par[a]=p;
la[dep[a]].push_back(a);
rmq[0][++top]={dep[a], a};
first_euler[a]=last_euler[a]=top;
for(auto&b:g[a]){
if(b==p) continue;
dep[b]=1+dep[a];
build(b,a);
rmq[0][++top]={dep[a], a};
last_euler[a]=top;
}
}
int get_lca(int a,int b){
if(first_euler[a]>last_euler[b]) swap(a,b);
a=first_euler[a];
b=last_euler[b];
assert(a<=b);
int k=lg[b-a+1];
return min(rmq[k][a],rmq[k][b-(1<<k)+1]).second;
}
int get_dist(int a,int b){
int c=get_lca(a,b);
return dep[a]+dep[b]-2*dep[c];
}
int cnt_at_dep_before(int dep,int when) {
int low=0,high=(int)la[dep].size()-1,sol=0;
while(low<=high){
int mid=(low+high)/2;
if(first_euler[la[dep][mid]]<=when){
sol=mid+1;
low=mid+1;
}else{
high=mid-1;
}
}
return sol;
}
int delta[N];
const int MX=40;
int mlt[N][MX+1];
void mulup(int &a,int b){
assert(0<=a&&a<mod);
assert(0<=b&&b<mod);
a=a*(ll)b%mod;
}
signed main() {
#ifdef ONLINE_JUDGE
home = 0;
#endif
for(int i=0;i<N;i++){
for(int j=0;j<=MX;j++){
mlt[i][j]=1;
}
}
home=0;
if (home) {
freopen("I_am_iron_man", "r", stdin);
}
else {
ios::sync_with_stdio(0); cin.tie(0);
}
cin>>n>>mod;
for (int i=1;i<n;i++) {
int a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
build(1);
for(int i=2;i<=top;i++) lg[i]=1+lg[i/2];
for (int k=1;(1<<k)<=top;k++) {
for(int i=1;i+(1<<k)-1<=top;i++){
rmq[k][i]=min(rmq[k-1][i],rmq[k-1][i+(1<<(k-1))]);
}
}
{
top=0;
for (int d=0;d<=n;d++){
delta[d]=top;
for(auto &i:la[d]){
verts[++top]=i;
pos[i]=top;
}
}
assert(top==n);
}
for (int i=1;i<=n;i++) {
cin>>h[pos[i]];
}
cin>>q;
while(q--){
int type;
cin>>type;
assert(type==1||type==2);
if(type==1){
int x,d,w;
cin>>x>>d>>w;
vector<pair<int, int>> prs;
int vertex=x;
while (vertex>=1&&d>=0) {
prs.push_back({vertex, d});
vertex=par[vertex];
d--;
}
reverse(prs.begin(),prs.end());
int maxdep=-1;
for (auto &it:prs){
vertex=it.first;
d=it.second;
for(int h=dep[vertex];h<=dep[vertex]+d;h++){
if(h>maxdep){
maxdep=h;
mulup(mlt[vertex][h-dep[vertex]],w);
}
}
}
}else{
int x,xinit;
cin>>x;
int sol=h[pos[x]];
int cur_dist=0;
while (x>=1&&cur_dist<=MX){
sol=sol*(ll)mlt[x][cur_dist]%mod;
x=par[x];
cur_dist++;
}
cout<<sol<<"\n";
}
}
}
Compilation message
sprinkler.cpp: In function 'int main()':
sprinkler.cpp:166:13: warning: unused variable 'xinit' [-Wunused-variable]
166 | int x,xinit;
| ^~~~~
sprinkler.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen("I_am_iron_man", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41812 KB |
Output is correct |
2 |
Correct |
22 ms |
41812 KB |
Output is correct |
3 |
Correct |
26 ms |
41828 KB |
Output is correct |
4 |
Correct |
24 ms |
42212 KB |
Output is correct |
5 |
Correct |
24 ms |
42108 KB |
Output is correct |
6 |
Correct |
25 ms |
42092 KB |
Output is correct |
7 |
Correct |
26 ms |
42188 KB |
Output is correct |
8 |
Correct |
24 ms |
42196 KB |
Output is correct |
9 |
Correct |
23 ms |
41864 KB |
Output is correct |
10 |
Correct |
25 ms |
41944 KB |
Output is correct |
11 |
Correct |
25 ms |
41812 KB |
Output is correct |
12 |
Correct |
24 ms |
41844 KB |
Output is correct |
13 |
Correct |
25 ms |
41804 KB |
Output is correct |
14 |
Correct |
30 ms |
41820 KB |
Output is correct |
15 |
Correct |
25 ms |
41812 KB |
Output is correct |
16 |
Correct |
25 ms |
41832 KB |
Output is correct |
17 |
Correct |
22 ms |
41836 KB |
Output is correct |
18 |
Correct |
23 ms |
41828 KB |
Output is correct |
19 |
Correct |
23 ms |
41804 KB |
Output is correct |
20 |
Correct |
22 ms |
41892 KB |
Output is correct |
21 |
Correct |
23 ms |
41820 KB |
Output is correct |
22 |
Correct |
22 ms |
41796 KB |
Output is correct |
23 |
Correct |
22 ms |
41836 KB |
Output is correct |
24 |
Correct |
25 ms |
41812 KB |
Output is correct |
25 |
Correct |
22 ms |
41812 KB |
Output is correct |
26 |
Correct |
23 ms |
41812 KB |
Output is correct |
27 |
Correct |
24 ms |
41892 KB |
Output is correct |
28 |
Correct |
23 ms |
41844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
41812 KB |
Output is correct |
2 |
Correct |
602 ms |
116696 KB |
Output is correct |
3 |
Correct |
518 ms |
113564 KB |
Output is correct |
4 |
Correct |
581 ms |
127708 KB |
Output is correct |
5 |
Correct |
509 ms |
115148 KB |
Output is correct |
6 |
Correct |
438 ms |
114876 KB |
Output is correct |
7 |
Correct |
399 ms |
115116 KB |
Output is correct |
8 |
Correct |
390 ms |
115460 KB |
Output is correct |
9 |
Correct |
645 ms |
136516 KB |
Output is correct |
10 |
Correct |
491 ms |
131672 KB |
Output is correct |
11 |
Correct |
654 ms |
116768 KB |
Output is correct |
12 |
Correct |
444 ms |
113416 KB |
Output is correct |
13 |
Correct |
337 ms |
114544 KB |
Output is correct |
14 |
Correct |
343 ms |
114060 KB |
Output is correct |
15 |
Correct |
319 ms |
113812 KB |
Output is correct |
16 |
Correct |
348 ms |
113816 KB |
Output is correct |
17 |
Correct |
330 ms |
114276 KB |
Output is correct |
18 |
Correct |
24 ms |
41836 KB |
Output is correct |
19 |
Correct |
25 ms |
41872 KB |
Output is correct |
20 |
Correct |
23 ms |
41888 KB |
Output is correct |
21 |
Correct |
24 ms |
41812 KB |
Output is correct |
22 |
Correct |
23 ms |
41908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
41812 KB |
Output is correct |
2 |
Correct |
602 ms |
116696 KB |
Output is correct |
3 |
Correct |
518 ms |
113564 KB |
Output is correct |
4 |
Correct |
581 ms |
127708 KB |
Output is correct |
5 |
Correct |
509 ms |
115148 KB |
Output is correct |
6 |
Correct |
438 ms |
114876 KB |
Output is correct |
7 |
Correct |
399 ms |
115116 KB |
Output is correct |
8 |
Correct |
390 ms |
115460 KB |
Output is correct |
9 |
Correct |
645 ms |
136516 KB |
Output is correct |
10 |
Correct |
491 ms |
131672 KB |
Output is correct |
11 |
Correct |
654 ms |
116768 KB |
Output is correct |
12 |
Correct |
444 ms |
113416 KB |
Output is correct |
13 |
Correct |
337 ms |
114544 KB |
Output is correct |
14 |
Correct |
343 ms |
114060 KB |
Output is correct |
15 |
Correct |
319 ms |
113812 KB |
Output is correct |
16 |
Correct |
348 ms |
113816 KB |
Output is correct |
17 |
Correct |
330 ms |
114276 KB |
Output is correct |
18 |
Correct |
24 ms |
41836 KB |
Output is correct |
19 |
Correct |
25 ms |
41872 KB |
Output is correct |
20 |
Correct |
23 ms |
41888 KB |
Output is correct |
21 |
Correct |
24 ms |
41812 KB |
Output is correct |
22 |
Correct |
23 ms |
41908 KB |
Output is correct |
23 |
Correct |
23 ms |
41764 KB |
Output is correct |
24 |
Correct |
582 ms |
116544 KB |
Output is correct |
25 |
Correct |
474 ms |
123596 KB |
Output is correct |
26 |
Correct |
579 ms |
143436 KB |
Output is correct |
27 |
Correct |
528 ms |
123916 KB |
Output is correct |
28 |
Correct |
424 ms |
124364 KB |
Output is correct |
29 |
Correct |
416 ms |
123976 KB |
Output is correct |
30 |
Correct |
358 ms |
124252 KB |
Output is correct |
31 |
Correct |
720 ms |
137548 KB |
Output is correct |
32 |
Correct |
523 ms |
142132 KB |
Output is correct |
33 |
Correct |
683 ms |
124108 KB |
Output is correct |
34 |
Correct |
613 ms |
124268 KB |
Output is correct |
35 |
Correct |
27 ms |
41936 KB |
Output is correct |
36 |
Correct |
24 ms |
41812 KB |
Output is correct |
37 |
Correct |
23 ms |
41844 KB |
Output is correct |
38 |
Correct |
24 ms |
41816 KB |
Output is correct |
39 |
Correct |
24 ms |
41844 KB |
Output is correct |
40 |
Correct |
23 ms |
41832 KB |
Output is correct |
41 |
Correct |
23 ms |
41924 KB |
Output is correct |
42 |
Correct |
23 ms |
41880 KB |
Output is correct |
43 |
Correct |
26 ms |
41812 KB |
Output is correct |
44 |
Correct |
24 ms |
41812 KB |
Output is correct |
45 |
Correct |
24 ms |
41804 KB |
Output is correct |
46 |
Correct |
28 ms |
41812 KB |
Output is correct |
47 |
Correct |
25 ms |
41824 KB |
Output is correct |
48 |
Correct |
26 ms |
41864 KB |
Output is correct |
49 |
Correct |
22 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41868 KB |
Output is correct |
2 |
Correct |
879 ms |
133776 KB |
Output is correct |
3 |
Correct |
2353 ms |
127884 KB |
Output is correct |
4 |
Correct |
1002 ms |
129200 KB |
Output is correct |
5 |
Correct |
873 ms |
113696 KB |
Output is correct |
6 |
Correct |
565 ms |
113716 KB |
Output is correct |
7 |
Correct |
519 ms |
113720 KB |
Output is correct |
8 |
Correct |
339 ms |
113876 KB |
Output is correct |
9 |
Correct |
841 ms |
127628 KB |
Output is correct |
10 |
Correct |
2310 ms |
131148 KB |
Output is correct |
11 |
Correct |
752 ms |
113772 KB |
Output is correct |
12 |
Correct |
1713 ms |
113336 KB |
Output is correct |
13 |
Correct |
1088 ms |
113968 KB |
Output is correct |
14 |
Correct |
1148 ms |
114924 KB |
Output is correct |
15 |
Correct |
24 ms |
41812 KB |
Output is correct |
16 |
Correct |
23 ms |
41780 KB |
Output is correct |
17 |
Correct |
24 ms |
41808 KB |
Output is correct |
18 |
Correct |
24 ms |
41824 KB |
Output is correct |
19 |
Correct |
24 ms |
41788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41868 KB |
Output is correct |
2 |
Correct |
896 ms |
129776 KB |
Output is correct |
3 |
Correct |
2429 ms |
131744 KB |
Output is correct |
4 |
Correct |
1000 ms |
134372 KB |
Output is correct |
5 |
Correct |
872 ms |
122908 KB |
Output is correct |
6 |
Correct |
595 ms |
122744 KB |
Output is correct |
7 |
Correct |
542 ms |
122420 KB |
Output is correct |
8 |
Correct |
369 ms |
122752 KB |
Output is correct |
9 |
Correct |
952 ms |
142152 KB |
Output is correct |
10 |
Correct |
2423 ms |
140012 KB |
Output is correct |
11 |
Correct |
781 ms |
123396 KB |
Output is correct |
12 |
Correct |
1701 ms |
121944 KB |
Output is correct |
13 |
Correct |
1099 ms |
122768 KB |
Output is correct |
14 |
Correct |
1163 ms |
123676 KB |
Output is correct |
15 |
Correct |
25 ms |
41812 KB |
Output is correct |
16 |
Correct |
27 ms |
41832 KB |
Output is correct |
17 |
Correct |
25 ms |
41788 KB |
Output is correct |
18 |
Correct |
24 ms |
41816 KB |
Output is correct |
19 |
Correct |
24 ms |
41836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
41812 KB |
Output is correct |
2 |
Correct |
22 ms |
41812 KB |
Output is correct |
3 |
Correct |
26 ms |
41828 KB |
Output is correct |
4 |
Correct |
24 ms |
42212 KB |
Output is correct |
5 |
Correct |
24 ms |
42108 KB |
Output is correct |
6 |
Correct |
25 ms |
42092 KB |
Output is correct |
7 |
Correct |
26 ms |
42188 KB |
Output is correct |
8 |
Correct |
24 ms |
42196 KB |
Output is correct |
9 |
Correct |
23 ms |
41864 KB |
Output is correct |
10 |
Correct |
25 ms |
41944 KB |
Output is correct |
11 |
Correct |
25 ms |
41812 KB |
Output is correct |
12 |
Correct |
24 ms |
41844 KB |
Output is correct |
13 |
Correct |
25 ms |
41804 KB |
Output is correct |
14 |
Correct |
30 ms |
41820 KB |
Output is correct |
15 |
Correct |
25 ms |
41812 KB |
Output is correct |
16 |
Correct |
25 ms |
41832 KB |
Output is correct |
17 |
Correct |
22 ms |
41836 KB |
Output is correct |
18 |
Correct |
23 ms |
41828 KB |
Output is correct |
19 |
Correct |
23 ms |
41804 KB |
Output is correct |
20 |
Correct |
22 ms |
41892 KB |
Output is correct |
21 |
Correct |
23 ms |
41820 KB |
Output is correct |
22 |
Correct |
22 ms |
41796 KB |
Output is correct |
23 |
Correct |
22 ms |
41836 KB |
Output is correct |
24 |
Correct |
25 ms |
41812 KB |
Output is correct |
25 |
Correct |
22 ms |
41812 KB |
Output is correct |
26 |
Correct |
23 ms |
41812 KB |
Output is correct |
27 |
Correct |
24 ms |
41892 KB |
Output is correct |
28 |
Correct |
23 ms |
41844 KB |
Output is correct |
29 |
Correct |
22 ms |
41812 KB |
Output is correct |
30 |
Correct |
602 ms |
116696 KB |
Output is correct |
31 |
Correct |
518 ms |
113564 KB |
Output is correct |
32 |
Correct |
581 ms |
127708 KB |
Output is correct |
33 |
Correct |
509 ms |
115148 KB |
Output is correct |
34 |
Correct |
438 ms |
114876 KB |
Output is correct |
35 |
Correct |
399 ms |
115116 KB |
Output is correct |
36 |
Correct |
390 ms |
115460 KB |
Output is correct |
37 |
Correct |
645 ms |
136516 KB |
Output is correct |
38 |
Correct |
491 ms |
131672 KB |
Output is correct |
39 |
Correct |
654 ms |
116768 KB |
Output is correct |
40 |
Correct |
444 ms |
113416 KB |
Output is correct |
41 |
Correct |
337 ms |
114544 KB |
Output is correct |
42 |
Correct |
343 ms |
114060 KB |
Output is correct |
43 |
Correct |
319 ms |
113812 KB |
Output is correct |
44 |
Correct |
348 ms |
113816 KB |
Output is correct |
45 |
Correct |
330 ms |
114276 KB |
Output is correct |
46 |
Correct |
24 ms |
41836 KB |
Output is correct |
47 |
Correct |
25 ms |
41872 KB |
Output is correct |
48 |
Correct |
23 ms |
41888 KB |
Output is correct |
49 |
Correct |
24 ms |
41812 KB |
Output is correct |
50 |
Correct |
23 ms |
41908 KB |
Output is correct |
51 |
Correct |
23 ms |
41764 KB |
Output is correct |
52 |
Correct |
582 ms |
116544 KB |
Output is correct |
53 |
Correct |
474 ms |
123596 KB |
Output is correct |
54 |
Correct |
579 ms |
143436 KB |
Output is correct |
55 |
Correct |
528 ms |
123916 KB |
Output is correct |
56 |
Correct |
424 ms |
124364 KB |
Output is correct |
57 |
Correct |
416 ms |
123976 KB |
Output is correct |
58 |
Correct |
358 ms |
124252 KB |
Output is correct |
59 |
Correct |
720 ms |
137548 KB |
Output is correct |
60 |
Correct |
523 ms |
142132 KB |
Output is correct |
61 |
Correct |
683 ms |
124108 KB |
Output is correct |
62 |
Correct |
613 ms |
124268 KB |
Output is correct |
63 |
Correct |
27 ms |
41936 KB |
Output is correct |
64 |
Correct |
24 ms |
41812 KB |
Output is correct |
65 |
Correct |
23 ms |
41844 KB |
Output is correct |
66 |
Correct |
24 ms |
41816 KB |
Output is correct |
67 |
Correct |
24 ms |
41844 KB |
Output is correct |
68 |
Correct |
23 ms |
41832 KB |
Output is correct |
69 |
Correct |
23 ms |
41924 KB |
Output is correct |
70 |
Correct |
23 ms |
41880 KB |
Output is correct |
71 |
Correct |
26 ms |
41812 KB |
Output is correct |
72 |
Correct |
24 ms |
41812 KB |
Output is correct |
73 |
Correct |
24 ms |
41804 KB |
Output is correct |
74 |
Correct |
28 ms |
41812 KB |
Output is correct |
75 |
Correct |
25 ms |
41824 KB |
Output is correct |
76 |
Correct |
26 ms |
41864 KB |
Output is correct |
77 |
Correct |
22 ms |
41848 KB |
Output is correct |
78 |
Correct |
23 ms |
41868 KB |
Output is correct |
79 |
Correct |
879 ms |
133776 KB |
Output is correct |
80 |
Correct |
2353 ms |
127884 KB |
Output is correct |
81 |
Correct |
1002 ms |
129200 KB |
Output is correct |
82 |
Correct |
873 ms |
113696 KB |
Output is correct |
83 |
Correct |
565 ms |
113716 KB |
Output is correct |
84 |
Correct |
519 ms |
113720 KB |
Output is correct |
85 |
Correct |
339 ms |
113876 KB |
Output is correct |
86 |
Correct |
841 ms |
127628 KB |
Output is correct |
87 |
Correct |
2310 ms |
131148 KB |
Output is correct |
88 |
Correct |
752 ms |
113772 KB |
Output is correct |
89 |
Correct |
1713 ms |
113336 KB |
Output is correct |
90 |
Correct |
1088 ms |
113968 KB |
Output is correct |
91 |
Correct |
1148 ms |
114924 KB |
Output is correct |
92 |
Correct |
24 ms |
41812 KB |
Output is correct |
93 |
Correct |
23 ms |
41780 KB |
Output is correct |
94 |
Correct |
24 ms |
41808 KB |
Output is correct |
95 |
Correct |
24 ms |
41824 KB |
Output is correct |
96 |
Correct |
24 ms |
41788 KB |
Output is correct |
97 |
Correct |
23 ms |
41868 KB |
Output is correct |
98 |
Correct |
896 ms |
129776 KB |
Output is correct |
99 |
Correct |
2429 ms |
131744 KB |
Output is correct |
100 |
Correct |
1000 ms |
134372 KB |
Output is correct |
101 |
Correct |
872 ms |
122908 KB |
Output is correct |
102 |
Correct |
595 ms |
122744 KB |
Output is correct |
103 |
Correct |
542 ms |
122420 KB |
Output is correct |
104 |
Correct |
369 ms |
122752 KB |
Output is correct |
105 |
Correct |
952 ms |
142152 KB |
Output is correct |
106 |
Correct |
2423 ms |
140012 KB |
Output is correct |
107 |
Correct |
781 ms |
123396 KB |
Output is correct |
108 |
Correct |
1701 ms |
121944 KB |
Output is correct |
109 |
Correct |
1099 ms |
122768 KB |
Output is correct |
110 |
Correct |
1163 ms |
123676 KB |
Output is correct |
111 |
Correct |
25 ms |
41812 KB |
Output is correct |
112 |
Correct |
27 ms |
41832 KB |
Output is correct |
113 |
Correct |
25 ms |
41788 KB |
Output is correct |
114 |
Correct |
24 ms |
41816 KB |
Output is correct |
115 |
Correct |
24 ms |
41836 KB |
Output is correct |
116 |
Correct |
774 ms |
121868 KB |
Output is correct |
117 |
Correct |
1792 ms |
124604 KB |
Output is correct |
118 |
Correct |
1058 ms |
142444 KB |
Output is correct |
119 |
Correct |
901 ms |
124108 KB |
Output is correct |
120 |
Correct |
622 ms |
123628 KB |
Output is correct |
121 |
Correct |
620 ms |
124312 KB |
Output is correct |
122 |
Correct |
422 ms |
124288 KB |
Output is correct |
123 |
Correct |
959 ms |
139624 KB |
Output is correct |
124 |
Correct |
2406 ms |
136044 KB |
Output is correct |
125 |
Correct |
788 ms |
122548 KB |
Output is correct |
126 |
Correct |
1842 ms |
124364 KB |
Output is correct |
127 |
Correct |
1873 ms |
124988 KB |
Output is correct |
128 |
Correct |
1159 ms |
122044 KB |
Output is correct |
129 |
Correct |
1238 ms |
114668 KB |
Output is correct |