답안 #925520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925520 2024-02-11T22:14:07 Z beaboss 디지털 회로 (IOI22_circuit) C++17
100 / 100
749 ms 47196 KB
// Source: https://oj.uz/problem/view/IOI22_circuit
// 

#include "bits/stdc++.h"

using namespace std;

#define s second
#define f first
#define pb push_back

typedef long long ll;

typedef pair<ll, ll> pii;
typedef vector<pii> vpii;

typedef vector<ll> vi;

#define FOR(i, a, b) for (ll i = (a); i<b; i++)

bool ckmin(ll& a, ll b){ return b < a ? a = b, true : false; }

bool ckmax(ll& a, ll b){ return b > a ? a = b, true : false; }

#define lc (i << 1)
#define rc (i << 1) + 1

const ll N = 2e5 + 10;

const ll MOD = 1e9 + 2022;

ll n, m;
ll p[N], contrib[N], sub[N];
vi adj[N];

void calc_sub(ll cur) {
	sub[cur] = 1;
	for (auto val: adj[cur]) {
		calc_sub(val);
		sub[cur] = sub[cur] * sub[val] % MOD;
	}
	if (adj[cur].size()) sub[cur] = sub[cur] * adj[cur].size() % MOD;
	// cout <<  ' ' << cur << sub[cur] << adj[cur].size() << endl;
}

void dfs(ll cur, ll prod = 1) {
	// cout << cur << prod << endl;
	contrib[cur]=prod;

	vi pref(adj[cur].size()), suff(adj[cur].size());

	FOR(i, 0, adj[cur].size()) {
		if (i) pref[i] = pref[i-1];
		else pref[i]=1;
		pref[i] = pref[i] * sub[adj[cur][i]] % MOD;
	}

	for (ll i = adj[cur].size() - 1; i >= 0; i--) {
		if (i != adj[cur].size() - 1) suff[i] = suff[i + 1];
		else suff[i]=1;
		suff[i] = suff[i] * sub[adj[cur][i]] % MOD;
	}

	FOR(i, 0, adj[cur].size()) {
		ll here = prod;
		if (i) here = here * pref[i-1] % MOD;
		if (i != adj[cur].size() - 1) here = here * suff[i+1] % MOD;
		dfs(adj[cur][i], here);
	}

}

struct node {
	ll val;
	ll sum;
	ll flip;
} st[4 * N];

void up(ll i) {
	st[i].val = (st[rc].val + st[lc].val) % MOD;
	st[i].sum = (st[rc].sum + st[lc].sum) % MOD;
}

void down(ll i) {
	if (!st[i].flip) return;

	st[rc].val = (st[rc].sum - st[rc].val + MOD) % MOD;
	st[lc].val = (st[lc].sum - st[lc].val + MOD) % MOD;

	st[rc].flip = !st[rc].flip;
	st[lc].flip = !st[lc].flip;
	st[i].flip = 0;


}

void adjust(ll ind, ll val, ll on, ll i = 1, ll l = 0, ll r = N) { // should happen before range updates
	if (r <= l) return;
	if (r - l == 1 && l == ind) {
		// cout << val << endl;
		// assert(on <= 1);
		st[i].flip = 0;
		st[i].sum = val;
		st[i].val = st[i].sum * on;
		// cout << l << r << ' ' << st[i].val << endl;
		return;
	}
	// if (r - l == 1) return;
	// cout << l << r << endl;
	// cout << val << endl;

	ll m = (l + r) / 2;
	if (m > ind) adjust(ind, val, on, lc, l, m);
	else adjust(ind, val, on, rc, m, r);

	up(i);

	// cout << l << r << ' ' << st[i].val << ' ' << st[rc].val << ' ' << st[lc].val << endl;
}

void update(ll ul, ll ur, ll i = 1, ll l = 0, ll r = N) {
	if (r <= l) return;

	if (ul <= l && r <= ur) { // fully contained
		st[i].val = (st[i].sum - st[i].val + MOD) % MOD;
		st[i].flip = !st[i].flip;
		// cout << l << ' ' << r << ' ' << st[i].val << endl;
		return;
	}

	down(i);

	ll m = (l + r) / 2;
	if (m > ul) update(ul, ur, lc, l, m);
	if (m < ur) update(ul, ur, rc, m, r);

	up(i);
}

