Submission #719079

# Submission time Handle Problem Language Result Execution time Memory
719079 2023-04-05T10:06:19 Z baojiaopisu Digital Circuit (IOI22_circuit) C++17
100 / 100
1141 ms 44196 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;

#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz

#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y)
        {
            x = y;
            return true;
        }
        return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y)
        {
            x = y;
            return true;
        }
        return false;
    }

const int MOD = 1000002022; //998244353

template<class X, class Y>
	void add(X &x, const Y &y) {
		x = (x + y);
		if(x >= MOD) x -= MOD;
	}

template<class X, class Y> 
	void sub(X &x, const Y &y) {
		x = (x - y);
		if(x < 0) x += MOD;
	}

/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/

const ll INF = 1e9;
const int N = 2e5 + 10;

vector<int> g[N];
ll f[N], val[N];
vector<ll> v;

struct SegTree {
	int n;
	struct Node {
		ll one, zero;
		int lazy;

		Node(ll _one = 0, ll _zero = 0, int _lazy = 0) {
			one = _one;
			zero = _zero;
			lazy = _lazy;
		}
	};
	vector<Node> node;

	SegTree(int _n = 0) {
		n = _n;
		node.resize(4 * n + 7);
		if(n > 0) build(0, n - 1, 1);
	}
private:
	void Down(int id) {
		if(!node[id].lazy) return;
		node[id].lazy = 0;

		for(int i = (id << 1); i <= (id << 1 | 1); i++) {
			swap(node[i].one, node[i].zero);
			node[i].lazy ^= 1;
		}
	}

	void build(int l, int r, int id) {
		if(l == r) {
			node[id].zero = v[l];
			return;
		}
		int mid = (l + r) >> 1;
		build(l, mid, id << 1);
		build(mid + 1, r, id << 1 | 1);
		node[id] = merge(node[id << 1], node[id << 1 | 1]);
	}

	Node merge(const Node &a, const Node &b) {
		Node ans = Node();
		ans.one = (a.one + b.one) % MOD;
		ans.zero = (a.zero + b.zero) % MOD;
		return ans;
	}

	void Update(int l, int r, int lo, int hi, int id) {
		if(l > hi || r < lo) return;
		if(lo <= l && r <= hi) {
			swap(node[id].one, node[id].zero);
			node[id].lazy ^= 1;
			return;
		}

		int mid = (l + r) >> 1;
		Down(id);
		Update(l, mid, lo, hi, id << 1);
		Update(mid + 1, r, lo, hi, id << 1 | 1);
		node[id] = merge(node[id << 1], node[id << 1 | 1]);
	}
public:
	void update(int l, int r) {
		Update(0, n - 1, l, r, 1);
	}
};
vector<int> state;
SegTree IT = SegTree(0);

int n, m;
ll coef = 0;

void dfs(int u) {
	f[u] = 1;
	if(u >= n) return;
	for(auto v : g[u]) {
		dfs(v);
		f[u] = (f[u] * f[v]) % MOD;
	}

	f[u] = f[u] * g[u].size() % MOD;
	// IT = SegTree(m);
}

void dfs2(int u) {
	if(u >= n) return;
	int sz = g[u].size();
	vector<ll> pref(sz), suff(sz);
	pref[0] = f[g[u][0]];
	suff[sz - 1] = f[g[u][sz - 1]];

	for(int i = 1; i < sz; i++) {
		pref[i] = pref[i - 1] * f[g[u][i]] % MOD;
	}

	for(int i = sz - 2; i >= 0; i--) {
		suff[i] = suff[i + 1] * f[g[u][i]] % MOD;
	}

	for(int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		val[v] = val[u];
		if(i > 0) val[v] = (val[v] * pref[i - 1]) % MOD;
		if(i + 1 < g[u].size()) val[v] = (val[v] * suff[i + 1]) % MOD;
		dfs2(v);
	}
}


void init(int N, int M, vector<int> p, vector<int> A) {
	for(int i = 1; i < N + M; i++) {
		g[p[i]].pb(i);
	}
	n = N; m = M;
	state = A;
	coef = 1;
	dfs(0);
	val[0] = 1;
	dfs2(0);
	v.resize(m);
	for(int i = 0; i < m; i++) v[i] = val[i + n];
	IT = SegTree(m);
	for(int i = 0; i < m; i++) {
		if(state[i]) IT.update(i, i);
	}
}

