Submission #636745

# Submission time Handle Problem Language Result Execution time Memory
636745 2022-08-30T02:13:22 Z sentheta Digital Circuit (IOI22_circuit) C++17
100 / 100
1172 ms 25276 KB
#include "circuit.h"
// author : sentheta aka vanwij
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cassert>
#include<random>
#include<chrono>
#include<cmath>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;

#define Int long long
#define V vector
#define pii pair<int,int>
#define ff first
#define ss second

#define rand() (uniform_int_distribution<int>(0,1<<30)(rng))
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pow2(x) (1LL<<(x))
#define msb(x) (63-__builtin_clzll(x))
#define bitcnt(x) (__builtin_popcountll(x))

#define nl '\n'
#define _ << ' ' <<
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define dbg(x) if(1) cout << "?" << #x << " : " << (x) << endl << flush;

const Int MOD = 1e9+2022;



const int N = 1e5+5;

int n, m;
V<int> p;


#define mid (tl+tr)/2
#define lc v+1
#define rc v+2*(mid-tl+1)

struct Multree{
	Int st[4*N];
	Multree(){
		rep(i,0,4*N) st[i] = 1;
	}
	void upd(int l,int r,int x,int v=0,int tl=0,int tr=n+m-1){
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r){
			(st[v] *= x )%=MOD; return;
		}
		upd(l,r,x, lc,tl,mid);
		upd(l,r,x, rc,mid+1,tr);
	}
	Int qry(int i,int v=0,int tl=0,int tr=n+m-1){
		if(tl==tr) return st[v];
		if(i<=mid) return st[v] *qry(i, lc,tl,mid)%MOD ;
		else return st[v] *qry(i, rc,mid+1,tr)%MOD;
	}
} multree;


struct Fliptree{
	int st[2*N][2];
	bool lz[2*N];
	void assign(int i,int x,int v=0,int tl=0,int tr=m-1){
		if(tl==tr){
			st[v][0] = x; return;
		}
		if(i<=mid) assign(i,x, lc,tl,mid);
		else assign(i,x, rc,mid+1,tr);
		rep(i,0,2)
			(st[v][i] = st[lc][i] + st[rc][i] )%=MOD;
	}
	void flip(int l,int r,int v=0,int tl=0,int tr=m-1){
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r){
			swap(st[v][0], st[v][1]);
			lz[v] ^= 1; return;
		}
		if(lz[v]){
			flip(tl,tr, lc,tl,mid);
			flip(tl,tr, rc,mid+1,tr);
			lz[v] = 0;
		}
		flip(l,r, lc,tl,mid);
		flip(l,r, rc,mid+1,tr);
		rep(i,0,2)
			(st[v][i] = st[lc][i] + st[rc][i] )%=MOD;
	}
	int qry(){
		return st[0][1];
	}
} fliptree;


V<int> adj[2*N];
int in[2*N], out[2*N], timer=0;
Int dp[2*N];
void dfs(int x=0){
	in[x] = timer++;

	if(adj[x].size()==0) dp[x] = 1;
	else dp[x] = adj[x].size();

	for(int y : adj[x]){
		dfs(y);
		(dp[x] *= dp[y] )%=MOD;
	}
	out[x] = timer-1;
}


void init(int _n,int _m,V<int> _p,V<int> a){
	n = _n; m = _m; p.swap(_p);

	rep(i,1,n+m){
		adj[p[i]].push_back(i);
	}
	dfs();

	rep(i,1,n+m){
		// dbg(i); dbg(dp[i]);
		// dbg(in[i]); dbg(out[i]);
		if(in[p[i]]+1 <= in[i]-1){
			// cout << in[p[i]]+1 _ in[i]-1 << nl;
			multree.upd(in[p[i]]+1,in[i]-1, dp[i]);
		}
		if(out[i]+1 <= out[p[i]]){
			// cout << out[i]+1 _ out[p[i]] << nl;
			multree.upd(out[i]+1, out[p[i]], dp[i]);
		}
		// cout << nl;
	}

	rep(i,0,m){
		// dbg(in[n+i]);
		// dbg(multree.qry(in[n+i]));
		fliptree.assign(i, multree.qry(in[n+i]));
		if(a[i]){
			fliptree.flip(i,i);
		}
	}

	// dbg(fliptree.qry());
}

