답안 #1030900

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030900 2024-07-22T11:39:28 Z 0npata 디지털 회로 (IOI22_circuit) C++17
100 / 100
774 ms 34260 KB
#include "circuit.h"
#include<bits/stdc++.h>

using namespace std;

#define vec vector
#define int long long
#define arr array

const int MX = 200'005;

vec<int32_t> a;
vec<int> tree[MX];
int tot[MX];
int n, m;
const int MXM = 100'005;
const int MOD = 1'000'002'022;
int contr[MXM];

struct SegLazy {
	bool flip = false;

	SegLazy merge(SegLazy other) {
		return {flip ^ other.flip};
	}
};

struct SegNode {
	arr<int, 2> sums = {0, 0};

	SegNode merge(SegNode other) {
		return {{sums[0]+other.sums[0], sums[1]+other.sums[1]}};
	}

	SegNode upd(SegLazy lazy) {
		if(lazy.flip) {
			return {{sums[1], sums[0]}};
		}
		else {
			return {{sums[0], sums[1]}};
		}
	}
};


struct SegTree {
	int n;
	vec<SegNode> data;
	vec<SegLazy> lazy;

	SegTree(int in) {
		n = 1;
		while(n < in) n *= 2;
		data = vec<SegNode>(n*2);
		lazy = vec<SegLazy>(n*2);
	}

	void pull(int i) {
		data[i] = data[i*2].merge(data[i*2+1]);
	}

	void set(int i, SegNode val) {
		i += n;
		data[i] = val;
		while(i > 1) {
			i /= 2;
			pull(i);
		}
	}

	void push(int i) {
		data[i] = data[i].upd(lazy[i]);
		if(i*2 >= n*2) {
			lazy[i] = {};
			return;
		}
		lazy[i*2] = lazy[i*2].merge(lazy[i]);
		lazy[i*2+1] = lazy[i*2+1].merge(lazy[i]);
		lazy[i] = {};
	}


	void upd(int l, int r, SegLazy val) {
		_upd(l, r, 1, 0, n, val);
	}

	void _upd(int l, int r, int ti, int tl, int tr, SegLazy val) {
		//cerr << tl << ' ' << tr << '\n';
		push(ti);
		if(l >= tr || r <= tl) return;
		if(l <= tl && r >= tr) {
			lazy[ti] = val;
			push(ti);
			return;
		}

		int tm = (tl+tr)/2;
		_upd(l, r, ti*2, tl, tm, val);
		_upd(l, r, ti*2+1, tm, tr, val);
		pull(ti);
	}
};

SegTree st(0);

void dfs1(int u) {
	if(u >= n) {
		tot[u] = 1;
		return;
	}
	tot[u] = tree[u].size();
	for(int v : tree[u]) {
		dfs1(v);
		tot[u] *= tot[v];
		tot[u] %= MOD;
	}
}

void dfs2(int u, int top) {
	if(u >= n) {
		contr[u-n] = top;
		return;
	}
	vec<int> pref_mul(tree[u].size()+1);
	vec<int> suf_mul(tree[u].size()+1);

	pref_mul[0] = 1;
	suf_mul[0] =1;

	for(int i = 0; i<tree[u].size(); i++) {
		pref_mul[i+1] = pref_mul[i]*tot[tree[u][i]];
		pref_mul[i+1] %= MOD;
		suf_mul[i+1] = suf_mul[i]*tot[tree[u][tree[u].size()-i-1]];
		suf_mul[i+1] %= MOD;
	}

	for(int i = 0; i<tree[u].size(); i++) {
		dfs2(tree[u][i], (((top*pref_mul[i])%MOD)*suf_mul[tree[u].size()-i-1]) % MOD);
	}
}


void init(int32_t N, int32_t M, std::vector<int32_t> P, std::vector<int32_t> A) {
	n = N;
	m = M;
	a = A;

	for(int i = 1; i<N+M; i++) {
		tree[P[i]].push_back(i);
	}

	dfs1(0);
	dfs2(0, 1);

	st = SegTree(m);
	for(int i = 0; i<m; i++) {
		//cerr << contr[i] << ' ';
		SegNode node = {{0, contr[i]}};
		st.set(i, node);
		st.upd(i, i+1, {A[i]});
	}
	//cerr << '\n';
	//cerr << st.data[1].sums[0] << '\n';
}

int32_t count_ways(int32_t L, int32_t R) {
	//cerr << "QUERY: " << L << ' ' << R << '\n';
	st.upd(L-n, R+1-n, {true});
	return st.data[1].sums[0] % MOD;
}

Compilation message

