//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 200 * 1000 + 10;
const int mod = 1e9 + 2022;
vi V[nax];
int sz[nax];
int val[nax];
int inner;
void dfs(int x) {
sz[x] = 1;
for(int nbh : V[x]) {
dfs(nbh);
sz[x] = ((ll)sz[x] * sz[nbh]) % mod;
}
sz[x] = ((ll)sz[x] * max(1, (int)V[x].size())) % mod;
cerr << x << " -> " << sz[x] << endl;
}
int n;
void dfs2(int x, int cont) {
vi pref((int)V[x].size() + 2), suf((int)V[x].size() + 2);
pref[0] = 1; suf[(int)V[x].size() + 1] = 1;
for(int i = 1; i <= (int)V[x].size(); ++i) {
pref[i] = ((ll)pref[i - 1] * sz[V[x][i - 1]]) % mod;
}
for(int i = (int)V[x].size(); i >= 1; --i) {
suf[i] = ((ll)suf[i + 1] * sz[V[x][i - 1]]) % mod;
}
for(int i = 1; i <= (int)V[x].size(); ++i) {
int nbh = V[x][i - 1];
ll v = ((ll)pref[i - 1] * suf[i + 1]) % mod;
dfs2(nbh, (v * cont) % mod);
}
if(x >= inner) {
val[x - inner] = cont;
cerr << x << ": " << cont << "\n";
}
}
struct Node {
int f, sum;
bool rev;
};
Node T[(1 << 19)];
void pull(int v) {
T[v].sum = (T[v << 1].sum + T[v << 1 | 1].sum) % mod;
T[v].f = (T[v << 1].f + T[v << 1 | 1].f) % mod;
}
void build(vi &A, int l = 0, int r = n - 1, int v = 1) {
if(l == r) {
T[v].f = A[l] * val[l];
T[v].sum = val[l];
T[v].rev = 0;
return;
}
int mid = (l + r) >> 1;
build(A, l, mid, v << 1);
build(A, mid + 1, r, v << 1 | 1);
pull(v);
T[v].rev = 0;
}
void putTag(int v) {
T[v].f = (T[v].sum - T[v].f) % mod;
T[v].rev ^= 1;
}
void push(int v) {
if(!T[v].rev) return;
putTag(v << 1);
putTag(v << 1 | 1);
T[v].rev = 0;
}
void upd(int a, int b, int l = 0, int r = n - 1, int v = 1) {
if(a <= l && r <= b) {
putTag(v);
return;
}
int mid = (l + r) >> 1;
push(v);
if(a <= mid) upd(a, b, l, mid, v << 1);
if(mid < b) upd(a, b, mid + 1, r, v << 1 | 1);
pull(v);
}
void init(int N, int M, vi P, vi A) {
inner = N;
n = M;
for(int i = 1; i < N + M; ++i) {
V[P[i]].PB(i);
}
dfs(0);
dfs2(0, 1);
build(A);
}
int count_ways(int l, int r) {
upd(l - inner, r - inner);
int w = T[1].f;
if(w < 0) w += mod;
return w;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
13 ms |
5032 KB |
Output is correct |
4 |
Correct |
13 ms |
5096 KB |
Output is correct |
5 |
Correct |
13 ms |
5044 KB |
Output is correct |
6 |
Correct |
13 ms |
5072 KB |
Output is correct |
7 |
Correct |
13 ms |
5064 KB |
Output is correct |
8 |
Correct |
12 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
7 ms |
5040 KB |
Output is correct |
3 |
Correct |
10 ms |
5064 KB |
Output is correct |
4 |
Correct |
11 ms |
5072 KB |
Output is correct |
5 |
Correct |
11 ms |
5044 KB |
Output is correct |
6 |
Correct |
14 ms |
5072 KB |
Output is correct |
7 |
Correct |
18 ms |
5096 KB |
Output is correct |
8 |
Correct |
18 ms |
5076 KB |
Output is correct |
9 |
Correct |
18 ms |
5080 KB |
Output is correct |
10 |
Correct |
18 ms |
5328 KB |
Output is correct |
11 |
Correct |
18 ms |
5328 KB |
Output is correct |
12 |
Correct |
16 ms |
5096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
13 ms |
5032 KB |
Output is correct |
4 |
Correct |
13 ms |
5096 KB |
Output is correct |
5 |
Correct |
13 ms |
5044 KB |
Output is correct |
6 |
Correct |
13 ms |
5072 KB |
Output is correct |
7 |
Correct |
13 ms |
5064 KB |
Output is correct |
8 |
Correct |
12 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
7 ms |
5040 KB |
Output is correct |
11 |
Correct |
10 ms |
5064 KB |
Output is correct |
12 |
Correct |
11 ms |
5072 KB |
Output is correct |
13 |
Correct |
11 ms |
5044 KB |
Output is correct |
14 |
Correct |
14 ms |
5072 KB |
Output is correct |
15 |
Correct |
18 ms |
5096 KB |
Output is correct |
16 |
Correct |
18 ms |
5076 KB |
Output is correct |
17 |
Correct |
18 ms |
5080 KB |
Output is correct |
18 |
Correct |
18 ms |
5328 KB |
Output is correct |
19 |
Correct |
18 ms |
5328 KB |
Output is correct |
20 |
Correct |
16 ms |
5096 KB |
Output is correct |
21 |
Correct |
16 ms |
5080 KB |
Output is correct |
22 |
Correct |
12 ms |
5072 KB |
Output is correct |
23 |
Correct |
12 ms |
5072 KB |
Output is correct |
24 |
Correct |
17 ms |
5072 KB |
Output is correct |
25 |
Correct |
17 ms |
5072 KB |
Output is correct |
26 |
Correct |
17 ms |
5072 KB |
Output is correct |
27 |
Correct |
19 ms |
5212 KB |
Output is correct |
28 |
Correct |
17 ms |
5108 KB |
Output is correct |
29 |
Correct |
13 ms |
5064 KB |
Output is correct |
30 |
Correct |
12 ms |
5068 KB |
Output is correct |
31 |
Correct |
8 ms |
5200 KB |
Output is correct |
32 |
Correct |
18 ms |
5072 KB |
Output is correct |
33 |
Correct |
16 ms |
5072 KB |
Output is correct |
34 |
Correct |
14 ms |
5060 KB |
Output is correct |
35 |
Correct |
11 ms |
5056 KB |
Output is correct |
36 |
Correct |
17 ms |
5328 KB |
Output is correct |
37 |
Correct |
18 ms |
5328 KB |
Output is correct |
38 |
Correct |
18 ms |
5328 KB |
Output is correct |
39 |
Correct |
11 ms |
5112 KB |
Output is correct |
40 |
Correct |
14 ms |
5040 KB |
Output is correct |
41 |
Correct |
13 ms |
4996 KB |
Output is correct |
42 |
Correct |
14 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1174 ms |
9176 KB |
Output is correct |
2 |
Correct |
1741 ms |
13464 KB |
Output is correct |
3 |
Correct |
1634 ms |
13568 KB |
Output is correct |
4 |
Correct |
1794 ms |
13516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1174 ms |
9176 KB |
Output is correct |
2 |
Correct |
1741 ms |
13464 KB |
Output is correct |
3 |
Correct |
1634 ms |
13568 KB |
Output is correct |
4 |
Correct |
1794 ms |
13516 KB |
Output is correct |
5 |
Correct |
1278 ms |
9120 KB |
Output is correct |
6 |
Correct |
1812 ms |
13532 KB |
Output is correct |
7 |
Correct |
1939 ms |
13516 KB |
Output is correct |
8 |
Correct |
1629 ms |
13524 KB |
Output is correct |
9 |
Correct |
372 ms |
5200 KB |
Output is correct |
10 |
Correct |
1125 ms |
5484 KB |
Output is correct |
11 |
Correct |
853 ms |
5396 KB |
Output is correct |
12 |
Correct |
793 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
7 ms |
5040 KB |
Output is correct |
3 |
Correct |
10 ms |
5064 KB |
Output is correct |
4 |
Correct |
11 ms |
5072 KB |
Output is correct |
5 |
Correct |
11 ms |
5044 KB |
Output is correct |
6 |
Correct |
14 ms |
5072 KB |
Output is correct |
7 |
Correct |
18 ms |
5096 KB |
Output is correct |
8 |
Correct |
18 ms |
5076 KB |
Output is correct |
9 |
Correct |
18 ms |
5080 KB |
Output is correct |
10 |
Correct |
18 ms |
5328 KB |
Output is correct |
11 |
Correct |
18 ms |
5328 KB |
Output is correct |
12 |
Correct |
16 ms |
5096 KB |
Output is correct |
13 |
Correct |
1174 ms |
9176 KB |
Output is correct |
14 |
Correct |
1741 ms |
13464 KB |
Output is correct |
15 |
Correct |
1634 ms |
13568 KB |
Output is correct |
16 |
Correct |
1794 ms |
13516 KB |
Output is correct |
17 |
Correct |
1278 ms |
9120 KB |
Output is correct |
18 |
Correct |
1812 ms |
13532 KB |
Output is correct |
19 |
Correct |
1939 ms |
13516 KB |
Output is correct |
20 |
Correct |
1629 ms |
13524 KB |
Output is correct |
21 |
Correct |
372 ms |
5200 KB |
Output is correct |
22 |
Correct |
1125 ms |
5484 KB |
Output is correct |
23 |
Correct |
853 ms |
5396 KB |
Output is correct |
24 |
Correct |
793 ms |
5504 KB |
Output is correct |
25 |
Correct |
2240 ms |
18792 KB |
Output is correct |
26 |
Correct |
2218 ms |
19048 KB |
Output is correct |
27 |
Correct |
2439 ms |
18932 KB |
Output is correct |
28 |
Correct |
2361 ms |
19040 KB |
Output is correct |
29 |
Correct |
2396 ms |
41652 KB |
Output is correct |
30 |
Correct |
2240 ms |
41628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
13 ms |
5032 KB |
Output is correct |
4 |
Correct |
13 ms |
5096 KB |
Output is correct |
5 |
Correct |
13 ms |
5044 KB |
Output is correct |
6 |
Correct |
13 ms |
5072 KB |
Output is correct |
7 |
Correct |
13 ms |
5064 KB |
Output is correct |
8 |
Correct |
12 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
7 ms |
5040 KB |
Output is correct |
11 |
Correct |
10 ms |
5064 KB |
Output is correct |
12 |
Correct |
11 ms |
5072 KB |
Output is correct |
13 |
Correct |
11 ms |
5044 KB |
Output is correct |
14 |
Correct |
14 ms |
5072 KB |
Output is correct |
15 |
Correct |
18 ms |
5096 KB |
Output is correct |
16 |
Correct |
18 ms |
5076 KB |
Output is correct |
17 |
Correct |
18 ms |
5080 KB |
Output is correct |
18 |
Correct |
18 ms |
5328 KB |
Output is correct |
19 |
Correct |
18 ms |
5328 KB |
Output is correct |
20 |
Correct |
16 ms |
5096 KB |
Output is correct |
21 |
Correct |
16 ms |
5080 KB |
Output is correct |
22 |
Correct |
12 ms |
5072 KB |
Output is correct |
23 |
Correct |
12 ms |
5072 KB |
Output is correct |
24 |
Correct |
17 ms |
5072 KB |
Output is correct |
25 |
Correct |
17 ms |
5072 KB |
Output is correct |
26 |
Correct |
17 ms |
5072 KB |
Output is correct |
27 |
Correct |
19 ms |
5212 KB |
Output is correct |
28 |
Correct |
17 ms |
5108 KB |
Output is correct |
29 |
Correct |
13 ms |
5064 KB |
Output is correct |
30 |
Correct |
12 ms |
5068 KB |
Output is correct |
31 |
Correct |
8 ms |
5200 KB |
Output is correct |
32 |
Correct |
18 ms |
5072 KB |
Output is correct |
33 |
Correct |
16 ms |
5072 KB |
Output is correct |
34 |
Correct |
14 ms |
5060 KB |
Output is correct |
35 |
Correct |
11 ms |
5056 KB |
Output is correct |
36 |
Correct |
17 ms |
5328 KB |
Output is correct |
37 |
Correct |
18 ms |
5328 KB |
Output is correct |
38 |
Correct |
18 ms |
5328 KB |
Output is correct |
39 |
Correct |
11 ms |
5112 KB |
Output is correct |
40 |
Correct |
14 ms |
5040 KB |
Output is correct |
41 |
Correct |
13 ms |
4996 KB |
Output is correct |
42 |
Correct |
14 ms |
5072 KB |
Output is correct |
43 |
Correct |
748 ms |
5360 KB |
Output is correct |
44 |
Correct |
968 ms |
5308 KB |
Output is correct |
45 |
Correct |
1039 ms |
5324 KB |
Output is correct |
46 |
Correct |
903 ms |
5652 KB |
Output is correct |
47 |
Correct |
1057 ms |
5616 KB |
Output is correct |
48 |
Correct |
838 ms |
5632 KB |
Output is correct |
49 |
Correct |
981 ms |
5632 KB |
Output is correct |
50 |
Correct |
1000 ms |
5608 KB |
Output is correct |
51 |
Correct |
842 ms |
5364 KB |
Output is correct |
52 |
Correct |
939 ms |
5448 KB |
Output is correct |
53 |
Correct |
711 ms |
6344 KB |
Output is correct |
54 |
Correct |
919 ms |
5600 KB |
Output is correct |
55 |
Correct |
998 ms |
5500 KB |
Output is correct |
56 |
Correct |
998 ms |
5448 KB |
Output is correct |
57 |
Correct |
959 ms |
5448 KB |
Output is correct |
58 |
Correct |
1037 ms |
6772 KB |
Output is correct |
59 |
Correct |
1000 ms |
6856 KB |
Output is correct |
60 |
Correct |
871 ms |
6780 KB |
Output is correct |
61 |
Correct |
950 ms |
5608 KB |
Output is correct |
62 |
Correct |
820 ms |
5348 KB |
Output is correct |
63 |
Correct |
1152 ms |
5232 KB |
Output is correct |
64 |
Correct |
810 ms |
5392 KB |
Output is correct |
65 |
Correct |
421 ms |
5192 KB |
Output is correct |
66 |
Correct |
951 ms |
5496 KB |
Output is correct |
67 |
Correct |
833 ms |
5476 KB |
Output is correct |
68 |
Correct |
938 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
13 ms |
5032 KB |
Output is correct |
4 |
Correct |
13 ms |
5096 KB |
Output is correct |
5 |
Correct |
13 ms |
5044 KB |
Output is correct |
6 |
Correct |
13 ms |
5072 KB |
Output is correct |
7 |
Correct |
13 ms |
5064 KB |
Output is correct |
8 |
Correct |
12 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
7 ms |
5040 KB |
Output is correct |
11 |
Correct |
10 ms |
5064 KB |
Output is correct |
12 |
Correct |
11 ms |
5072 KB |
Output is correct |
13 |
Correct |
11 ms |
5044 KB |
Output is correct |
14 |
Correct |
14 ms |
5072 KB |
Output is correct |
15 |
Correct |
18 ms |
5096 KB |
Output is correct |
16 |
Correct |
18 ms |
5076 KB |
Output is correct |
17 |
Correct |
18 ms |
5080 KB |
Output is correct |
18 |
Correct |
18 ms |
5328 KB |
Output is correct |
19 |
Correct |
18 ms |
5328 KB |
Output is correct |
20 |
Correct |
16 ms |
5096 KB |
Output is correct |
21 |
Correct |
16 ms |
5080 KB |
Output is correct |
22 |
Correct |
12 ms |
5072 KB |
Output is correct |
23 |
Correct |
12 ms |
5072 KB |
Output is correct |
24 |
Correct |
17 ms |
5072 KB |
Output is correct |
25 |
Correct |
17 ms |
5072 KB |
Output is correct |
26 |
Correct |
17 ms |
5072 KB |
Output is correct |
27 |
Correct |
19 ms |
5212 KB |
Output is correct |
28 |
Correct |
17 ms |
5108 KB |
Output is correct |
29 |
Correct |
13 ms |
5064 KB |
Output is correct |
30 |
Correct |
12 ms |
5068 KB |
Output is correct |
31 |
Correct |
8 ms |
5200 KB |
Output is correct |
32 |
Correct |
18 ms |
5072 KB |
Output is correct |
33 |
Correct |
16 ms |
5072 KB |
Output is correct |
34 |
Correct |
14 ms |
5060 KB |
Output is correct |
35 |
Correct |
11 ms |
5056 KB |
Output is correct |
36 |
Correct |
17 ms |
5328 KB |
Output is correct |
37 |
Correct |
18 ms |
5328 KB |
Output is correct |
38 |
Correct |
18 ms |
5328 KB |
Output is correct |
39 |
Correct |
11 ms |
5112 KB |
Output is correct |
40 |
Correct |
14 ms |
5040 KB |
Output is correct |
41 |
Correct |
13 ms |
4996 KB |
Output is correct |
42 |
Correct |
14 ms |
5072 KB |
Output is correct |
43 |
Correct |
1174 ms |
9176 KB |
Output is correct |
44 |
Correct |
1741 ms |
13464 KB |
Output is correct |
45 |
Correct |
1634 ms |
13568 KB |
Output is correct |
46 |
Correct |
1794 ms |
13516 KB |
Output is correct |
47 |
Correct |
1278 ms |
9120 KB |
Output is correct |
48 |
Correct |
1812 ms |
13532 KB |
Output is correct |
49 |
Correct |
1939 ms |
13516 KB |
Output is correct |
50 |
Correct |
1629 ms |
13524 KB |
Output is correct |
51 |
Correct |
372 ms |
5200 KB |
Output is correct |
52 |
Correct |
1125 ms |
5484 KB |
Output is correct |
53 |
Correct |
853 ms |
5396 KB |
Output is correct |
54 |
Correct |
793 ms |
5504 KB |
Output is correct |
55 |
Correct |
2240 ms |
18792 KB |
Output is correct |
56 |
Correct |
2218 ms |
19048 KB |
Output is correct |
57 |
Correct |
2439 ms |
18932 KB |
Output is correct |
58 |
Correct |
2361 ms |
19040 KB |
Output is correct |
59 |
Correct |
2396 ms |
41652 KB |
Output is correct |
60 |
Correct |
2240 ms |
41628 KB |
Output is correct |
61 |
Correct |
748 ms |
5360 KB |
Output is correct |
62 |
Correct |
968 ms |
5308 KB |
Output is correct |
63 |
Correct |
1039 ms |
5324 KB |
Output is correct |
64 |
Correct |
903 ms |
5652 KB |
Output is correct |
65 |
Correct |
1057 ms |
5616 KB |
Output is correct |
66 |
Correct |
838 ms |
5632 KB |
Output is correct |
67 |
Correct |
981 ms |
5632 KB |
Output is correct |
68 |
Correct |
1000 ms |
5608 KB |
Output is correct |
69 |
Correct |
842 ms |
5364 KB |
Output is correct |
70 |
Correct |
939 ms |
5448 KB |
Output is correct |
71 |
Correct |
711 ms |
6344 KB |
Output is correct |
72 |
Correct |
919 ms |
5600 KB |
Output is correct |
73 |
Correct |
998 ms |
5500 KB |
Output is correct |
74 |
Correct |
998 ms |
5448 KB |
Output is correct |
75 |
Correct |
959 ms |
5448 KB |
Output is correct |
76 |
Correct |
1037 ms |
6772 KB |
Output is correct |
77 |
Correct |
1000 ms |
6856 KB |
Output is correct |
78 |
Correct |
871 ms |
6780 KB |
Output is correct |
79 |
Correct |
950 ms |
5608 KB |
Output is correct |
80 |
Correct |
820 ms |
5348 KB |
Output is correct |
81 |
Correct |
1152 ms |
5232 KB |
Output is correct |
82 |
Correct |
810 ms |
5392 KB |
Output is correct |
83 |
Correct |
421 ms |
5192 KB |
Output is correct |
84 |
Correct |
951 ms |
5496 KB |
Output is correct |
85 |
Correct |
833 ms |
5476 KB |
Output is correct |
86 |
Correct |
938 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
1773 ms |
17552 KB |
Output is correct |
89 |
Correct |
1857 ms |
13392 KB |
Output is correct |
90 |
Correct |
2009 ms |
13120 KB |
Output is correct |
91 |
Correct |
2306 ms |
19064 KB |
Output is correct |
92 |
Correct |
2260 ms |
19024 KB |
Output is correct |
93 |
Correct |
2233 ms |
18892 KB |
Output is correct |
94 |
Correct |
2301 ms |
19060 KB |
Output is correct |
95 |
Correct |
2552 ms |
19072 KB |
Output is correct |
96 |
Correct |
1985 ms |
12988 KB |
Output is correct |
97 |
Correct |
1940 ms |
12924 KB |
Output is correct |
98 |
Correct |
1402 ms |
32216 KB |
Output is correct |
99 |
Correct |
2307 ms |
18932 KB |
Output is correct |
100 |
Correct |
2283 ms |
16064 KB |
Output is correct |
101 |
Correct |
2232 ms |
15084 KB |
Output is correct |
102 |
Correct |
1861 ms |
13692 KB |
Output is correct |
103 |
Correct |
2542 ms |
41564 KB |
Output is correct |
104 |
Correct |
2250 ms |
40880 KB |
Output is correct |
105 |
Correct |
2300 ms |
41024 KB |
Output is correct |
106 |
Correct |
1597 ms |
17764 KB |
Output is correct |
107 |
Correct |
1636 ms |
13892 KB |
Output is correct |
108 |
Correct |
1870 ms |
13872 KB |
Output is correct |
109 |
Correct |
1832 ms |
13792 KB |
Output is correct |