Submission #1063681

# Submission time Handle Problem Language Result Execution time Memory
1063681 2024-08-17T23:24:55 Z Arapak Digital Circuit (IOI22_circuit) C++17
100 / 100
786 ms 30936 KB
#include "circuit.h"
#include "bits/stdc++.h"

using namespace std;

#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#ifdef DEBUG
auto& operator<<(auto& os, pair<auto, auto> &p) {
	return os<<"("<<p.first<<", "<<p.second<<")";
}
auto& operator<<(auto& os, const auto &v) {
	os<<"{";
	for(auto it=begin(v);it!=end(v);++it) {
		if(it != begin(v)) os<<", ";
		os<<(*it);
	}
	return os<<"}";
}

void dbg_out(auto... x) {
	((cerr<<' '<<x), ...) << endl;
}
#define dbg(x...) cerr<<"("<<#x<<"):", dbg_out(x);
#else
#define dbg(...)
#endif

const ll mod = 1000002022;

struct SegTree {
	int base;
	vector<ll> tree, inv;
	vector<bool> lazy;
	void merge(int v) {
		tree[v] = (tree[v*2] + tree[v*2+1]) % mod;
		inv[v] = (inv[v*2] + inv[v*2+1]) % mod;
	}

	void build(int v, int l, int r, const vector<ll> &vals, const vector<int> &a) {
		if(l+1 == r) {
			tree[v] = l < sz(vals) ? vals[l] : 0;
			if(l < sz(vals) && !a[l]) swap(tree[v], inv[v]);
			return;
		}
		int mid = (l + r) / 2;
		build(v*2, l, mid, vals, a);
		build(v*2+1, mid, r, vals, a);
		merge(v);
	}

	SegTree(const vector<ll> &vals, const vector<int> &a) {
		// base = bit_ceil(uint(vals.size()));
		base = 1;
		while(base < sz(vals))
			base *= 2;
		dbg(base);
		tree.assign(2*base, 0);
		inv.assign(2*base, 0);
		lazy.assign(2*base, 0);
		build(1, 0, base, vals, a);
	}

	void push(int v) {
		if(lazy[v]) {
			swap(tree[v*2], inv[v*2]);
			lazy[v*2] = !lazy[v*2];
			swap(tree[v*2+1], inv[v*2+1]);
			lazy[v*2+1] = !lazy[v*2+1];
			lazy[v] = false;
		}
	}

	void update(int v, int l, int r, int a, int b) {
		if(r <= a || b <= l) return;
		if(a <= l && r <= b) {
			swap(tree[v], inv[v]);
			lazy[v] = !lazy[v];
			return;
		}
		push(v);
		int mid = (l + r) / 2;
		update(v*2, l, mid, a, b);
		update(v*2+1, mid, r, a, b);
		merge(v);
	}
};

int n, m;
vi p, a;
vector<vi> g;

vector<ll> mult;
void dfs_mult(int v) {
	if(v >= n) return;
	dbg(v, g[v]);
	mult[v] = ll(sz(g[v]));
	for(auto e : g[v]) {
		dfs_mult(e);
		if(e < n)
			mult[v] = mult[v] * mult[e] % mod;
	}
}

vector<ll> weight;
void dfs(int v, ll w) {
	if(v >= n) {
		weight[v - n] = w;
		return;
	}
	dbg(v, w);
	vector<ll> pref(sz(g[v]) + 1, 1), suff(sz(g[v]) + 1, 1);
	rep(i,0,sz(g[v])) {
		pref[i+1] = (pref[i] * mult[g[v][i]]) % mod;
		suff[i+1] = (suff[i] * mult[g[v][sz(g[v]) -i-1]]) % mod;
	}
	dbg(pref, suff);

	rep(i,0,sz(g[v])) {
		ll we = pref[i] * suff[sz(g[v]) - i - 1] % mod;
		dfs(g[v][i], w * we % mod);
	}
}

SegTree *tree;

void init(int N, int M, vi P, vi A) {
	n = N; m = M; p = P; a = A;
	g.resize(n);
	rep(i,1,n+m)
		g[p[i]].push_back(i);
	mult.assign(n+m, 1);
	dfs_mult(0);
	dbg(mult);
	weight.resize(m);
	dfs(0, 1);
	dbg(weight);
	tree = new SegTree(weight, a);
}

int count_ways(int L, int R) {
	tree->update(1,0,tree->base,L-n,R-n+1);
	dbg(tree->tree);
	dbg(tree->inv);
	dbg(tree->lazy);
	return (int)tree->tree[1];

	// rep(i,L-n,R-n+1)
	// 	a[i] = !a[i];

	// ll res = 0;
	// rep(i,0,m)
	// 	if(a[i]) res = (res + weight[i]) % mod;

	// return (int)res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 488 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 488 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 456 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 544 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 469 ms 5208 KB Output is correct
2 Correct 700 ms 9808 KB Output is correct
3 Correct 656 ms 9816 KB Output is correct
4 Correct 678 ms 9808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 469 ms 5208 KB Output is correct
2 Correct 700 ms 9808 KB Output is correct
3 Correct 656 ms 9816 KB Output is correct
4 Correct 678 ms 9808 KB Output is correct
5 Correct 530 ms 5208 KB Output is correct
6 Correct 668 ms 9816 KB Output is correct
7 Correct 705 ms 9952 KB Output is correct
8 Correct 708 ms 9816 KB Output is correct
9 Correct 331 ms 600 KB Output is correct
10 Correct 670 ms 856 KB Output is correct
11 Correct 614 ms 856 KB Output is correct
12 Correct 640 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 469 ms 5208 KB Output is correct
14 Correct 700 ms 9808 KB Output is correct
15 Correct 656 ms 9816 KB Output is correct
16 Correct 678 ms 9808 KB Output is correct
17 Correct 530 ms 5208 KB Output is correct
18 Correct 668 ms 9816 KB Output is correct
19 Correct 705 ms 9952 KB Output is correct
20 Correct 708 ms 9816 KB Output is correct
21 Correct 331 ms 600 KB Output is correct
22 Correct 670 ms 856 KB Output is correct
23 Correct 614 ms 856 KB Output is correct
24 Correct 640 ms 856 KB Output is correct
25 Correct 704 ms 15696 KB Output is correct
26 Correct 744 ms 15688 KB Output is correct
27 Correct 742 ms 15696 KB Output is correct
28 Correct 599 ms 15704 KB Output is correct
29 Correct 786 ms 28752 KB Output is correct
30 Correct 724 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 488 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 456 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 544 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 467 ms 856 KB Output is correct
44 Correct 624 ms 856 KB Output is correct
45 Correct 709 ms 856 KB Output is correct
46 Correct 627 ms 1112 KB Output is correct
47 Correct 699 ms 1112 KB Output is correct
48 Correct 639 ms 1112 KB Output is correct
49 Correct 646 ms 1112 KB Output is correct
50 Correct 687 ms 1112 KB Output is correct
51 Correct 618 ms 1008 KB Output is correct
52 Correct 679 ms 1016 KB Output is correct
53 Correct 584 ms 1624 KB Output is correct
54 Correct 622 ms 1112 KB Output is correct
55 Correct 653 ms 856 KB Output is correct
56 Correct 639 ms 856 KB Output is correct
57 Correct 621 ms 856 KB Output is correct
58 Correct 634 ms 1624 KB Output is correct
59 Correct 638 ms 1880 KB Output is correct
60 Correct 656 ms 1880 KB Output is correct
61 Correct 660 ms 1112 KB Output is correct
62 Correct 602 ms 856 KB Output is correct
63 Correct 624 ms 600 KB Output is correct
64 Correct 641 ms 856 KB Output is correct
65 Correct 296 ms 600 KB Output is correct
66 Correct 656 ms 856 KB Output is correct
67 Correct 654 ms 856 KB Output is correct
68 Correct 625 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 488 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 456 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 544 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 469 ms 5208 KB Output is correct
44 Correct 700 ms 9808 KB Output is correct
45 Correct 656 ms 9816 KB Output is correct
46 Correct 678 ms 9808 KB Output is correct
47 Correct 530 ms 5208 KB Output is correct
48 Correct 668 ms 9816 KB Output is correct
49 Correct 705 ms 9952 KB Output is correct
50 Correct 708 ms 9816 KB Output is correct
51 Correct 331 ms 600 KB Output is correct
52 Correct 670 ms 856 KB Output is correct
53 Correct 614 ms 856 KB Output is correct
54 Correct 640 ms 856 KB Output is correct
55 Correct 704 ms 15696 KB Output is correct
56 Correct 744 ms 15688 KB Output is correct
57 Correct 742 ms 15696 KB Output is correct
58 Correct 599 ms 15704 KB Output is correct
59 Correct 786 ms 28752 KB Output is correct
60 Correct 724 ms 28752 KB Output is correct
61 Correct 467 ms 856 KB Output is correct
62 Correct 624 ms 856 KB Output is correct
63 Correct 709 ms 856 KB Output is correct
64 Correct 627 ms 1112 KB Output is correct
65 Correct 699 ms 1112 KB Output is correct
66 Correct 639 ms 1112 KB Output is correct
67 Correct 646 ms 1112 KB Output is correct
68 Correct 687 ms 1112 KB Output is correct
69 Correct 618 ms 1008 KB Output is correct
70 Correct 679 ms 1016 KB Output is correct
71 Correct 584 ms 1624 KB Output is correct
72 Correct 622 ms 1112 KB Output is correct
73 Correct 653 ms 856 KB Output is correct
74 Correct 639 ms 856 KB Output is correct
75 Correct 621 ms 856 KB Output is correct
76 Correct 634 ms 1624 KB Output is correct
77 Correct 638 ms 1880 KB Output is correct
78 Correct 656 ms 1880 KB Output is correct
79 Correct 660 ms 1112 KB Output is correct
80 Correct 602 ms 856 KB Output is correct
81 Correct 624 ms 600 KB Output is correct
82 Correct 641 ms 856 KB Output is correct
83 Correct 296 ms 600 KB Output is correct
84 Correct 656 ms 856 KB Output is correct
85 Correct 654 ms 856 KB Output is correct
86 Correct 625 ms 856 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 474 ms 14736 KB Output is correct
89 Correct 731 ms 10284 KB Output is correct
90 Correct 654 ms 9560 KB Output is correct
91 Correct 652 ms 15952 KB Output is correct
92 Correct 764 ms 15952 KB Output is correct
93 Correct 741 ms 15952 KB Output is correct
94 Correct 690 ms 15952 KB Output is correct
95 Correct 651 ms 15996 KB Output is correct
96 Correct 636 ms 8880 KB Output is correct
97 Correct 678 ms 8880 KB Output is correct
98 Correct 617 ms 24912 KB Output is correct
99 Correct 753 ms 15884 KB Output is correct
100 Correct 677 ms 12112 KB Output is correct
101 Correct 675 ms 10840 KB Output is correct
102 Correct 744 ms 8784 KB Output is correct
103 Correct 734 ms 28752 KB Output is correct
104 Correct 729 ms 30936 KB Output is correct
105 Correct 712 ms 30936 KB Output is correct
106 Correct 714 ms 12740 KB Output is correct
107 Correct 724 ms 10072 KB Output is correct
108 Correct 737 ms 9816 KB Output is correct
109 Correct 695 ms 9040 KB Output is correct