void init(int n_, int m_, vector<int> p_, vector<int> a) {
	n = n_;
	m = m_;
	FOR(i, 0, n + m) {

		p[i] = p_[i];
		if (i) adj[p[i]].pb(i);
	}
	calc_sub(0);
	dfs(0);

	FOR(i, n, n + m) {
		// cout << i << endl;
		adjust(i - n, contrib[i], a[i - n]);
	}
}

ll count_ways(int l, int r) {
	update(l - n, r - n+1);
	return st[1].val;
}


// int main() {
// 	// ll p[7] =  {-1, 0, 1, 2, 1, 1, 0};
// 	// ll a[4] = {1, 0, 1, 0};
// 	// init(3, 4, p, a);

// 	// // cout << st[1].val << endl;
// 	// cout << count_ways(3, 4) << endl;
// 	// cout << count_ways(4, 5) << endl;
// 	// cout << count_ways(3, 6) << endl;
// 	// FOR(i, n, n + m) cout << contrib[i] << endl;
// }









Compilation message

circuit.cpp: In function 'void dfs(ll, ll)':
circuit.cpp:19:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 | #define FOR(i, a, b) for (ll i = (a); i<b; i++)
......
   52 |  FOR(i, 0, adj[cur].size()) {
      |      ~~~~~~~~~~~~~~~~~~~~~              
circuit.cpp:52:2: note: in expansion of macro 'FOR'
   52 |  FOR(i, 0, adj[cur].size()) {
      |  ^~~
circuit.cpp:59:9: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   if (i != adj[cur].size() - 1) suff[i] = suff[i + 1];
      |       ~~^~~~~~~~~~~~~~~~~~~~~~
circuit.cpp:19:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 | #define FOR(i, a, b) for (ll i = (a); i<b; i++)
......
   64 |  FOR(i, 0, adj[cur].size()) {
      |      ~~~~~~~~~~~~~~~~~~~~~              
circuit.cpp:64:2: note: in expansion of macro 'FOR'
   64 |  FOR(i, 0, adj[cur].size()) {
      |  ^~~
circuit.cpp:67:9: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |   if (i != adj[cur].size() - 1) here = here * suff[i+1] % MOD;
      |       ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 12628 KB Output is correct
3 Correct 4 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 2 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14824 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 12628 KB Output is correct
3 Correct 4 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 2 ms 14680 KB Output is correct
12 Correct 3 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 3 ms 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14680 KB Output is correct
18 Correct 3 ms 14824 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 2 ms 14680 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 2 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 3 ms 14680 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 3 ms 14680 KB Output is correct
30 Correct 2 ms 14680 KB Output is correct
31 Correct 2 ms 12888 KB Output is correct
32 Correct 3 ms 14680 KB Output is correct
33 Correct 3 ms 14680 KB Output is correct
34 Correct 3 ms 14680 KB Output is correct
35 Correct 3 ms 14680 KB Output is correct
36 Correct 3 ms 14936 KB Output is correct
37 Correct 3 ms 14936 KB Output is correct
38 Correct 3 ms 14936 KB Output is correct
39 Correct 2 ms 14680 KB Output is correct
40 Correct 3 ms 14680 KB Output is correct
41 Correct 3 ms 14680 KB Output is correct
42 Correct 3 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 18996 KB Output is correct
2 Correct 631 ms 21264 KB Output is correct
3 Correct 650 ms 21200 KB Output is correct
4 Correct 597 ms 21204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 18996 KB Output is correct
2 Correct 631 ms 21264 KB Output is correct
3 Correct 650 ms 21200 KB Output is correct
4 Correct 597 ms 21204 KB Output is correct
5 Correct 555 ms 18776 KB Output is correct
6 Correct 700 ms 21308 KB Output is correct
7 Correct 642 ms 21200 KB Output is correct
8 Correct 603 ms 21188 KB Output is correct
9 Correct 292 ms 14936 KB Output is correct
10 Correct 623 ms 14936 KB Output is correct
11 Correct 620 ms 14936 KB Output is correct
12 Correct 625 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 2 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14680 KB Output is correct
8 Correct 3 ms 14680 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14824 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 436 ms 18996 KB Output is correct
14 Correct 631 ms 21264 KB Output is correct
15 Correct 650 ms 21200 KB Output is correct
16 Correct 597 ms 21204 KB Output is correct
17 Correct 555 ms 18776 KB Output is correct
18 Correct 700 ms 21308 KB Output is correct
19 Correct 642 ms 21200 KB Output is correct
20 Correct 603 ms 21188 KB Output is correct
21 Correct 292 ms 14936 KB Output is correct
22 Correct 623 ms 14936 KB Output is correct
23 Correct 620 ms 14936 KB Output is correct
24 Correct 625 ms 14936 KB Output is correct
25 Correct 688 ms 25568 KB Output is correct
26 Correct 677 ms 25708 KB Output is correct
27 Correct 698 ms 25688 KB Output is correct
28 Correct 516 ms 25684 KB Output is correct
29 Correct 701 ms 46160 KB Output is correct
30 Correct 631 ms 45984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 12628 KB Output is correct
3 Correct 4 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 2 ms 14680 KB Output is correct
12 Correct 3 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 3 ms 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14680 KB Output is correct
18 Correct 3 ms 14824 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 2 ms 14680 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 2 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 3 ms 14680 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 3 ms 14680 KB Output is correct
30 Correct 2 ms 14680 KB Output is correct
31 Correct 2 ms 12888 KB Output is correct
32 Correct 3 ms 14680 KB Output is correct
33 Correct 3 ms 14680 KB Output is correct
34 Correct 3 ms 14680 KB Output is correct
35 Correct 3 ms 14680 KB Output is correct
36 Correct 3 ms 14936 KB Output is correct
37 Correct 3 ms 14936 KB Output is correct
38 Correct 3 ms 14936 KB Output is correct
39 Correct 2 ms 14680 KB Output is correct
40 Correct 3 ms 14680 KB Output is correct
41 Correct 3 ms 14680 KB Output is correct
42 Correct 3 ms 14680 KB Output is correct
43 Correct 385 ms 14936 KB Output is correct
44 Correct 580 ms 14936 KB Output is correct
45 Correct 595 ms 14936 KB Output is correct
46 Correct 584 ms 14936 KB Output is correct
47 Correct 600 ms 15028 KB Output is correct
48 Correct 585 ms 14936 KB Output is correct
49 Correct 524 ms 14936 KB Output is correct
50 Correct 596 ms 14936 KB Output is correct
51 Correct 534 ms 15024 KB Output is correct
52 Correct 549 ms 15184 KB Output is correct
53 Correct 582 ms 13908 KB Output is correct
54 Correct 604 ms 14936 KB Output is correct
55 Correct 575 ms 14936 KB Output is correct
56 Correct 540 ms 14936 KB Output is correct
57 Correct 604 ms 14936 KB Output is correct
58 Correct 622 ms 15960 KB Output is correct
59 Correct 612 ms 16216 KB Output is correct
60 Correct 515 ms 16216 KB Output is correct
61 Correct 561 ms 15192 KB Output is correct
62 Correct 590 ms 14936 KB Output is correct
63 Correct 615 ms 14936 KB Output is correct
64 Correct 581 ms 14936 KB Output is correct
65 Correct 264 ms 14936 KB Output is correct
66 Correct 599 ms 14936 KB Output is correct
67 Correct 580 ms 14936 KB Output is correct
68 Correct 549 ms 14948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 2 ms 12628 KB Output is correct
3 Correct 4 ms 14680 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 2 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 2 ms 14680 KB Output is correct
8 Correct 2 ms 14680 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 3 ms 14680 KB Output is correct
11 Correct 2 ms 14680 KB Output is correct
12 Correct 3 ms 14680 KB Output is correct
13 Correct 2 ms 14680 KB Output is correct
14 Correct 3 ms 14680 KB Output is correct
15 Correct 3 ms 14680 KB Output is correct
16 Correct 3 ms 14680 KB Output is correct
17 Correct 3 ms 14680 KB Output is correct
18 Correct 3 ms 14824 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 2 ms 14680 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 2 ms 14680 KB Output is correct
24 Correct 3 ms 14680 KB Output is correct
25 Correct 3 ms 14680 KB Output is correct
26 Correct 3 ms 14680 KB Output is correct
27 Correct 3 ms 14680 KB Output is correct
28 Correct 3 ms 14680 KB Output is correct
29 Correct 3 ms 14680 KB Output is correct
30 Correct 2 ms 14680 KB Output is correct
31 Correct 2 ms 12888 KB Output is correct
32 Correct 3 ms 14680 KB Output is correct
33 Correct 3 ms 14680 KB Output is correct
34 Correct 3 ms 14680 KB Output is correct
35 Correct 3 ms 14680 KB Output is correct
36 Correct 3 ms 14936 KB Output is correct
37 Correct 3 ms 14936 KB Output is correct
38 Correct 3 ms 14936 KB Output is correct
39 Correct 2 ms 14680 KB Output is correct
40 Correct 3 ms 14680 KB Output is correct
41 Correct 3 ms 14680 KB Output is correct
42 Correct 3 ms 14680 KB Output is correct
43 Correct 436 ms 18996 KB Output is correct
44 Correct 631 ms 21264 KB Output is correct
45 Correct 650 ms 21200 KB Output is correct
46 Correct 597 ms 21204 KB Output is correct
47 Correct 555 ms 18776 KB Output is correct
48 Correct 700 ms 21308 KB Output is correct
49 Correct 642 ms 21200 KB Output is correct
50 Correct 603 ms 21188 KB Output is correct
51 Correct 292 ms 14936 KB Output is correct
52 Correct 623 ms 14936 KB Output is correct
53 Correct 620 ms 14936 KB Output is correct
54 Correct 625 ms 14936 KB Output is correct
55 Correct 688 ms 25568 KB Output is correct
56 Correct 677 ms 25708 KB Output is correct
57 Correct 698 ms 25688 KB Output is correct
58 Correct 516 ms 25684 KB Output is correct
59 Correct 701 ms 46160 KB Output is correct
60 Correct 631 ms 45984 KB Output is correct
61 Correct 385 ms 14936 KB Output is correct
62 Correct 580 ms 14936 KB Output is correct
63 Correct 595 ms 14936 KB Output is correct
64 Correct 584 ms 14936 KB Output is correct
65 Correct 600 ms 15028 KB Output is correct
66 Correct 585 ms 14936 KB Output is correct
67 Correct 524 ms 14936 KB Output is correct
68 Correct 596 ms 14936 KB Output is correct
69 Correct 534 ms 15024 KB Output is correct
70 Correct 549 ms 15184 KB Output is correct
71 Correct 582 ms 13908 KB Output is correct
72 Correct 604 ms 14936 KB Output is correct
73 Correct 575 ms 14936 KB Output is correct
74 Correct 540 ms 14936 KB Output is correct
75 Correct 604 ms 14936 KB Output is correct
76 Correct 622 ms 15960 KB Output is correct
77 Correct 612 ms 16216 KB Output is correct
78 Correct 515 ms 16216 KB Output is correct
79 Correct 561 ms 15192 KB Output is correct
80 Correct 590 ms 14936 KB Output is correct
81 Correct 615 ms 14936 KB Output is correct
82 Correct 581 ms 14936 KB Output is correct
83 Correct 264 ms 14936 KB Output is correct
84 Correct 599 ms 14936 KB Output is correct
85 Correct 580 ms 14936 KB Output is correct
86 Correct 549 ms 14948 KB Output is correct
87 Correct 2 ms 14680 KB Output is correct
88 Correct 420 ms 25592 KB Output is correct
89 Correct 628 ms 21368 KB Output is correct
90 Correct 698 ms 21328 KB Output is correct
91 Correct 644 ms 26360 KB Output is correct
92 Correct 726 ms 26200 KB Output is correct
93 Correct 671 ms 26212 KB Output is correct
94 Correct 667 ms 26192 KB Output is correct
95 Correct 659 ms 26460 KB Output is correct
96 Correct 580 ms 21924 KB Output is correct
97 Correct 674 ms 22048 KB Output is correct
98 Correct 582 ms 37460 KB Output is correct
99 Correct 705 ms 25684 KB Output is correct
100 Correct 749 ms 24632 KB Output is correct
101 Correct 663 ms 23612 KB Output is correct
102 Correct 636 ms 22060 KB Output is correct
103 Correct 668 ms 46160 KB Output is correct
104 Correct 680 ms 46764 KB Output is correct
105 Correct 600 ms 47196 KB Output is correct
106 Correct 618 ms 25664 KB Output is correct
107 Correct 635 ms 20324 KB Output is correct
108 Correct 647 ms 22692 KB Output is correct
109 Correct 698 ms 22232 KB Output is correct