ll count_ways(int l, int r) {
	l -= n;
	r -= n;

	IT.update(l, r);
	return (coef * IT.node[1].one) % MOD;
}

Compilation message

circuit.cpp: In function 'void dfs2(int)':
circuit.cpp:176:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |  for(int i = 0; i < g[u].size(); i++) {
      |                 ~~^~~~~~~~~~~~~
circuit.cpp:180:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |   if(i + 1 < g[u].size()) val[v] = (val[v] * suff[i + 1]) % MOD;
      |      ~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5156 KB Output is correct
5 Correct 5 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 4 ms 5328 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5156 KB Output is correct
5 Correct 5 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5084 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 4 ms 5328 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 4 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 5 ms 5200 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 4 ms 5136 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 751 ms 11288 KB Output is correct
2 Correct 890 ms 17552 KB Output is correct
3 Correct 966 ms 17480 KB Output is correct
4 Correct 810 ms 17536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 751 ms 11288 KB Output is correct
2 Correct 890 ms 17552 KB Output is correct
3 Correct 966 ms 17480 KB Output is correct
4 Correct 810 ms 17536 KB Output is correct
5 Correct 821 ms 11208 KB Output is correct
6 Correct 1041 ms 17536 KB Output is correct
7 Correct 836 ms 17564 KB Output is correct
8 Correct 742 ms 17488 KB Output is correct
9 Correct 457 ms 5328 KB Output is correct
10 Correct 910 ms 5712 KB Output is correct
11 Correct 771 ms 5712 KB Output is correct
12 Correct 879 ms 5712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5084 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 3 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 4 ms 5328 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 751 ms 11288 KB Output is correct
14 Correct 890 ms 17552 KB Output is correct
15 Correct 966 ms 17480 KB Output is correct
16 Correct 810 ms 17536 KB Output is correct
17 Correct 821 ms 11208 KB Output is correct
18 Correct 1041 ms 17536 KB Output is correct
19 Correct 836 ms 17564 KB Output is correct
20 Correct 742 ms 17488 KB Output is correct
21 Correct 457 ms 5328 KB Output is correct
22 Correct 910 ms 5712 KB Output is correct
23 Correct 771 ms 5712 KB Output is correct
24 Correct 879 ms 5712 KB Output is correct
25 Correct 1076 ms 23848 KB Output is correct
26 Correct 1141 ms 24104 KB Output is correct
27 Correct 1020 ms 24120 KB Output is correct
28 Correct 778 ms 24188 KB Output is correct
29 Correct 901 ms 43676 KB Output is correct
30 Correct 1000 ms 43740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5156 KB Output is correct
5 Correct 5 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5084 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 4 ms 5328 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 4 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 5 ms 5200 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 4 ms 5136 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5064 KB Output is correct
43 Correct 610 ms 5456 KB Output is correct
44 Correct 727 ms 5584 KB Output is correct
45 Correct 670 ms 5516 KB Output is correct
46 Correct 824 ms 5896 KB Output is correct
47 Correct 981 ms 5968 KB Output is correct
48 Correct 897 ms 5968 KB Output is correct
49 Correct 862 ms 5968 KB Output is correct
50 Correct 973 ms 5968 KB Output is correct
51 Correct 938 ms 5712 KB Output is correct
52 Correct 919 ms 5712 KB Output is correct
53 Correct 738 ms 6224 KB Output is correct
54 Correct 843 ms 5904 KB Output is correct
55 Correct 1020 ms 5840 KB Output is correct
56 Correct 910 ms 5712 KB Output is correct
57 Correct 792 ms 5712 KB Output is correct
58 Correct 685 ms 6864 KB Output is correct
59 Correct 757 ms 6964 KB Output is correct
60 Correct 864 ms 7052 KB Output is correct
61 Correct 902 ms 5772 KB Output is correct
62 Correct 756 ms 5584 KB Output is correct
63 Correct 784 ms 5584 KB Output is correct
64 Correct 849 ms 5712 KB Output is correct
65 Correct 476 ms 5328 KB Output is correct
66 Correct 640 ms 5712 KB Output is correct
67 Correct 841 ms 5772 KB Output is correct
68 Correct 614 ms 5772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5156 KB Output is correct
5 Correct 5 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 3 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5084 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 4 ms 5328 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 3 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 4 ms 5200 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 5 ms 5200 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5200 KB Output is correct
32 Correct 4 ms 5200 KB Output is correct
33 Correct 4 ms 5136 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 3 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 4 ms 5328 KB Output is correct
38 Correct 4 ms 5328 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5064 KB Output is correct
43 Correct 751 ms 11288 KB Output is correct
44 Correct 890 ms 17552 KB Output is correct
45 Correct 966 ms 17480 KB Output is correct
46 Correct 810 ms 17536 KB Output is correct
47 Correct 821 ms 11208 KB Output is correct
48 Correct 1041 ms 17536 KB Output is correct
49 Correct 836 ms 17564 KB Output is correct
50 Correct 742 ms 17488 KB Output is correct
51 Correct 457 ms 5328 KB Output is correct
52 Correct 910 ms 5712 KB Output is correct
53 Correct 771 ms 5712 KB Output is correct
54 Correct 879 ms 5712 KB Output is correct
55 Correct 1076 ms 23848 KB Output is correct
56 Correct 1141 ms 24104 KB Output is correct
57 Correct 1020 ms 24120 KB Output is correct
58 Correct 778 ms 24188 KB Output is correct
59 Correct 901 ms 43676 KB Output is correct
60 Correct 1000 ms 43740 KB Output is correct
61 Correct 610 ms 5456 KB Output is correct
62 Correct 727 ms 5584 KB Output is correct
63 Correct 670 ms 5516 KB Output is correct
64 Correct 824 ms 5896 KB Output is correct
65 Correct 981 ms 5968 KB Output is correct
66 Correct 897 ms 5968 KB Output is correct
67 Correct 862 ms 5968 KB Output is correct
68 Correct 973 ms 5968 KB Output is correct
69 Correct 938 ms 5712 KB Output is correct
70 Correct 919 ms 5712 KB Output is correct
71 Correct 738 ms 6224 KB Output is correct
72 Correct 843 ms 5904 KB Output is correct
73 Correct 1020 ms 5840 KB Output is correct
74 Correct 910 ms 5712 KB Output is correct
75 Correct 792 ms 5712 KB Output is correct
76 Correct 685 ms 6864 KB Output is correct
77 Correct 757 ms 6964 KB Output is correct
78 Correct 864 ms 7052 KB Output is correct
79 Correct 902 ms 5772 KB Output is correct
80 Correct 756 ms 5584 KB Output is correct
81 Correct 784 ms 5584 KB Output is correct
82 Correct 849 ms 5712 KB Output is correct
83 Correct 476 ms 5328 KB Output is correct
84 Correct 640 ms 5712 KB Output is correct
85 Correct 841 ms 5772 KB Output is correct
86 Correct 614 ms 5772 KB Output is correct
87 Correct 3 ms 4944 KB Output is correct
88 Correct 602 ms 21496 KB Output is correct
89 Correct 1016 ms 15944 KB Output is correct
90 Correct 767 ms 16456 KB Output is correct
91 Correct 793 ms 24232 KB Output is correct
92 Correct 901 ms 24220 KB Output is correct
93 Correct 857 ms 24196 KB Output is correct
94 Correct 1133 ms 24192 KB Output is correct
95 Correct 711 ms 24268 KB Output is correct
96 Correct 967 ms 19104 KB Output is correct
97 Correct 994 ms 19024 KB Output is correct
98 Correct 873 ms 30720 KB Output is correct
99 Correct 748 ms 24284 KB Output is correct
100 Correct 1051 ms 21376 KB Output is correct
101 Correct 946 ms 20488 KB Output is correct
102 Correct 776 ms 19236 KB Output is correct
103 Correct 1041 ms 43744 KB Output is correct
104 Correct 905 ms 44196 KB Output is correct
105 Correct 908 ms 44124 KB Output is correct
106 Correct 971 ms 18972 KB Output is correct
107 Correct 853 ms 16336 KB Output is correct
108 Correct 941 ms 17332 KB Output is correct
109 Correct 919 ms 19272 KB Output is correct