Submission #800708

# Submission time Handle Problem Language Result Execution time Memory
800708 2023-08-01T18:41:40 Z dxz05 Digital Circuit (IOI22_circuit) C++17
100 / 100
954 ms 35912 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")

#include "circuit.h"
#include <bits/stdc++.h>
 
using namespace std;
 
const int MaxN = 200005;
const int MOD = 1e9 + 2022;

int binpow(int n, int k){
	int res = 1;
    while (k > 0){
        if (k & 1) res = 1LL * res * n % MOD;
        n = 1LL * n * n % MOD;
        k >>= 1;
    }
    return res;
}

int N, M;
vector<int> A;
 
int contribution[MaxN];
 
struct node{
	int sum;
	int sum2;
	bool flag;
	
	node(){
		sum = sum2 = 0;
		flag = false;
	};
};
 
node tree[4 * MaxN];
 
void flip(int v){
	swap(tree[v].sum, tree[v].sum2);
	tree[v].flag ^= 1;
}
 
void push(int v, int tl, int tr){
	if (tl == tr || !tree[v].flag) return;
	flip(v << 1);
	flip(v << 1 | 1);
	tree[v].flag = false;
}
 
void build(int v, int tl, int tr){
	if (tl == tr){
		tree[v].sum = A[tl - N] * contribution[tl];
		tree[v].sum2 = (!A[tl - N]) * contribution[tl];
		return;
	}
	int tm = (tl + tr) >> 1;
	build(v << 1, tl, tm);
	build(v << 1 | 1, tm + 1, tr);
	
	tree[v].sum = (tree[v << 1].sum + tree[v << 1 | 1].sum) % MOD;
	tree[v].sum2 = (tree[v << 1].sum2 + tree[v << 1 | 1].sum2) % MOD;
}
 
void update(int v, int tl, int tr, int l, int r){
	push(v, tl, tr);
	if (l <= tl && tr <= r){
		flip(v);
		return;
	}
	if (tl > r || tr < l) return;
	int tm = (tl + tr) >> 1;
	update(v << 1, tl, tm, l, r);
	update(v << 1 | 1, tm + 1, tr, l, r);
	
	tree[v].sum = (tree[v << 1].sum + tree[v << 1 | 1].sum) % MOD;
	tree[v].sum2 = (tree[v << 1].sum2 + tree[v << 1 | 1].sum2) % MOD;
}
 
void update(int l, int r){
	update(1, N, N + M - 1, l, r);
}
 
int get(){
	return tree[1].sum;
}
 
vector<int> g[MaxN];
 
long long sub[MaxN];

void dfs0(int v){
	sub[v] = 1;
	if (!g[v].empty()){
		sub[v] = sub[v] * (int) g[v].size() % MOD;
	}
	
	for (int u : g[v]){
		dfs0(u);
		sub[v] = sub[v] * sub[u] % MOD;		
	}
}

void dfs1(int v, long long up){
	int sz = (int) g[v].size();
	
	if (sz == 0){
		contribution[v] = up % MOD;
		return;
	}
	
	vector<long long> suff(sz, 1);
	for (int i = sz - 1; i >= 0; i--){
		int u = g[v][i];
		
		suff[i] = sub[u];
		if (i + 1 < sz) {
			suff[i] = suff[i] * suff[i + 1] % MOD;
		}
	}
	
	long long pref = 1;
	for (int i = 0; i < sz; i++){
		int u = g[v][i];
		
		long long ways = up * pref % MOD;
		if (i + 1 < sz) ways = ways * suff[i + 1] % MOD;
		
		dfs1(u, ways);
		
		pref = pref * sub[u] % MOD;		
	}
}

void init(int _N, int _M, vector<int> P, vector<int> _A) {
    N = _N, M = _M, A = _A;
    for (int i = 1; i < N + M; i++){
        g[P[i]].push_back(i);
    }
	
    dfs0(0);
    dfs1(0, 1);
        
    build(1, N, N + M - 1);
}
 
