# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
615124 |
2022-07-31T07:00:56 Z |
이동현(#8495) |
Sprinkler (JOI22_sprinkler) |
C++17 |
|
4000 ms |
572720 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int NS = (int)2e5 + 4;
int n, mod;
vector<int> way[NS], cway[NS];
int dep[NS], sz[NS], megop[NS][44], ind[NS];
int lca[NS][20], cchk[NS], init[NS];
int cpr[NS];
struct Seg{
int n;
vector<int> tr;
Seg(){}
Seg(int n):n(n + 4){
tr.resize(n * 4, 1);
}
void push(int x, int s, int e, int pos, int val){
if(s == e){
tr[x] = ((long long)tr[x] * val) % mod;
return;
}
int m = s + e >> 1;
if(pos <= m){
push(x * 2, s, m, pos, val);
}
else{
push(x * 2 + 1, m + 1, e, pos, val);
}
tr[x] = ((long long)tr[x * 2] * tr[x * 2 + 1]) % mod;
}
int get(int x, int s, int e, int fs, int fe){
if(fe < s || fs > e || fs > fe) return 1;
if(fs <= s && fe >= e){
return tr[x];
}
int m = s + e >> 1;
return (long long)get(x * 2, s, m, fs, fe) * get(x * 2 + 1, m + 1, e, fs, fe) % mod;
}
}tree[NS][44];
void dfslca(int x, int pr){
lca[x][0] = pr;
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
dep[nxt] = dep[x] + 1;
dfslca(nxt, x);
}
}
void dfssz(int x, int pr = -1){
sz[x] = 1;
for(auto&nxt:way[x]){
if(nxt == pr || cchk[nxt]){
continue;
}
dfssz(nxt, x);
sz[x] += sz[nxt];
}
}
int getcent(int x, int allsz, int pr = -1){
for(auto&nxt:way[x]){
if(nxt == pr || cchk[nxt]){
continue;
}
if(sz[nxt] * 2 > allsz){
return getcent(nxt, allsz, x);
}
}
return x;
}
int docent(int x){
dfssz(x);
x = getcent(x, sz[x]);
cchk[x] = 1;
for(auto&nxt:way[x]){
if(cchk[nxt]){
continue;
}
int rv = docent(nxt);
ind[rv] = (int)cway[x].size();
cway[x].push_back(rv);
cpr[rv] = x;
}
return x;
}
int getdist(int x, int y){
int xs = x, ys = y;
if(dep[x] < dep[y]){
swap(x, y);
}
for(int i = 19; i >= 0; --i){
if(dep[x] - (1 << i) >= dep[y]){
x = lca[x][i];
}
}
if(x != y){
for(int i = 19; i >= 0; --i){
if(lca[x][i] != lca[y][i]){
x = lca[x][i];
y = lca[y][i];
}
}
x = lca[x][0];
}
return dep[xs] + dep[ys] - 2 * dep[x];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> mod;
for(int i = 1; i < n; ++i){
int x, y; cin >> x >> y;
way[x].push_back(y), way[y].push_back(x);
}
for(int i = 1; i <= n; ++i){
cin >> init[i];
}
dfslca(1, 0);
for(int j = 1; j < 20; ++j){
for(int i = 1; i <= n; ++i){
lca[i][j] = lca[lca[i][j - 1]][j - 1];
}
}
int top = docent(1);
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= 40; ++j){
tree[i][j] = Seg((int)cway[i].size());
megop[i][j] = 1;
}
}
// for(int i = 1; i <= n; ++i){
// for(auto&nxt:cway[i]){
// cout << i << ' ' << nxt << endl;
// }
// }
int q; cin >> q;
while(q--){
int t; cin >> t;
if(t == 1){
int x, d, w; cin >> x >> d >> w;
int now = x;
for(int i = 0; i <= d; ++i){
megop[now][i] = ((long long)megop[now][i] * w) % mod;
}
while(now != top){
int up = cpr[now];
int dist = getdist(x, up);
if(d >= dist){
for(int i = 0; i <= d - dist; ++i){
tree[up][i].push(1, 0, (int)cway[up].size() - 1, ind[now], w);
}
}
now = up;
}
}
else{
int x; cin >> x;
int ans = init[x];
int now = x;
ans = ((long long)ans * tree[now][0].get(1, 0, (int)cway[now].size() - 1, 0, (int)cway[now].size() - 1)) % mod;
while(true){
int dist = getdist(x, now);
if(dist <= 40){
ans = ((long long)ans * megop[now][dist]) % mod;
}
if(now == top) break;
int up = cpr[now];
dist = getdist(x, up);
if(dist <= 40){
ans = ((long long)ans * tree[up][dist].get(1, 0, (int)cway[up].size() - 1, 0, ind[now] - 1)) % mod;
ans = ((long long)ans * tree[up][dist].get(1, 0, (int)cway[up].size() - 1, ind[now] + 1, (int)cway[up].size() - 1)) % mod;
}
now = up;
}
cout << ans << '\n';
}
}
return 0;
}
Compilation message
sprinkler.cpp: In member function 'void Seg::push(int, int, int, int, int)':
sprinkler.cpp:29:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int m = s + e >> 1;
| ~~^~~
sprinkler.cpp: In member function 'int Seg::get(int, int, int, int, int)':
sprinkler.cpp:45:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int m = s + e >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
285280 KB |
Output is correct |
2 |
Correct |
171 ms |
285240 KB |
Output is correct |
3 |
Correct |
144 ms |
285296 KB |
Output is correct |
4 |
Correct |
155 ms |
286540 KB |
Output is correct |
5 |
Correct |
156 ms |
286552 KB |
Output is correct |
6 |
Correct |
164 ms |
286520 KB |
Output is correct |
7 |
Correct |
195 ms |
286488 KB |
Output is correct |
8 |
Correct |
189 ms |
286252 KB |
Output is correct |
9 |
Correct |
162 ms |
285300 KB |
Output is correct |
10 |
Correct |
179 ms |
285300 KB |
Output is correct |
11 |
Correct |
186 ms |
285280 KB |
Output is correct |
12 |
Correct |
167 ms |
285288 KB |
Output is correct |
13 |
Correct |
156 ms |
285236 KB |
Output is correct |
14 |
Correct |
176 ms |
285312 KB |
Output is correct |
15 |
Correct |
159 ms |
285300 KB |
Output is correct |
16 |
Correct |
138 ms |
285272 KB |
Output is correct |
17 |
Correct |
153 ms |
285284 KB |
Output is correct |
18 |
Correct |
143 ms |
285292 KB |
Output is correct |
19 |
Correct |
153 ms |
285340 KB |
Output is correct |
20 |
Correct |
166 ms |
285244 KB |
Output is correct |
21 |
Correct |
162 ms |
285232 KB |
Output is correct |
22 |
Correct |
144 ms |
285316 KB |
Output is correct |
23 |
Correct |
163 ms |
285408 KB |
Output is correct |
24 |
Correct |
166 ms |
285320 KB |
Output is correct |
25 |
Correct |
135 ms |
285256 KB |
Output is correct |
26 |
Correct |
135 ms |
285280 KB |
Output is correct |
27 |
Correct |
137 ms |
285208 KB |
Output is correct |
28 |
Correct |
158 ms |
285308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
285260 KB |
Output is correct |
2 |
Correct |
2525 ms |
542156 KB |
Output is correct |
3 |
Correct |
2075 ms |
538680 KB |
Output is correct |
4 |
Correct |
3314 ms |
569144 KB |
Output is correct |
5 |
Correct |
2185 ms |
540216 KB |
Output is correct |
6 |
Correct |
1632 ms |
534400 KB |
Output is correct |
7 |
Correct |
1397 ms |
527704 KB |
Output is correct |
8 |
Correct |
765 ms |
478392 KB |
Output is correct |
9 |
Correct |
3283 ms |
572720 KB |
Output is correct |
10 |
Correct |
2819 ms |
569132 KB |
Output is correct |
11 |
Correct |
2533 ms |
541856 KB |
Output is correct |
12 |
Correct |
2035 ms |
538652 KB |
Output is correct |
13 |
Correct |
591 ms |
488412 KB |
Output is correct |
14 |
Correct |
736 ms |
488456 KB |
Output is correct |
15 |
Correct |
753 ms |
488960 KB |
Output is correct |
16 |
Correct |
815 ms |
488380 KB |
Output is correct |
17 |
Correct |
783 ms |
489168 KB |
Output is correct |
18 |
Correct |
137 ms |
285200 KB |
Output is correct |
19 |
Correct |
164 ms |
285296 KB |
Output is correct |
20 |
Correct |
141 ms |
285196 KB |
Output is correct |
21 |
Correct |
132 ms |
285204 KB |
Output is correct |
22 |
Correct |
134 ms |
285296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
285260 KB |
Output is correct |
2 |
Correct |
2525 ms |
542156 KB |
Output is correct |
3 |
Correct |
2075 ms |
538680 KB |
Output is correct |
4 |
Correct |
3314 ms |
569144 KB |
Output is correct |
5 |
Correct |
2185 ms |
540216 KB |
Output is correct |
6 |
Correct |
1632 ms |
534400 KB |
Output is correct |
7 |
Correct |
1397 ms |
527704 KB |
Output is correct |
8 |
Correct |
765 ms |
478392 KB |
Output is correct |
9 |
Correct |
3283 ms |
572720 KB |
Output is correct |
10 |
Correct |
2819 ms |
569132 KB |
Output is correct |
11 |
Correct |
2533 ms |
541856 KB |
Output is correct |
12 |
Correct |
2035 ms |
538652 KB |
Output is correct |
13 |
Correct |
591 ms |
488412 KB |
Output is correct |
14 |
Correct |
736 ms |
488456 KB |
Output is correct |
15 |
Correct |
753 ms |
488960 KB |
Output is correct |
16 |
Correct |
815 ms |
488380 KB |
Output is correct |
17 |
Correct |
783 ms |
489168 KB |
Output is correct |
18 |
Correct |
137 ms |
285200 KB |
Output is correct |
19 |
Correct |
164 ms |
285296 KB |
Output is correct |
20 |
Correct |
141 ms |
285196 KB |
Output is correct |
21 |
Correct |
132 ms |
285204 KB |
Output is correct |
22 |
Correct |
134 ms |
285296 KB |
Output is correct |
23 |
Correct |
132 ms |
285280 KB |
Output is correct |
24 |
Correct |
2815 ms |
541952 KB |
Output is correct |
25 |
Correct |
2470 ms |
538792 KB |
Output is correct |
26 |
Correct |
3575 ms |
571484 KB |
Output is correct |
27 |
Correct |
2579 ms |
540724 KB |
Output is correct |
28 |
Correct |
1903 ms |
535280 KB |
Output is correct |
29 |
Correct |
1519 ms |
528060 KB |
Output is correct |
30 |
Correct |
992 ms |
479028 KB |
Output is correct |
31 |
Correct |
3976 ms |
571464 KB |
Output is correct |
32 |
Execution timed out |
4054 ms |
569680 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
198 ms |
285328 KB |
Output is correct |
2 |
Execution timed out |
4098 ms |
571632 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
285172 KB |
Output is correct |
2 |
Execution timed out |
4111 ms |
572004 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
285280 KB |
Output is correct |
2 |
Correct |
171 ms |
285240 KB |
Output is correct |
3 |
Correct |
144 ms |
285296 KB |
Output is correct |
4 |
Correct |
155 ms |
286540 KB |
Output is correct |
5 |
Correct |
156 ms |
286552 KB |
Output is correct |
6 |
Correct |
164 ms |
286520 KB |
Output is correct |
7 |
Correct |
195 ms |
286488 KB |
Output is correct |
8 |
Correct |
189 ms |
286252 KB |
Output is correct |
9 |
Correct |
162 ms |
285300 KB |
Output is correct |
10 |
Correct |
179 ms |
285300 KB |
Output is correct |
11 |
Correct |
186 ms |
285280 KB |
Output is correct |
12 |
Correct |
167 ms |
285288 KB |
Output is correct |
13 |
Correct |
156 ms |
285236 KB |
Output is correct |
14 |
Correct |
176 ms |
285312 KB |
Output is correct |
15 |
Correct |
159 ms |
285300 KB |
Output is correct |
16 |
Correct |
138 ms |
285272 KB |
Output is correct |
17 |
Correct |
153 ms |
285284 KB |
Output is correct |
18 |
Correct |
143 ms |
285292 KB |
Output is correct |
19 |
Correct |
153 ms |
285340 KB |
Output is correct |
20 |
Correct |
166 ms |
285244 KB |
Output is correct |
21 |
Correct |
162 ms |
285232 KB |
Output is correct |
22 |
Correct |
144 ms |
285316 KB |
Output is correct |
23 |
Correct |
163 ms |
285408 KB |
Output is correct |
24 |
Correct |
166 ms |
285320 KB |
Output is correct |
25 |
Correct |
135 ms |
285256 KB |
Output is correct |
26 |
Correct |
135 ms |
285280 KB |
Output is correct |
27 |
Correct |
137 ms |
285208 KB |
Output is correct |
28 |
Correct |
158 ms |
285308 KB |
Output is correct |
29 |
Correct |
142 ms |
285260 KB |
Output is correct |
30 |
Correct |
2525 ms |
542156 KB |
Output is correct |
31 |
Correct |
2075 ms |
538680 KB |
Output is correct |
32 |
Correct |
3314 ms |
569144 KB |
Output is correct |
33 |
Correct |
2185 ms |
540216 KB |
Output is correct |
34 |
Correct |
1632 ms |
534400 KB |
Output is correct |
35 |
Correct |
1397 ms |
527704 KB |
Output is correct |
36 |
Correct |
765 ms |
478392 KB |
Output is correct |
37 |
Correct |
3283 ms |
572720 KB |
Output is correct |
38 |
Correct |
2819 ms |
569132 KB |
Output is correct |
39 |
Correct |
2533 ms |
541856 KB |
Output is correct |
40 |
Correct |
2035 ms |
538652 KB |
Output is correct |
41 |
Correct |
591 ms |
488412 KB |
Output is correct |
42 |
Correct |
736 ms |
488456 KB |
Output is correct |
43 |
Correct |
753 ms |
488960 KB |
Output is correct |
44 |
Correct |
815 ms |
488380 KB |
Output is correct |
45 |
Correct |
783 ms |
489168 KB |
Output is correct |
46 |
Correct |
137 ms |
285200 KB |
Output is correct |
47 |
Correct |
164 ms |
285296 KB |
Output is correct |
48 |
Correct |
141 ms |
285196 KB |
Output is correct |
49 |
Correct |
132 ms |
285204 KB |
Output is correct |
50 |
Correct |
134 ms |
285296 KB |
Output is correct |
51 |
Correct |
132 ms |
285280 KB |
Output is correct |
52 |
Correct |
2815 ms |
541952 KB |
Output is correct |
53 |
Correct |
2470 ms |
538792 KB |
Output is correct |
54 |
Correct |
3575 ms |
571484 KB |
Output is correct |
55 |
Correct |
2579 ms |
540724 KB |
Output is correct |
56 |
Correct |
1903 ms |
535280 KB |
Output is correct |
57 |
Correct |
1519 ms |
528060 KB |
Output is correct |
58 |
Correct |
992 ms |
479028 KB |
Output is correct |
59 |
Correct |
3976 ms |
571464 KB |
Output is correct |
60 |
Execution timed out |
4054 ms |
569680 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |