Submission #808661

# Submission time Handle Problem Language Result Execution time Memory
808661 2023-08-05T09:00:56 Z alex_2008 Digital Circuit (IOI22_circuit) C++17
61 / 100
3000 ms 7848 KB
#include "circuit.h"
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
int NN, MM;
const ll N = 2e5 + 10, mod = 1000002022;
int p[N], d[N];
int lazy[4 * N];
int a[N], cnt[N];
ll pref[N];
bool ch = true, ch2 = true;
pair <ll, ll> tree[4 * N];
ll tree2[4 * N];
ll binpow(ll a, ll n) {
	if (n == 0) return 1LL;
	if (n % 2 == 0) {
		ll u = binpow(a, n / 2);
		return (u * u) % mod;
	}
	return (a * binpow(a, n - 1)) % mod;
}
void pushh(int v, int tl, int tr) {
	if (lazy[v]) {
		lazy[v] = 0;
		if (tl != tr) {
			lazy[2 * v] = (lazy[2 * v] + 1) % 2;
			lazy[2 * v + 1] = (lazy[2 * v + 1] + 1) % 2;
			swap(tree[2 * v].first, tree[2 * v].second);
			swap(tree[2 * v + 1].first, tree[2 * v + 1].second);
		}
	}
}
void build_tree(int v, int tl, int tr) {
	if (tl == tr) {
		if (a[tl] == 1) tree[v] = { 1, 0 };
		else tree[v] = { 0, 1 };
	}
	else {
		int tm = (tl + tr) / 2;
		build_tree(2 * v, tl, tm);
		build_tree(2 * v + 1, tm + 1, tr);
		ll a = tree[2 * v].first, b = tree[2 * v].second, c = tree[2 * v + 1].first, d = tree[2 * v + 1].second;
		tree[v].first = 2 * a * c + a * d + b * c;
		tree[v].second = 2 * b * d + a * d + b * c;
		tree[v].first %= mod;
		tree[v].second %= mod;
	}
}
void update(int v, int tl, int tr, int l, int r) {
	if (tl > r || tr < l) return;
	if (tl >= l && tr <= r) {
		lazy[v] = (lazy[v] + 1) % 2;
		swap(tree[v].first, tree[v].second);
		return;
	}
	pushh(v, tl, tr);
	int tm = (tl + tr) / 2;
	update(2 * v, tl, tm, l, r);
	update(2 * v + 1, tm + 1, tr, l, r);
	ll a = tree[2 * v].first, b = tree[2 * v].second, c = tree[2 * v + 1].first, d = tree[2 * v + 1].second;
	tree[v].first = 2 * a * c + a * d + b * c;
	tree[v].second = 2 * b * d + a * d + b * c;
	tree[v].first %= mod;
	tree[v].second %= mod;
}
void build_tree2(int v, int tl, int tr) {
	if (tl == tr) {
		if (a[tl]) {
			tree2[v] = pref[tl];
			if (tl) tree2[v] -= pref[tl - 1];
			tree2[v] = (tree2[v] + mod) % mod;
		}
		else tree2[v] = 0;
	}
	else {
		int tm = (tl + tr) / 2;
		build_tree2(2 * v, tl, tm);
		build_tree2(2 * v + 1, tm + 1, tr);
		tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
		tree2[v] %= mod;
	}
}
void pushh2(int v, int tl, int tr) {
	if (lazy[v]) {
		lazy[v] = 0;
		if (tl != tr) {
			lazy[2 * v] = (lazy[2 * v] + 1) % 2;
			lazy[2 * v + 1] = (lazy[2 * v + 1] + 1) % 2;
			int tm = (tl + tr) / 2;
			ll u = pref[tm];
			if (tl) u -= pref[tl - 1];
			u += mod;
			u %= mod;
			tree2[2 * v] = (u + mod - tree2[2 * v]) % mod;
			u = pref[tr] - pref[tm] + mod;
			u %= mod;
			tree2[2 * v + 1] = (u + mod - tree2[2 * v + 1]) % mod;
		}
	}
}
void update2(int v, int tl, int tr, int l, int r) {
	if (tl > r || tr < l) return;
	if (tl >= l && tr <= r) {
		lazy[v] = (lazy[v] + 1) % 2;
		ll u = pref[tr];
		if (tl) u -= pref[tl - 1];
		u += mod;
		u %= mod;
		tree2[v] = (u - tree2[v] + mod) % mod;
		return;
	}
	pushh2(v, tl, tr);
	int tm = (tl + tr) / 2;
	update2(2 * v, tl, tm, l, r);
	update2(2 * v + 1, tm + 1, tr, l, r);
	tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
	tree2[v] %= mod;
}
pair<ll, ll> dp[N];
vector <vector<int>> G;
void dfs(int v, int p) {
	vector <int> childrens;
	for (auto it : G[v]) {
		if (it == p) continue;
		dfs(it, v);
		childrens.push_back(it);
	}
	if (childrens.empty()) {
		dp[v].first = 1;
		dp[v].second = 0;
		if (a[v - NN] == 0) swap(dp[v].first, dp[v].second);
		return;
	}
	vector <vector<ll>> w((int)childrens.size());
	for (int i = 0; i < (int)w.size(); i++) {
		w[i].resize(i + 2);
	}
	w[0][0] = dp[childrens[0]].second;
	w[0][1] = dp[childrens[0]].first;
	for (int i = 1; i < (int)childrens.size(); i++) {
		for (int j = 0; j <= i + 1; j++) {
			w[i][j] = 0;
			if (j != i + 1) w[i][j] += w[i - 1][j] * dp[childrens[i]].second;
			if (j > 0) w[i][j] += w[i - 1][j - 1] * dp[childrens[i]].first;
			w[i][j] %= mod;
		}
	}
	ll ff = 0, ss = 0;
	for (int j = 0; j <= (int)childrens.size(); j++) {
		ll u = w[(int)childrens.size() - 1][j] * (ll(j));
		u %= mod;
		ff += u;
		ff %= mod;
		u = w[(int)childrens.size() - 1][j] * ((ll)childrens.size() - ll(j));
		u %= mod;
		ss += u;
		ss %= mod;
	}
	dp[v].first = ff;
	dp[v].second = ss;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
	NN = N, MM = M;
	for (int i = 0; i < N + M; i++) {
		p[i] = P[i];
		if (i == 0) d[0] = 0;
		if (i > 0 && p[i] != (i - 1) / 2) ch = false;
		if (i) {
			d[i] = d[P[i]] + 1;
			cnt[P[i]]++;
		}
	}
	for (int i = 0; i < N; i++) {
		if (cnt[i] != 2) ch2 = false;
	}
	for (int i = 0; i < M; i++) {
		a[i] = A[i];
	}
	if (M != N + 1) ch = false;
	int u = log2(M);
	if ((1 << u) != M) ch = false;
	if (ch) build_tree(1, 0, M - 1);
	else if (ch2) {
		for (int i = 0; i < M; i++) {
			pref[i] = binpow(2, N - d[i + N]);
			if (i) pref[i] += pref[i - 1];
			pref[i] %= mod;
		}
		build_tree2(1, 0, MM - 1);
	}
	else {
		G.resize(N + M);
		for (int i = 1; i < N + M; i++) {
			G[p[i]].push_back(i);
		}
		dfs(0, -1);
	}
}
int count_ways(int L, int R) {
	if (ch) {
		update(1, 0, MM - 1, L - NN, R - NN);
		return tree[1].first;
	}
	else if (ch2) {
		update2(1, 0, MM - 1, L - NN, R - NN);
		return tree2[1];
	}
	else {
		for (int i = L - NN; i <= R - NN; i++) {
			a[i] = (a[i] + 1) % 2;
		}
		dfs(0, -1);
		return dp[0].first;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 12 ms 4132 KB Output is correct
4 Correct 12 ms 4368 KB Output is correct
5 Correct 13 ms 4368 KB Output is correct
6 Correct 15 ms 4356 KB Output is correct
7 Correct 14 ms 4368 KB Output is correct
8 Correct 12 ms 4368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 404 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 12 ms 4132 KB Output is correct
4 Correct 12 ms 4368 KB Output is correct
5 Correct 13 ms 4368 KB Output is correct
6 Correct 15 ms 4356 KB Output is correct
7 Correct 14 ms 4368 KB Output is correct
8 Correct 12 ms 4368 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 404 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 464 KB Output is correct
26 Correct 2 ms 464 KB Output is correct
27 Correct 2 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 12 ms 4368 KB Output is correct
30 Correct 12 ms 4388 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 3 ms 588 KB Output is correct
36 Correct 2 ms 592 KB Output is correct
37 Correct 16 ms 4592 KB Output is correct
38 Correct 13 ms 4620 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 543 ms 2880 KB Output is correct
2 Correct 767 ms 5436 KB Output is correct
3 Correct 779 ms 5424 KB Output is correct
4 Correct 761 ms 5428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 543 ms 2880 KB Output is correct
2 Correct 767 ms 5436 KB Output is correct
3 Correct 779 ms 5424 KB Output is correct
4 Correct 761 ms 5428 KB Output is correct
5 Correct 619 ms 2896 KB Output is correct
6 Correct 846 ms 5420 KB Output is correct
7 Correct 888 ms 5408 KB Output is correct
8 Correct 910 ms 5420 KB Output is correct
9 Correct 242 ms 464 KB Output is correct
10 Correct 563 ms 592 KB Output is correct
11 Correct 819 ms 680 KB Output is correct
12 Correct 585 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 404 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 543 ms 2880 KB Output is correct
14 Correct 767 ms 5436 KB Output is correct
15 Correct 779 ms 5424 KB Output is correct
16 Correct 761 ms 5428 KB Output is correct
17 Correct 619 ms 2896 KB Output is correct
18 Correct 846 ms 5420 KB Output is correct
19 Correct 888 ms 5408 KB Output is correct
20 Correct 910 ms 5420 KB Output is correct
21 Correct 242 ms 464 KB Output is correct
22 Correct 563 ms 592 KB Output is correct
23 Correct 819 ms 680 KB Output is correct
24 Correct 585 ms 592 KB Output is correct
25 Correct 725 ms 7752 KB Output is correct
26 Correct 599 ms 7848 KB Output is correct
27 Correct 938 ms 7828 KB Output is correct
28 Correct 546 ms 7844 KB Output is correct
29 Correct 829 ms 7844 KB Output is correct
30 Correct 661 ms 7844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 12 ms 4132 KB Output is correct
4 Correct 12 ms 4368 KB Output is correct
5 Correct 13 ms 4368 KB Output is correct
6 Correct 15 ms 4356 KB Output is correct
7 Correct 14 ms 4368 KB Output is correct
8 Correct 12 ms 4368 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 404 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 464 KB Output is correct
26 Correct 2 ms 464 KB Output is correct
27 Correct 2 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 12 ms 4368 KB Output is correct
30 Correct 12 ms 4388 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 3 ms 588 KB Output is correct
36 Correct 2 ms 592 KB Output is correct
37 Correct 16 ms 4592 KB Output is correct
38 Correct 13 ms 4620 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 368 KB Output is correct
43 Execution timed out 3019 ms 720 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 12 ms 4132 KB Output is correct
4 Correct 12 ms 4368 KB Output is correct
5 Correct 13 ms 4368 KB Output is correct
6 Correct 15 ms 4356 KB Output is correct
7 Correct 14 ms 4368 KB Output is correct
8 Correct 12 ms 4368 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 404 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 464 KB Output is correct
26 Correct 2 ms 464 KB Output is correct
27 Correct 2 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 12 ms 4368 KB Output is correct
30 Correct 12 ms 4388 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 3 ms 588 KB Output is correct
36 Correct 2 ms 592 KB Output is correct
37 Correct 16 ms 4592 KB Output is correct
38 Correct 13 ms 4620 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 368 KB Output is correct
43 Correct 543 ms 2880 KB Output is correct
44 Correct 767 ms 5436 KB Output is correct
45 Correct 779 ms 5424 KB Output is correct
46 Correct 761 ms 5428 KB Output is correct
47 Correct 619 ms 2896 KB Output is correct
48 Correct 846 ms 5420 KB Output is correct
49 Correct 888 ms 5408 KB Output is correct
50 Correct 910 ms 5420 KB Output is correct
51 Correct 242 ms 464 KB Output is correct
52 Correct 563 ms 592 KB Output is correct
53 Correct 819 ms 680 KB Output is correct
54 Correct 585 ms 592 KB Output is correct
55 Correct 725 ms 7752 KB Output is correct
56 Correct 599 ms 7848 KB Output is correct
57 Correct 938 ms 7828 KB Output is correct
58 Correct 546 ms 7844 KB Output is correct
59 Correct 829 ms 7844 KB Output is correct
60 Correct 661 ms 7844 KB Output is correct
61 Execution timed out 3019 ms 720 KB Time limit exceeded
62 Halted 0 ms 0 KB -