int count_ways(int L, int R) {
    update(L, R);
    return get();
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14288 KB Output is correct
2 Correct 7 ms 14324 KB Output is correct
3 Correct 9 ms 14440 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14392 KB Output is correct
6 Correct 8 ms 14388 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 8 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14332 KB Output is correct
2 Correct 8 ms 14328 KB Output is correct
3 Correct 10 ms 14416 KB Output is correct
4 Correct 10 ms 14372 KB Output is correct
5 Correct 8 ms 14392 KB Output is correct
6 Correct 9 ms 14456 KB Output is correct
7 Correct 8 ms 14464 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 9 ms 14452 KB Output is correct
10 Correct 7 ms 14544 KB Output is correct
11 Correct 9 ms 14544 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14288 KB Output is correct
2 Correct 7 ms 14324 KB Output is correct
3 Correct 9 ms 14440 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14392 KB Output is correct
6 Correct 8 ms 14388 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 8 ms 14416 KB Output is correct
9 Correct 9 ms 14332 KB Output is correct
10 Correct 8 ms 14328 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14372 KB Output is correct
13 Correct 8 ms 14392 KB Output is correct
14 Correct 9 ms 14456 KB Output is correct
15 Correct 8 ms 14464 KB Output is correct
16 Correct 9 ms 14416 KB Output is correct
17 Correct 9 ms 14452 KB Output is correct
18 Correct 7 ms 14544 KB Output is correct
19 Correct 9 ms 14544 KB Output is correct
20 Correct 8 ms 14416 KB Output is correct
21 Correct 8 ms 14416 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 9 ms 14416 KB Output is correct
24 Correct 8 ms 14364 KB Output is correct
25 Correct 7 ms 14416 KB Output is correct
26 Correct 8 ms 14508 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 9 ms 14392 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 7 ms 14452 KB Output is correct
32 Correct 8 ms 14416 KB Output is correct
33 Correct 10 ms 14436 KB Output is correct
34 Correct 9 ms 14416 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 8 ms 14544 KB Output is correct
37 Correct 8 ms 14532 KB Output is correct
38 Correct 10 ms 14544 KB Output is correct
39 Correct 9 ms 14416 KB Output is correct
40 Correct 7 ms 14416 KB Output is correct
41 Correct 10 ms 14416 KB Output is correct
42 Correct 7 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 695 ms 16924 KB Output is correct
2 Correct 860 ms 19496 KB Output is correct
3 Correct 849 ms 19528 KB Output is correct
4 Correct 738 ms 19528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 695 ms 16924 KB Output is correct
2 Correct 860 ms 19496 KB Output is correct
3 Correct 849 ms 19528 KB Output is correct
4 Correct 738 ms 19528 KB Output is correct
5 Correct 626 ms 17024 KB Output is correct
6 Correct 903 ms 19536 KB Output is correct
7 Correct 914 ms 19560 KB Output is correct
8 Correct 697 ms 19528 KB Output is correct
9 Correct 401 ms 14544 KB Output is correct
10 Correct 760 ms 14672 KB Output is correct
11 Correct 922 ms 14672 KB Output is correct
12 Correct 703 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14332 KB Output is correct
2 Correct 8 ms 14328 KB Output is correct
3 Correct 10 ms 14416 KB Output is correct
4 Correct 10 ms 14372 KB Output is correct
5 Correct 8 ms 14392 KB Output is correct
6 Correct 9 ms 14456 KB Output is correct
7 Correct 8 ms 14464 KB Output is correct
8 Correct 9 ms 14416 KB Output is correct
9 Correct 9 ms 14452 KB Output is correct
10 Correct 7 ms 14544 KB Output is correct
11 Correct 9 ms 14544 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 695 ms 16924 KB Output is correct
14 Correct 860 ms 19496 KB Output is correct
15 Correct 849 ms 19528 KB Output is correct
16 Correct 738 ms 19528 KB Output is correct
17 Correct 626 ms 17024 KB Output is correct
18 Correct 903 ms 19536 KB Output is correct
19 Correct 914 ms 19560 KB Output is correct
20 Correct 697 ms 19528 KB Output is correct
21 Correct 401 ms 14544 KB Output is correct
22 Correct 760 ms 14672 KB Output is correct
23 Correct 922 ms 14672 KB Output is correct
24 Correct 703 ms 14720 KB Output is correct
25 Correct 837 ms 22068 KB Output is correct
26 Correct 815 ms 22100 KB Output is correct
27 Correct 803 ms 22220 KB Output is correct
28 Correct 768 ms 22104 KB Output is correct
29 Correct 765 ms 34692 KB Output is correct
30 Correct 954 ms 34732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14288 KB Output is correct
2 Correct 7 ms 14324 KB Output is correct
3 Correct 9 ms 14440 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14392 KB Output is correct
6 Correct 8 ms 14388 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 8 ms 14416 KB Output is correct
9 Correct 9 ms 14332 KB Output is correct
10 Correct 8 ms 14328 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14372 KB Output is correct
13 Correct 8 ms 14392 KB Output is correct
14 Correct 9 ms 14456 KB Output is correct
15 Correct 8 ms 14464 KB Output is correct
16 Correct 9 ms 14416 KB Output is correct
17 Correct 9 ms 14452 KB Output is correct
18 Correct 7 ms 14544 KB Output is correct
19 Correct 9 ms 14544 KB Output is correct
20 Correct 8 ms 14416 KB Output is correct
21 Correct 8 ms 14416 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 9 ms 14416 KB Output is correct
24 Correct 8 ms 14364 KB Output is correct
25 Correct 7 ms 14416 KB Output is correct
26 Correct 8 ms 14508 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 9 ms 14392 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 7 ms 14452 KB Output is correct
32 Correct 8 ms 14416 KB Output is correct
33 Correct 10 ms 14436 KB Output is correct
34 Correct 9 ms 14416 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 8 ms 14544 KB Output is correct
37 Correct 8 ms 14532 KB Output is correct
38 Correct 10 ms 14544 KB Output is correct
39 Correct 9 ms 14416 KB Output is correct
40 Correct 7 ms 14416 KB Output is correct
41 Correct 10 ms 14416 KB Output is correct
42 Correct 7 ms 14464 KB Output is correct
43 Correct 556 ms 14544 KB Output is correct
44 Correct 674 ms 14720 KB Output is correct
45 Correct 683 ms 14544 KB Output is correct
46 Correct 541 ms 14808 KB Output is correct
47 Correct 521 ms 14716 KB Output is correct
48 Correct 754 ms 14700 KB Output is correct
49 Correct 805 ms 14724 KB Output is correct
50 Correct 952 ms 14708 KB Output is correct
51 Correct 629 ms 14632 KB Output is correct
52 Correct 756 ms 14668 KB Output is correct
53 Correct 571 ms 15184 KB Output is correct
54 Correct 787 ms 14704 KB Output is correct
55 Correct 696 ms 14564 KB Output is correct
56 Correct 870 ms 14624 KB Output is correct
57 Correct 891 ms 14544 KB Output is correct
58 Correct 825 ms 15324 KB Output is correct
59 Correct 833 ms 15528 KB Output is correct
60 Correct 811 ms 15488 KB Output is correct
61 Correct 781 ms 14800 KB Output is correct
62 Correct 888 ms 14608 KB Output is correct
63 Correct 641 ms 14544 KB Output is correct
64 Correct 792 ms 14544 KB Output is correct
65 Correct 412 ms 14592 KB Output is correct
66 Correct 532 ms 14652 KB Output is correct
67 Correct 806 ms 14592 KB Output is correct
68 Correct 766 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14288 KB Output is correct
2 Correct 7 ms 14324 KB Output is correct
3 Correct 9 ms 14440 KB Output is correct
4 Correct 8 ms 14416 KB Output is correct
5 Correct 9 ms 14392 KB Output is correct
6 Correct 8 ms 14388 KB Output is correct
7 Correct 8 ms 14440 KB Output is correct
8 Correct 8 ms 14416 KB Output is correct
9 Correct 9 ms 14332 KB Output is correct
10 Correct 8 ms 14328 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14372 KB Output is correct
13 Correct 8 ms 14392 KB Output is correct
14 Correct 9 ms 14456 KB Output is correct
15 Correct 8 ms 14464 KB Output is correct
16 Correct 9 ms 14416 KB Output is correct
17 Correct 9 ms 14452 KB Output is correct
18 Correct 7 ms 14544 KB Output is correct
19 Correct 9 ms 14544 KB Output is correct
20 Correct 8 ms 14416 KB Output is correct
21 Correct 8 ms 14416 KB Output is correct
22 Correct 7 ms 14416 KB Output is correct
23 Correct 9 ms 14416 KB Output is correct
24 Correct 8 ms 14364 KB Output is correct
25 Correct 7 ms 14416 KB Output is correct
26 Correct 8 ms 14508 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 9 ms 14392 KB Output is correct
30 Correct 8 ms 14416 KB Output is correct
31 Correct 7 ms 14452 KB Output is correct
32 Correct 8 ms 14416 KB Output is correct
33 Correct 10 ms 14436 KB Output is correct
34 Correct 9 ms 14416 KB Output is correct
35 Correct 9 ms 14416 KB Output is correct
36 Correct 8 ms 14544 KB Output is correct
37 Correct 8 ms 14532 KB Output is correct
38 Correct 10 ms 14544 KB Output is correct
39 Correct 9 ms 14416 KB Output is correct
40 Correct 7 ms 14416 KB Output is correct
41 Correct 10 ms 14416 KB Output is correct
42 Correct 7 ms 14464 KB Output is correct
43 Correct 695 ms 16924 KB Output is correct
44 Correct 860 ms 19496 KB Output is correct
45 Correct 849 ms 19528 KB Output is correct
46 Correct 738 ms 19528 KB Output is correct
47 Correct 626 ms 17024 KB Output is correct
48 Correct 903 ms 19536 KB Output is correct
49 Correct 914 ms 19560 KB Output is correct
50 Correct 697 ms 19528 KB Output is correct
51 Correct 401 ms 14544 KB Output is correct
52 Correct 760 ms 14672 KB Output is correct
53 Correct 922 ms 14672 KB Output is correct
54 Correct 703 ms 14720 KB Output is correct
55 Correct 837 ms 22068 KB Output is correct
56 Correct 815 ms 22100 KB Output is correct
57 Correct 803 ms 22220 KB Output is correct
58 Correct 768 ms 22104 KB Output is correct
59 Correct 765 ms 34692 KB Output is correct
60 Correct 954 ms 34732 KB Output is correct
61 Correct 556 ms 14544 KB Output is correct
62 Correct 674 ms 14720 KB Output is correct
63 Correct 683 ms 14544 KB Output is correct
64 Correct 541 ms 14808 KB Output is correct
65 Correct 521 ms 14716 KB Output is correct
66 Correct 754 ms 14700 KB Output is correct
67 Correct 805 ms 14724 KB Output is correct
68 Correct 952 ms 14708 KB Output is correct
69 Correct 629 ms 14632 KB Output is correct
70 Correct 756 ms 14668 KB Output is correct
71 Correct 571 ms 15184 KB Output is correct
72 Correct 787 ms 14704 KB Output is correct
73 Correct 696 ms 14564 KB Output is correct
74 Correct 870 ms 14624 KB Output is correct
75 Correct 891 ms 14544 KB Output is correct
76 Correct 825 ms 15324 KB Output is correct
77 Correct 833 ms 15528 KB Output is correct
78 Correct 811 ms 15488 KB Output is correct
79 Correct 781 ms 14800 KB Output is correct
80 Correct 888 ms 14608 KB Output is correct
81 Correct 641 ms 14544 KB Output is correct
82 Correct 792 ms 14544 KB Output is correct
83 Correct 412 ms 14592 KB Output is correct
84 Correct 532 ms 14652 KB Output is correct
85 Correct 806 ms 14592 KB Output is correct
86 Correct 766 ms 14672 KB Output is correct
87 Correct 7 ms 14416 KB Output is correct
88 Correct 434 ms 21416 KB Output is correct
89 Correct 629 ms 19508 KB Output is correct
90 Correct 866 ms 19248 KB Output is correct
91 Correct 600 ms 22212 KB Output is correct
92 Correct 804 ms 22332 KB Output is correct
93 Correct 746 ms 22228 KB Output is correct
94 Correct 810 ms 22264 KB Output is correct
95 Correct 669 ms 22332 KB Output is correct
96 Correct 660 ms 18760 KB Output is correct
97 Correct 776 ms 18756 KB Output is correct
98 Correct 639 ms 31560 KB Output is correct
99 Correct 788 ms 22216 KB Output is correct
100 Correct 827 ms 19784 KB Output is correct
101 Correct 780 ms 19104 KB Output is correct
102 Correct 887 ms 17992 KB Output is correct
103 Correct 933 ms 34732 KB Output is correct
104 Correct 812 ms 35908 KB Output is correct
105 Correct 788 ms 35912 KB Output is correct
106 Correct 721 ms 21936 KB Output is correct
107 Correct 777 ms 18352 KB Output is correct
108 Correct 780 ms 18256 KB Output is correct
109 Correct 814 ms 18132 KB Output is correct