Submission #831032

# Submission time Handle Problem Language Result Execution time Memory
831032 2023-08-19T15:24:13 Z caganyanmaz Digital Circuit (IOI22_circuit) C++17
100 / 100
978 ms 37688 KB
#include <bits/stdc++.h>
#define mp(x...) array<int, 2>({x})
#define pb push_back
using namespace std;
#include "circuit.h"

constexpr static int MXN = 2e5;
constexpr static int MOD = 1000002022;
constexpr static int MXST = MXN<<2;
int mul() { return 1; }
template<typename... V>
int mul(int64_t a, V... v) { return (a* mul(v...)) % MOD; }
int add() { return 0; }
template<typename... V>
int add(int a, V... v) { return (a+add(v...)) % MOD; }


//#define DEBUGGING
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif

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


vector<int> g[MXN];
int factor[MXN];
int pcount[MXN];
void calculate(int i);
static inline int mul(int64_t a, int64_t b) { return (a*b) % MOD; }
static inline int add(int a, int b) { return (a+b) % MOD; }

struct SegTree
{
	int n;
	array<int, 2> data[MXST];
	bitset<MXST> lazy;
	array<int, 2> merge(array<int, 2> a, array<int, 2> b)
	{
		return mp(add(a[0], b[0]), add(a[1], b[1]));
	}
	void push(int l, int r, int index)
	{
		if (lazy[index] == 1)
			swap(data[index][0], data[index][1]);
		if (r - l > 1)
		{
			lazy[index<<1] = lazy[index<<1] ^  lazy[index];
			lazy[(index<<1)|1] = lazy[(index<<1)|1] ^ lazy[index];
		}
		lazy[index] = 0;
	}
	void build(int l, int r, int index, vector<int>& a, vector<int>& f)
	{
		if (r - l == 1)
		{
			data[index][0] = mul((a[l]^1), f[l]);
			data[index][1] = mul(a[l], f[l]);
			debug(l, data[index]);
			return;
		}
		int m = l+r>>1;
		build(l, m, index<<1, a, f);
		build(m, r, (index<<1)|1, a, f);
		data[index] = merge(data[index<<1], data[(index<<1)|1]);
		debug(l, r, data[index]);
	}
	void toggle(int l, int r, int index, int ss, int ee)
	{
		push(l, r, index);
		if (ss >= r || l >= ee)
			return;
		if (ee >= r && l >= ss)
		{
			lazy[index] = lazy[index] ^ 1;
			push(l, r, index);
			debug(l, r, data[index]);
			return;
		}
		int m = l+r>>1;
		toggle(l, m, index<<1, ss, ee);
		toggle(m, r, (index<<1)|1, ss, ee);
		data[index] = merge(data[index<<1], data[(index<<1)|1]);
		debug(l, r, data[index]);
	}
	int query(int l, int r, int index, int ss, int ee)
	{
		push(l, r, index);
		if (ss >= r || l >= ee)
			return 0;
		if (ee >= r && l >= ss)
			return data[index][1];
		int m = l+r>>1;
		int lres = query(l, m, index<<1, ss, ee);
		int rres = query(m, r, (index<<1)|1, ss, ee);
		return add(lres, rres);
	}
};
SegTree st;

void dfs1(int node)
{
	if (node >= n)
		pcount[node] = 1;
	else
		pcount[node] = g[node].size();
	for (int c : g[node])
	{
		dfs1(c);
		pcount[node] = mul(pcount[node], pcount[c]);
	}
}

void dfs2(int node)
{
	vector<int> pf(g[node].size()+1), sf(g[node].size()+1);
	pf[0] = 1, sf[g[node].size()] = 1;
	for (int i = 1; i <= g[node].size(); i++)
		pf[i] = mul(pf[i-1], pcount[g[node][i-1]]);
	for (int i = g[node].size()-1; i >= 0; i--)
		sf[i] = mul(sf[i+1], pcount[g[node][i]]);
	for (int i = 0; i < g[node].size(); i++)
	{
		int c = g[node][i];
		factor[c] = mul(factor[node],pf[i], sf[i+1]);
		dfs2(c);
	}
}

void init(int N, int M, vector<int> P, vector<int> A) 
{
	n = N;
	m = M;
	p = P;
	a = A;
	for (int i = 1; i < n+m; i++)
		g[p[i]].pb(i);
	dfs1(0);
	factor[0] = 1;
	dfs2(0);
	vector<int> as;
	for (int i = n; i < n+m; i++)
		as.pb(factor[i]);
	st.build(0, m, 1, a, as);
}