circuit.cpp: In member function 'SegLazy SegLazy::merge(SegLazy)':
circuit.cpp:24:16: warning: narrowing conversion of '(((int)((SegLazy*)this)->SegLazy::flip) ^ ((int)other.SegLazy::flip))' from 'int' to 'bool' [-Wnarrowing]
   24 |   return {flip ^ other.flip};
      |           ~~~~~^~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(long long int, long long int)':
circuit.cpp:130:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for(int i = 0; i<tree[u].size(); i++) {
      |                 ~^~~~~~~~~~~~~~~
circuit.cpp:137:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |  for(int i = 0; i<tree[u].size(); i++) {
      |                 ~^~~~~~~~~~~~~~~
circuit.cpp: In function 'void init(int32_t, int32_t, std::vector<int>, std::vector<int>)':
circuit.cpp:160:24: warning: narrowing conversion of 'A.std::vector<int>::operator[](((std::vector<int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'bool' [-Wnarrowing]
  160 |   st.upd(i, i+1, {A[i]});
      |                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 1 ms 7256 KB Output is correct
5 Correct 2 ms 7344 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 1 ms 7256 KB Output is correct
8 Correct 1 ms 7256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 2 ms 7256 KB Output is correct
5 Correct 2 ms 7256 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 2 ms 7256 KB Output is correct
8 Correct 2 ms 7256 KB Output is correct
9 Correct 2 ms 7256 KB Output is correct
10 Correct 2 ms 7512 KB Output is correct
11 Correct 2 ms 7512 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 1 ms 7256 KB Output is correct
5 Correct 2 ms 7344 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 1 ms 7256 KB Output is correct
8 Correct 1 ms 7256 KB Output is correct
9 Correct 1 ms 7256 KB Output is correct
10 Correct 1 ms 7256 KB Output is correct
11 Correct 1 ms 7256 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
13 Correct 2 ms 7256 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7256 KB Output is correct
17 Correct 2 ms 7256 KB Output is correct
18 Correct 2 ms 7512 KB Output is correct
19 Correct 2 ms 7512 KB Output is correct
20 Correct 2 ms 7256 KB Output is correct
21 Correct 2 ms 7256 KB Output is correct
22 Correct 1 ms 7256 KB Output is correct
23 Correct 1 ms 7256 KB Output is correct
24 Correct 2 ms 7256 KB Output is correct
25 Correct 2 ms 7296 KB Output is correct
26 Correct 2 ms 7256 KB Output is correct
27 Correct 2 ms 7256 KB Output is correct
28 Correct 3 ms 7256 KB Output is correct
29 Correct 2 ms 7256 KB Output is correct
30 Correct 2 ms 7256 KB Output is correct
31 Correct 1 ms 7512 KB Output is correct
32 Correct 2 ms 7256 KB Output is correct
33 Correct 2 ms 7256 KB Output is correct
34 Correct 2 ms 7256 KB Output is correct
35 Correct 2 ms 7256 KB Output is correct
36 Correct 2 ms 7512 KB Output is correct
37 Correct 2 ms 7512 KB Output is correct
38 Correct 2 ms 7512 KB Output is correct
39 Correct 1 ms 7400 KB Output is correct
40 Correct 2 ms 7256 KB Output is correct
41 Correct 2 ms 7256 KB Output is correct
42 Correct 2 ms 7256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 10328 KB Output is correct
2 Correct 715 ms 13400 KB Output is correct
3 Correct 711 ms 13456 KB Output is correct
4 Correct 658 ms 13400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 10328 KB Output is correct
2 Correct 715 ms 13400 KB Output is correct
3 Correct 711 ms 13456 KB Output is correct
4 Correct 658 ms 13400 KB Output is correct
5 Correct 576 ms 10328 KB Output is correct
6 Correct 767 ms 13400 KB Output is correct
7 Correct 737 ms 13388 KB Output is correct
8 Correct 654 ms 13400 KB Output is correct
9 Correct 263 ms 7512 KB Output is correct
10 Correct 604 ms 7512 KB Output is correct
11 Correct 654 ms 7512 KB Output is correct
12 Correct 599 ms 7512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 2 ms 7256 KB Output is correct
5 Correct 2 ms 7256 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 2 ms 7256 KB Output is correct
8 Correct 2 ms 7256 KB Output is correct
9 Correct 2 ms 7256 KB Output is correct
10 Correct 2 ms 7512 KB Output is correct
11 Correct 2 ms 7512 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
13 Correct 436 ms 10328 KB Output is correct
14 Correct 715 ms 13400 KB Output is correct
15 Correct 711 ms 13456 KB Output is correct
16 Correct 658 ms 13400 KB Output is correct
17 Correct 576 ms 10328 KB Output is correct
18 Correct 767 ms 13400 KB Output is correct
19 Correct 737 ms 13388 KB Output is correct
20 Correct 654 ms 13400 KB Output is correct
21 Correct 263 ms 7512 KB Output is correct
22 Correct 604 ms 7512 KB Output is correct
23 Correct 654 ms 7512 KB Output is correct
24 Correct 599 ms 7512 KB Output is correct
25 Correct 627 ms 17488 KB Output is correct
26 Correct 627 ms 17692 KB Output is correct
27 Correct 678 ms 17720 KB Output is correct
28 Correct 559 ms 17720 KB Output is correct
29 Correct 673 ms 32080 KB Output is correct
30 Correct 739 ms 32080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 1 ms 7256 KB Output is correct
5 Correct 2 ms 7344 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 1 ms 7256 KB Output is correct
8 Correct 1 ms 7256 KB Output is correct
9 Correct 1 ms 7256 KB Output is correct
10 Correct 1 ms 7256 KB Output is correct
11 Correct 1 ms 7256 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
13 Correct 2 ms 7256 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7256 KB Output is correct
17 Correct 2 ms 7256 KB Output is correct
18 Correct 2 ms 7512 KB Output is correct
19 Correct 2 ms 7512 KB Output is correct
20 Correct 2 ms 7256 KB Output is correct
21 Correct 2 ms 7256 KB Output is correct
22 Correct 1 ms 7256 KB Output is correct
23 Correct 1 ms 7256 KB Output is correct
24 Correct 2 ms 7256 KB Output is correct
25 Correct 2 ms 7296 KB Output is correct
26 Correct 2 ms 7256 KB Output is correct
27 Correct 2 ms 7256 KB Output is correct
28 Correct 3 ms 7256 KB Output is correct
29 Correct 2 ms 7256 KB Output is correct
30 Correct 2 ms 7256 KB Output is correct
31 Correct 1 ms 7512 KB Output is correct
32 Correct 2 ms 7256 KB Output is correct
33 Correct 2 ms 7256 KB Output is correct
34 Correct 2 ms 7256 KB Output is correct
35 Correct 2 ms 7256 KB Output is correct
36 Correct 2 ms 7512 KB Output is correct
37 Correct 2 ms 7512 KB Output is correct
38 Correct 2 ms 7512 KB Output is correct
39 Correct 1 ms 7400 KB Output is correct
40 Correct 2 ms 7256 KB Output is correct
41 Correct 2 ms 7256 KB Output is correct
42 Correct 2 ms 7256 KB Output is correct
43 Correct 439 ms 7644 KB Output is correct
44 Correct 622 ms 7512 KB Output is correct
45 Correct 655 ms 7512 KB Output is correct
46 Correct 602 ms 7768 KB Output is correct
47 Correct 639 ms 7768 KB Output is correct
48 Correct 611 ms 7768 KB Output is correct
49 Correct 699 ms 7768 KB Output is correct
50 Correct 630 ms 7768 KB Output is correct
51 Correct 615 ms 7768 KB Output is correct
52 Correct 683 ms 7768 KB Output is correct
53 Correct 592 ms 8280 KB Output is correct
54 Correct 663 ms 7768 KB Output is correct
55 Correct 663 ms 7768 KB Output is correct
56 Correct 647 ms 7764 KB Output is correct
57 Correct 642 ms 7768 KB Output is correct
58 Correct 687 ms 8536 KB Output is correct
59 Correct 624 ms 8792 KB Output is correct
60 Correct 698 ms 8792 KB Output is correct
61 Correct 624 ms 7768 KB Output is correct
62 Correct 635 ms 7512 KB Output is correct
63 Correct 590 ms 7512 KB Output is correct
64 Correct 681 ms 7768 KB Output is correct
65 Correct 296 ms 7256 KB Output is correct
66 Correct 626 ms 7512 KB Output is correct
67 Correct 607 ms 7512 KB Output is correct
68 Correct 650 ms 7512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7256 KB Output is correct
3 Correct 1 ms 7256 KB Output is correct
4 Correct 1 ms 7256 KB Output is correct
5 Correct 2 ms 7344 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 1 ms 7256 KB Output is correct
8 Correct 1 ms 7256 KB Output is correct
9 Correct 1 ms 7256 KB Output is correct
10 Correct 1 ms 7256 KB Output is correct
11 Correct 1 ms 7256 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
13 Correct 2 ms 7256 KB Output is correct
14 Correct 2 ms 7256 KB Output is correct
15 Correct 2 ms 7256 KB Output is correct
16 Correct 2 ms 7256 KB Output is correct
17 Correct 2 ms 7256 KB Output is correct
18 Correct 2 ms 7512 KB Output is correct
19 Correct 2 ms 7512 KB Output is correct
20 Correct 2 ms 7256 KB Output is correct
21 Correct 2 ms 7256 KB Output is correct
22 Correct 1 ms 7256 KB Output is correct
23 Correct 1 ms 7256 KB Output is correct
24 Correct 2 ms 7256 KB Output is correct
25 Correct 2 ms 7296 KB Output is correct
26 Correct 2 ms 7256 KB Output is correct
27 Correct 2 ms 7256 KB Output is correct
28 Correct 3 ms 7256 KB Output is correct
29 Correct 2 ms 7256 KB Output is correct
30 Correct 2 ms 7256 KB Output is correct
31 Correct 1 ms 7512 KB Output is correct
32 Correct 2 ms 7256 KB Output is correct
33 Correct 2 ms 7256 KB Output is correct
34 Correct 2 ms 7256 KB Output is correct
35 Correct 2 ms 7256 KB Output is correct
36 Correct 2 ms 7512 KB Output is correct
37 Correct 2 ms 7512 KB Output is correct
38 Correct 2 ms 7512 KB Output is correct
39 Correct 1 ms 7400 KB Output is correct
40 Correct 2 ms 7256 KB Output is correct
41 Correct 2 ms 7256 KB Output is correct
42 Correct 2 ms 7256 KB Output is correct
43 Correct 436 ms 10328 KB Output is correct
44 Correct 715 ms 13400 KB Output is correct
45 Correct 711 ms 13456 KB Output is correct
46 Correct 658 ms 13400 KB Output is correct
47 Correct 576 ms 10328 KB Output is correct
48 Correct 767 ms 13400 KB Output is correct
49 Correct 737 ms 13388 KB Output is correct
50 Correct 654 ms 13400 KB Output is correct
51 Correct 263 ms 7512 KB Output is correct
52 Correct 604 ms 7512 KB Output is correct
53 Correct 654 ms 7512 KB Output is correct
54 Correct 599 ms 7512 KB Output is correct
55 Correct 627 ms 17488 KB Output is correct
56 Correct 627 ms 17692 KB Output is correct
57 Correct 678 ms 17720 KB Output is correct
58 Correct 559 ms 17720 KB Output is correct
59 Correct 673 ms 32080 KB Output is correct
60 Correct 739 ms 32080 KB Output is correct
61 Correct 439 ms 7644 KB Output is correct
62 Correct 622 ms 7512 KB Output is correct
63 Correct 655 ms 7512 KB Output is correct
64 Correct 602 ms 7768 KB Output is correct
65 Correct 639 ms 7768 KB Output is correct
66 Correct 611 ms 7768 KB Output is correct
67 Correct 699 ms 7768 KB Output is correct
68 Correct 630 ms 7768 KB Output is correct
69 Correct 615 ms 7768 KB Output is correct
70 Correct 683 ms 7768 KB Output is correct
71 Correct 592 ms 8280 KB Output is correct
72 Correct 663 ms 7768 KB Output is correct
73 Correct 663 ms 7768 KB Output is correct
74 Correct 647 ms 7764 KB Output is correct
75 Correct 642 ms 7768 KB Output is correct
76 Correct 687 ms 8536 KB Output is correct
77 Correct 624 ms 8792 KB Output is correct
78 Correct 698 ms 8792 KB Output is correct
79 Correct 624 ms 7768 KB Output is correct
80 Correct 635 ms 7512 KB Output is correct
81 Correct 590 ms 7512 KB Output is correct
82 Correct 681 ms 7768 KB Output is correct
83 Correct 296 ms 7256 KB Output is correct
84 Correct 626 ms 7512 KB Output is correct
85 Correct 607 ms 7512 KB Output is correct
86 Correct 650 ms 7512 KB Output is correct
87 Correct 1 ms 7256 KB Output is correct
88 Correct 418 ms 17528 KB Output is correct
89 Correct 726 ms 13656 KB Output is correct
90 Correct 719 ms 13656 KB Output is correct
91 Correct 688 ms 18264 KB Output is correct
92 Correct 754 ms 18208 KB Output is correct
93 Correct 774 ms 18344 KB Output is correct
94 Correct 730 ms 18256 KB Output is correct
95 Correct 726 ms 18256 KB Output is correct
96 Correct 663 ms 14508 KB Output is correct
97 Correct 707 ms 14588 KB Output is correct
98 Correct 604 ms 29780 KB Output is correct
99 Correct 744 ms 17488 KB Output is correct
100 Correct 772 ms 16752 KB Output is correct
101 Correct 733 ms 15956 KB Output is correct
102 Correct 622 ms 14680 KB Output is correct
103 Correct 696 ms 32080 KB Output is correct
104 Correct 708 ms 34252 KB Output is correct
105 Correct 707 ms 34260 KB Output is correct
106 Correct 609 ms 17496 KB Output is correct
107 Correct 673 ms 14932 KB Output is correct
108 Correct 620 ms 15212 KB Output is correct
109 Correct 637 ms 14936 KB Output is correct