int count_ways(int l,int r){
	// return -1;
	l -= n; r -= n;
	fliptree.flip(l, r);
	return fliptree.qry();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 4 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8144 KB Output is correct
6 Correct 4 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 5 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8272 KB Output is correct
6 Correct 5 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
9 Correct 5 ms 8144 KB Output is correct
10 Correct 7 ms 8272 KB Output is correct
11 Correct 6 ms 8272 KB Output is correct
12 Correct 5 ms 8212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 4 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8144 KB Output is correct
6 Correct 4 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
9 Correct 4 ms 8144 KB Output is correct
10 Correct 5 ms 8144 KB Output is correct
11 Correct 5 ms 8144 KB Output is correct
12 Correct 4 ms 8144 KB Output is correct
13 Correct 5 ms 8272 KB Output is correct
14 Correct 5 ms 8144 KB Output is correct
15 Correct 5 ms 8144 KB Output is correct
16 Correct 5 ms 8144 KB Output is correct
17 Correct 5 ms 8144 KB Output is correct
18 Correct 7 ms 8272 KB Output is correct
19 Correct 6 ms 8272 KB Output is correct
20 Correct 5 ms 8212 KB Output is correct
21 Correct 5 ms 8144 KB Output is correct
22 Correct 4 ms 8144 KB Output is correct
23 Correct 5 ms 8144 KB Output is correct
24 Correct 4 ms 8144 KB Output is correct
25 Correct 5 ms 8144 KB Output is correct
26 Correct 5 ms 8236 KB Output is correct
27 Correct 5 ms 8144 KB Output is correct
28 Correct 7 ms 8256 KB Output is correct
29 Correct 5 ms 8144 KB Output is correct
30 Correct 5 ms 8144 KB Output is correct
31 Correct 4 ms 8144 KB Output is correct
32 Correct 5 ms 8236 KB Output is correct
33 Correct 5 ms 8144 KB Output is correct
34 Correct 5 ms 8144 KB Output is correct
35 Correct 4 ms 8104 KB Output is correct
36 Correct 6 ms 8272 KB Output is correct
37 Correct 5 ms 8272 KB Output is correct
38 Correct 5 ms 8272 KB Output is correct
39 Correct 4 ms 8144 KB Output is correct
40 Correct 5 ms 8144 KB Output is correct
41 Correct 5 ms 8204 KB Output is correct
42 Correct 5 ms 8144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 718 ms 11456 KB Output is correct
2 Correct 903 ms 14888 KB Output is correct
3 Correct 970 ms 14912 KB Output is correct
4 Correct 956 ms 14792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 718 ms 11456 KB Output is correct
2 Correct 903 ms 14888 KB Output is correct
3 Correct 970 ms 14912 KB Output is correct
4 Correct 956 ms 14792 KB Output is correct
5 Correct 796 ms 11476 KB Output is correct
6 Correct 1113 ms 14952 KB Output is correct
7 Correct 1008 ms 14948 KB Output is correct
8 Correct 911 ms 14936 KB Output is correct
9 Correct 436 ms 8272 KB Output is correct
10 Correct 844 ms 8528 KB Output is correct
11 Correct 793 ms 8528 KB Output is correct
12 Correct 968 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 5 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8272 KB Output is correct
6 Correct 5 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
9 Correct 5 ms 8144 KB Output is correct
10 Correct 7 ms 8272 KB Output is correct
11 Correct 6 ms 8272 KB Output is correct
12 Correct 5 ms 8212 KB Output is correct
13 Correct 718 ms 11456 KB Output is correct
14 Correct 903 ms 14888 KB Output is correct
15 Correct 970 ms 14912 KB Output is correct
16 Correct 956 ms 14792 KB Output is correct
17 Correct 796 ms 11476 KB Output is correct
18 Correct 1113 ms 14952 KB Output is correct
19 Correct 1008 ms 14948 KB Output is correct
20 Correct 911 ms 14936 KB Output is correct
21 Correct 436 ms 8272 KB Output is correct
22 Correct 844 ms 8528 KB Output is correct
23 Correct 793 ms 8528 KB Output is correct
24 Correct 968 ms 8528 KB Output is correct
25 Correct 1000 ms 18288 KB Output is correct
26 Correct 1092 ms 18488 KB Output is correct
27 Correct 1172 ms 18408 KB Output is correct
28 Correct 940 ms 18416 KB Output is correct
29 Correct 1016 ms 24640 KB Output is correct
30 Correct 950 ms 24664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 4 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8144 KB Output is correct
6 Correct 4 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
9 Correct 4 ms 8144 KB Output is correct
10 Correct 5 ms 8144 KB Output is correct
11 Correct 5 ms 8144 KB Output is correct
12 Correct 4 ms 8144 KB Output is correct
13 Correct 5 ms 8272 KB Output is correct
14 Correct 5 ms 8144 KB Output is correct
15 Correct 5 ms 8144 KB Output is correct
16 Correct 5 ms 8144 KB Output is correct
17 Correct 5 ms 8144 KB Output is correct
18 Correct 7 ms 8272 KB Output is correct
19 Correct 6 ms 8272 KB Output is correct
20 Correct 5 ms 8212 KB Output is correct
21 Correct 5 ms 8144 KB Output is correct
22 Correct 4 ms 8144 KB Output is correct
23 Correct 5 ms 8144 KB Output is correct
24 Correct 4 ms 8144 KB Output is correct
25 Correct 5 ms 8144 KB Output is correct
26 Correct 5 ms 8236 KB Output is correct
27 Correct 5 ms 8144 KB Output is correct
28 Correct 7 ms 8256 KB Output is correct
29 Correct 5 ms 8144 KB Output is correct
30 Correct 5 ms 8144 KB Output is correct
31 Correct 4 ms 8144 KB Output is correct
32 Correct 5 ms 8236 KB Output is correct
33 Correct 5 ms 8144 KB Output is correct
34 Correct 5 ms 8144 KB Output is correct
35 Correct 4 ms 8104 KB Output is correct
36 Correct 6 ms 8272 KB Output is correct
37 Correct 5 ms 8272 KB Output is correct
38 Correct 5 ms 8272 KB Output is correct
39 Correct 4 ms 8144 KB Output is correct
40 Correct 5 ms 8144 KB Output is correct
41 Correct 5 ms 8204 KB Output is correct
42 Correct 5 ms 8144 KB Output is correct
43 Correct 406 ms 8400 KB Output is correct
44 Correct 758 ms 8400 KB Output is correct
45 Correct 707 ms 8400 KB Output is correct
46 Correct 646 ms 8580 KB Output is correct
47 Correct 751 ms 8656 KB Output is correct
48 Correct 742 ms 8656 KB Output is correct
49 Correct 960 ms 8672 KB Output is correct
50 Correct 648 ms 8656 KB Output is correct
51 Correct 863 ms 8400 KB Output is correct
52 Correct 862 ms 8400 KB Output is correct
53 Correct 769 ms 8656 KB Output is correct
54 Correct 853 ms 8656 KB Output is correct
55 Correct 898 ms 8528 KB Output is correct
56 Correct 845 ms 8400 KB Output is correct
57 Correct 895 ms 8400 KB Output is correct
58 Correct 931 ms 8912 KB Output is correct
59 Correct 744 ms 9020 KB Output is correct
60 Correct 781 ms 9016 KB Output is correct
61 Correct 884 ms 8468 KB Output is correct
62 Correct 857 ms 8400 KB Output is correct
63 Correct 859 ms 8400 KB Output is correct
64 Correct 876 ms 8400 KB Output is correct
65 Correct 509 ms 8272 KB Output is correct
66 Correct 984 ms 8528 KB Output is correct
67 Correct 862 ms 8528 KB Output is correct
68 Correct 800 ms 8548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8144 KB Output is correct
2 Correct 4 ms 8144 KB Output is correct
3 Correct 5 ms 8144 KB Output is correct
4 Correct 4 ms 8144 KB Output is correct
5 Correct 5 ms 8144 KB Output is correct
6 Correct 4 ms 8144 KB Output is correct
7 Correct 5 ms 8144 KB Output is correct
8 Correct 5 ms 8144 KB Output is correct
9 Correct 4 ms 8144 KB Output is correct
10 Correct 5 ms 8144 KB Output is correct
11 Correct 5 ms 8144 KB Output is correct
12 Correct 4 ms 8144 KB Output is correct
13 Correct 5 ms 8272 KB Output is correct
14 Correct 5 ms 8144 KB Output is correct
15 Correct 5 ms 8144 KB Output is correct
16 Correct 5 ms 8144 KB Output is correct
17 Correct 5 ms 8144 KB Output is correct
18 Correct 7 ms 8272 KB Output is correct
19 Correct 6 ms 8272 KB Output is correct
20 Correct 5 ms 8212 KB Output is correct
21 Correct 5 ms 8144 KB Output is correct
22 Correct 4 ms 8144 KB Output is correct
23 Correct 5 ms 8144 KB Output is correct
24 Correct 4 ms 8144 KB Output is correct
25 Correct 5 ms 8144 KB Output is correct
26 Correct 5 ms 8236 KB Output is correct
27 Correct 5 ms 8144 KB Output is correct
28 Correct 7 ms 8256 KB Output is correct
29 Correct 5 ms 8144 KB Output is correct
30 Correct 5 ms 8144 KB Output is correct
31 Correct 4 ms 8144 KB Output is correct
32 Correct 5 ms 8236 KB Output is correct
33 Correct 5 ms 8144 KB Output is correct
34 Correct 5 ms 8144 KB Output is correct
35 Correct 4 ms 8104 KB Output is correct
36 Correct 6 ms 8272 KB Output is correct
37 Correct 5 ms 8272 KB Output is correct
38 Correct 5 ms 8272 KB Output is correct
39 Correct 4 ms 8144 KB Output is correct
40 Correct 5 ms 8144 KB Output is correct
41 Correct 5 ms 8204 KB Output is correct
42 Correct 5 ms 8144 KB Output is correct
43 Correct 718 ms 11456 KB Output is correct
44 Correct 903 ms 14888 KB Output is correct
45 Correct 970 ms 14912 KB Output is correct
46 Correct 956 ms 14792 KB Output is correct
47 Correct 796 ms 11476 KB Output is correct
48 Correct 1113 ms 14952 KB Output is correct
49 Correct 1008 ms 14948 KB Output is correct
50 Correct 911 ms 14936 KB Output is correct
51 Correct 436 ms 8272 KB Output is correct
52 Correct 844 ms 8528 KB Output is correct
53 Correct 793 ms 8528 KB Output is correct
54 Correct 968 ms 8528 KB Output is correct
55 Correct 1000 ms 18288 KB Output is correct
56 Correct 1092 ms 18488 KB Output is correct
57 Correct 1172 ms 18408 KB Output is correct
58 Correct 940 ms 18416 KB Output is correct
59 Correct 1016 ms 24640 KB Output is correct
60 Correct 950 ms 24664 KB Output is correct
61 Correct 406 ms 8400 KB Output is correct
62 Correct 758 ms 8400 KB Output is correct
63 Correct 707 ms 8400 KB Output is correct
64 Correct 646 ms 8580 KB Output is correct
65 Correct 751 ms 8656 KB Output is correct
66 Correct 742 ms 8656 KB Output is correct
67 Correct 960 ms 8672 KB Output is correct
68 Correct 648 ms 8656 KB Output is correct
69 Correct 863 ms 8400 KB Output is correct
70 Correct 862 ms 8400 KB Output is correct
71 Correct 769 ms 8656 KB Output is correct
72 Correct 853 ms 8656 KB Output is correct
73 Correct 898 ms 8528 KB Output is correct
74 Correct 845 ms 8400 KB Output is correct
75 Correct 895 ms 8400 KB Output is correct
76 Correct 931 ms 8912 KB Output is correct
77 Correct 744 ms 9020 KB Output is correct
78 Correct 781 ms 9016 KB Output is correct
79 Correct 884 ms 8468 KB Output is correct
80 Correct 857 ms 8400 KB Output is correct
81 Correct 859 ms 8400 KB Output is correct
82 Correct 876 ms 8400 KB Output is correct
83 Correct 509 ms 8272 KB Output is correct
84 Correct 984 ms 8528 KB Output is correct
85 Correct 862 ms 8528 KB Output is correct
86 Correct 800 ms 8548 KB Output is correct
87 Correct 4 ms 8144 KB Output is correct
88 Correct 645 ms 17340 KB Output is correct
89 Correct 741 ms 14860 KB Output is correct
90 Correct 967 ms 14544 KB Output is correct
91 Correct 951 ms 18500 KB Output is correct
92 Correct 1093 ms 18520 KB Output is correct
93 Correct 962 ms 18376 KB Output is correct
94 Correct 1065 ms 18500 KB Output is correct
95 Correct 863 ms 18672 KB Output is correct
96 Correct 969 ms 13584 KB Output is correct
97 Correct 984 ms 13508 KB Output is correct
98 Correct 809 ms 19880 KB Output is correct
99 Correct 1028 ms 18432 KB Output is correct
100 Correct 887 ms 15680 KB Output is correct
101 Correct 989 ms 14904 KB Output is correct
102 Correct 1066 ms 13444 KB Output is correct
103 Correct 766 ms 24792 KB Output is correct
104 Correct 985 ms 25160 KB Output is correct
105 Correct 862 ms 25276 KB Output is correct
106 Correct 956 ms 15692 KB Output is correct
107 Correct 904 ms 13632 KB Output is correct
108 Correct 1022 ms 13636 KB Output is correct
109 Correct 1086 ms 13536 KB Output is correct