int count_ways(int l, int r) 
{
	debug(l-n, r-n+1);
	st.toggle(0, m, 1, l-n, r-n+1);
	return st.query(0, m, 1, 0, m);
}

Compilation message

circuit.cpp: In member function 'void SegTree::build(int, int, int, std::vector<int>&, std::vector<int>&)':
circuit.cpp:66:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |   int m = l+r>>1;
      |           ~^~
circuit.cpp: In member function 'void SegTree::toggle(int, int, int, int, int)':
circuit.cpp:84:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |   int m = l+r>>1;
      |           ~^~
circuit.cpp: In member function 'int SegTree::query(int, int, int, int, int)':
circuit.cpp:97:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   97 |   int m = l+r>>1;
      |           ~^~
circuit.cpp: In function 'void dfs2(int)':
circuit.cpp:122:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |  for (int i = 1; i <= g[node].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
circuit.cpp:126:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |  for (int i = 0; i < g[node].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5024 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5072 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5200 KB Output is correct
7 Correct 3 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 2 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 3 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5024 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 5072 KB Output is correct
10 Correct 2 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 2 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 2 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 3 ms 5200 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 3 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 3 ms 5188 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5092 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 8564 KB Output is correct
2 Correct 728 ms 11852 KB Output is correct
3 Correct 743 ms 11896 KB Output is correct
4 Correct 640 ms 11900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 8564 KB Output is correct
2 Correct 728 ms 11852 KB Output is correct
3 Correct 743 ms 11896 KB Output is correct
4 Correct 640 ms 11900 KB Output is correct
5 Correct 524 ms 8548 KB Output is correct
6 Correct 846 ms 11904 KB Output is correct
7 Correct 863 ms 11912 KB Output is correct
8 Correct 739 ms 11892 KB Output is correct
9 Correct 369 ms 5328 KB Output is correct
10 Correct 842 ms 5456 KB Output is correct
11 Correct 665 ms 5456 KB Output is correct
12 Correct 729 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5072 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5200 KB Output is correct
7 Correct 3 ms 5200 KB Output is correct
8 Correct 3 ms 5200 KB Output is correct
9 Correct 2 ms 5200 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5328 KB Output is correct
12 Correct 3 ms 5200 KB Output is correct
13 Correct 560 ms 8564 KB Output is correct
14 Correct 728 ms 11852 KB Output is correct
15 Correct 743 ms 11896 KB Output is correct
16 Correct 640 ms 11900 KB Output is correct
17 Correct 524 ms 8548 KB Output is correct
18 Correct 846 ms 11904 KB Output is correct
19 Correct 863 ms 11912 KB Output is correct
20 Correct 739 ms 11892 KB Output is correct
21 Correct 369 ms 5328 KB Output is correct
22 Correct 842 ms 5456 KB Output is correct
23 Correct 665 ms 5456 KB Output is correct
24 Correct 729 ms 5456 KB Output is correct
25 Correct 665 ms 15732 KB Output is correct
26 Correct 764 ms 15884 KB Output is correct
27 Correct 893 ms 15876 KB Output is correct
28 Correct 846 ms 15876 KB Output is correct
29 Correct 880 ms 37292 KB Output is correct
30 Correct 797 ms 37384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5024 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 5072 KB Output is correct
10 Correct 2 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 2 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 2 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 3 ms 5200 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 3 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 3 ms 5188 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5092 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
43 Correct 409 ms 5412 KB Output is correct
44 Correct 695 ms 5456 KB Output is correct
45 Correct 640 ms 5456 KB Output is correct
46 Correct 677 ms 5712 KB Output is correct
47 Correct 683 ms 5712 KB Output is correct
48 Correct 730 ms 5688 KB Output is correct
49 Correct 843 ms 5712 KB Output is correct
50 Correct 703 ms 5712 KB Output is correct
51 Correct 656 ms 5464 KB Output is correct
52 Correct 650 ms 5464 KB Output is correct
53 Correct 532 ms 6344 KB Output is correct
54 Correct 808 ms 5712 KB Output is correct
55 Correct 656 ms 5456 KB Output is correct
56 Correct 704 ms 5456 KB Output is correct
57 Correct 694 ms 5456 KB Output is correct
58 Correct 697 ms 6736 KB Output is correct
59 Correct 642 ms 6736 KB Output is correct
60 Correct 557 ms 6736 KB Output is correct
61 Correct 609 ms 5584 KB Output is correct
62 Correct 809 ms 5328 KB Output is correct
63 Correct 560 ms 5328 KB Output is correct
64 Correct 608 ms 5456 KB Output is correct
65 Correct 346 ms 5328 KB Output is correct
66 Correct 698 ms 5456 KB Output is correct
67 Correct 722 ms 5504 KB Output is correct
68 Correct 807 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5072 KB Output is correct
2 Correct 2 ms 5024 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 5072 KB Output is correct
10 Correct 2 ms 5072 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 2 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5200 KB Output is correct
15 Correct 3 ms 5200 KB Output is correct
16 Correct 3 ms 5200 KB Output is correct
17 Correct 2 ms 5200 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5328 KB Output is correct
20 Correct 3 ms 5200 KB Output is correct
21 Correct 3 ms 5200 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5200 KB Output is correct
25 Correct 3 ms 5200 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 3 ms 5200 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 3 ms 5188 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 3 ms 5092 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
43 Correct 560 ms 8564 KB Output is correct
44 Correct 728 ms 11852 KB Output is correct
45 Correct 743 ms 11896 KB Output is correct
46 Correct 640 ms 11900 KB Output is correct
47 Correct 524 ms 8548 KB Output is correct
48 Correct 846 ms 11904 KB Output is correct
49 Correct 863 ms 11912 KB Output is correct
50 Correct 739 ms 11892 KB Output is correct
51 Correct 369 ms 5328 KB Output is correct
52 Correct 842 ms 5456 KB Output is correct
53 Correct 665 ms 5456 KB Output is correct
54 Correct 729 ms 5456 KB Output is correct
55 Correct 665 ms 15732 KB Output is correct
56 Correct 764 ms 15884 KB Output is correct
57 Correct 893 ms 15876 KB Output is correct
58 Correct 846 ms 15876 KB Output is correct
59 Correct 880 ms 37292 KB Output is correct
60 Correct 797 ms 37384 KB Output is correct
61 Correct 409 ms 5412 KB Output is correct
62 Correct 695 ms 5456 KB Output is correct
63 Correct 640 ms 5456 KB Output is correct
64 Correct 677 ms 5712 KB Output is correct
65 Correct 683 ms 5712 KB Output is correct
66 Correct 730 ms 5688 KB Output is correct
67 Correct 843 ms 5712 KB Output is correct
68 Correct 703 ms 5712 KB Output is correct
69 Correct 656 ms 5464 KB Output is correct
70 Correct 650 ms 5464 KB Output is correct
71 Correct 532 ms 6344 KB Output is correct
72 Correct 808 ms 5712 KB Output is correct
73 Correct 656 ms 5456 KB Output is correct
74 Correct 704 ms 5456 KB Output is correct
75 Correct 694 ms 5456 KB Output is correct
76 Correct 697 ms 6736 KB Output is correct
77 Correct 642 ms 6736 KB Output is correct
78 Correct 557 ms 6736 KB Output is correct
79 Correct 609 ms 5584 KB Output is correct
80 Correct 809 ms 5328 KB Output is correct
81 Correct 560 ms 5328 KB Output is correct
82 Correct 608 ms 5456 KB Output is correct
83 Correct 346 ms 5328 KB Output is correct
84 Correct 698 ms 5456 KB Output is correct
85 Correct 722 ms 5504 KB Output is correct
86 Correct 807 ms 5456 KB Output is correct
87 Correct 2 ms 5072 KB Output is correct
88 Correct 552 ms 14960 KB Output is correct
89 Correct 790 ms 11980 KB Output is correct
90 Correct 798 ms 11628 KB Output is correct
91 Correct 692 ms 15984 KB Output is correct
92 Correct 691 ms 16004 KB Output is correct
93 Correct 765 ms 15980 KB Output is correct
94 Correct 904 ms 15916 KB Output is correct
95 Correct 820 ms 15940 KB Output is correct
96 Correct 685 ms 11508 KB Output is correct
97 Correct 711 ms 11508 KB Output is correct
98 Correct 566 ms 32112 KB Output is correct
99 Correct 839 ms 15876 KB Output is correct
100 Correct 847 ms 13300 KB Output is correct
101 Correct 706 ms 12520 KB Output is correct
102 Correct 725 ms 11208 KB Output is correct
103 Correct 913 ms 37192 KB Output is correct
104 Correct 978 ms 37628 KB Output is correct
105 Correct 901 ms 37688 KB Output is correct
106 Correct 780 ms 16076 KB Output is correct
107 Correct 866 ms 11748 KB Output is correct
108 Correct 740 ms 11588 KB Output is correct
109 Correct 761 ms 11636 KB Output is correct