Submission #980225

# Submission time Handle Problem Language Result Execution time Memory
980225 2024-05-12T01:22:35 Z nextgenxing Digital Circuit (IOI22_circuit) C++17
100 / 100
734 ms 51652 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F0R(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0Rd(i, n) for(int i = (n)-1; i >= 0; i--)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define ff first 
#define ss second 
const int MAX_N = 2e5+5;
const ll MOD = 1e9+2022;

ll add(ll a, ll b){ return (a+b)%MOD; }
ll sub(ll a, ll b){ return (a-b+MOD)%MOD; }
ll mul(ll a, ll b){ return (a*b)%MOD; }
ll ADD(ll &a, ll b){ return a=add(a, b); }
ll SUB(ll &a, ll b){ return a=sub(a, b); }
ll MUL(ll &a, ll b){ return a=mul(a, b); }

int N, M;
ll tot[MAX_N], w[MAX_N], state[MAX_N], sum[4*MAX_N], seg[4*MAX_N], lazy[4*MAX_N], active[4*MAX_N];
vector<int> adj[MAX_N];

void dfs(int curr){
    if(adj[curr].size()) tot[curr] = adj[curr].size();
    else tot[curr] = 1;
    vector<ll> a(adj[curr].size()), b(adj[curr].size());
    F0R(i, adj[curr].size()){
        dfs(adj[curr][i]);
        if(i) a[i] = mul(tot[adj[curr][i]], a[i-1]);
        else a[i] = tot[adj[curr][i]];
        b[i] = tot[adj[curr][i]];
        MUL(tot[curr], tot[adj[curr][i]]);
    }
    F0Rd(i, adj[curr].size()-1) MUL(b[i], b[i+1]);
    F0R(i, adj[curr].size()){
        ll num = 1;
        if(i) MUL(num, a[i-1]);
        if(i < adj[curr].size()-1) MUL(num, b[i+1]);
        MUL(w[adj[curr][i]], num);
    }
}

void dfs2(int curr){
    for(auto child : adj[curr]){
        MUL(w[child], w[curr]);
        dfs2(child);
    }
}

void build(int idx, int l, int r){
    if(l == r){
        active[idx] = state[l];
        sum[idx] = w[l+N];
        seg[idx] = mul(active[idx], sum[idx]);
        return;
    }
    int m = (l+r)/2;
    build(2*idx, l, m), build(2*idx+1, m+1, r);
    seg[idx] = add(seg[2*idx], seg[2*idx+1]);
    sum[idx] = add(sum[2*idx], sum[2*idx+1]);
}

void prop(int idx, int l, int r){
    if(lazy[idx]){
        seg[idx] = sub(sum[idx], seg[idx]);
        if(l != r){
            lazy[2*idx] ^= lazy[idx];
            lazy[2*idx+1] ^= lazy[idx];
        } else active[idx] ^= 1;
        lazy[idx] = 0;
    }
}

void upd(int ql, int qr, int idx, int l, int r){
    prop(idx, l, r);
    if(ql > r || qr < l) return;
    if(ql <= l && qr >= r){
        lazy[idx] ^= 1;
        prop(idx, l, r);
        return;
    }
    int m = (l+r)/2;
    upd(ql, qr, 2*idx, l, m), upd(ql, qr, 2*idx+1, m+1, r);
    seg[idx] = add(seg[2*idx], seg[2*idx+1]);
}

ll qry(int ql, int qr, int idx, int l, int r){
    prop(idx, l, r);
    if(ql > r || qr < l) return 0;
    if(ql <= l && qr >= r) return seg[idx];
    int m = (l+r)/2;
    return add(qry(ql, qr, 2*idx, l, m), qry(ql, qr, 2*idx+1, m+1, r));
}

void init(int n, int m, vector<int> P, vector<int> A){
    N = n, M = m;
    FOR(i, 1, n+m) adj[P[i]].push_back(i);
    F0R(i, m) state[i] = A[i];
    F0R(i, n+m) w[i] = 1;
    dfs(0), dfs2(0);
    build(1, 0, M-1);
}

int count_ways(int l, int r){
    upd(l-N, r-N, 1, 0, M-1);
    return qry(0, M-1, 1, 0, M-1);
}

// int main() {
//   int N, M, Q;
//   assert(3 == scanf("%d %d %d", &N, &M, &Q));
//   std::vector<int> P(N + M), A(M);
//   for (int i = 0; i < N + M; ++i) {
//     assert(1 == scanf("%d", &P[i]));
//   }
//   for (int j = 0; j < M; ++j) {
//     assert(1 == scanf("%d", &A[j]));
//   }
//   init(N, M, P, A);

//   for (int i = 0; i < Q; ++i) {
//     int L, R;
//     assert(2 == scanf("%d %d", &L, &R));
//     printf("%d\n", count_ways(L, R));
//   }
//   return 0;
// }

Compilation message

circuit.cpp: In function 'void dfs(int)':
circuit.cpp:7:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define F0R(i, n) for(int i = 0; i < (n); i++)
      |                                    ^
circuit.cpp:31:5: note: in expansion of macro 'F0R'
   31 |     F0R(i, adj[curr].size()){
      |     ^~~
circuit.cpp:7:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define F0R(i, n) for(int i = 0; i < (n); i++)
      |                                    ^
circuit.cpp:39:5: note: in expansion of macro 'F0R'
   39 |     F0R(i, adj[curr].size()){
      |     ^~~
circuit.cpp:42:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         if(i < adj[curr].size()-1) MUL(num, b[i+1]);
      |            ~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14984 KB Output is correct
5 Correct 2 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 15188 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14936 KB Output is correct
5 Correct 3 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14984 KB Output is correct
5 Correct 2 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
9 Correct 2 ms 15188 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14936 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14936 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14936 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 15192 KB Output is correct
19 Correct 3 ms 15192 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
23 Correct 3 ms 14936 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 15192 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 14936 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 3 ms 14936 KB Output is correct
31 Correct 3 ms 15192 KB Output is correct
32 Correct 3 ms 14936 KB Output is correct
33 Correct 3 ms 14936 KB Output is correct
34 Correct 3 ms 14936 KB Output is correct
35 Correct 3 ms 15028 KB Output is correct
36 Correct 3 ms 15448 KB Output is correct
37 Correct 3 ms 15192 KB Output is correct
38 Correct 3 ms 15192 KB Output is correct
39 Correct 3 ms 14936 KB Output is correct
40 Correct 3 ms 14936 KB Output is correct
41 Correct 3 ms 14936 KB Output is correct
42 Correct 2 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 17296 KB Output is correct
2 Correct 629 ms 19324 KB Output is correct
3 Correct 579 ms 19320 KB Output is correct
4 Correct 608 ms 19380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 17296 KB Output is correct
2 Correct 629 ms 19324 KB Output is correct
3 Correct 579 ms 19320 KB Output is correct
4 Correct 608 ms 19380 KB Output is correct
5 Correct 505 ms 17360 KB Output is correct
6 Correct 700 ms 20080 KB Output is correct
7 Correct 677 ms 20320 KB Output is correct
8 Correct 607 ms 19332 KB Output is correct
9 Correct 285 ms 14936 KB Output is correct
10 Correct 584 ms 15192 KB Output is correct
11 Correct 610 ms 15192 KB Output is correct
12 Correct 588 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 15188 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14936 KB Output is correct
5 Correct 3 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 14936 KB Output is correct
13 Correct 401 ms 17296 KB Output is correct
14 Correct 629 ms 19324 KB Output is correct
15 Correct 579 ms 19320 KB Output is correct
16 Correct 608 ms 19380 KB Output is correct
17 Correct 505 ms 17360 KB Output is correct
18 Correct 700 ms 20080 KB Output is correct
19 Correct 677 ms 20320 KB Output is correct
20 Correct 607 ms 19332 KB Output is correct
21 Correct 285 ms 14936 KB Output is correct
22 Correct 584 ms 15192 KB Output is correct
23 Correct 610 ms 15192 KB Output is correct
24 Correct 588 ms 15192 KB Output is correct
25 Correct 647 ms 30560 KB Output is correct
26 Correct 683 ms 30560 KB Output is correct
27 Correct 689 ms 31324 KB Output is correct
28 Correct 569 ms 27952 KB Output is correct
29 Correct 686 ms 49276 KB Output is correct
30 Correct 649 ms 49300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14984 KB Output is correct
5 Correct 2 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
9 Correct 2 ms 15188 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14936 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14936 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14936 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 15192 KB Output is correct
19 Correct 3 ms 15192 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
23 Correct 3 ms 14936 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 15192 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 14936 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 3 ms 14936 KB Output is correct
31 Correct 3 ms 15192 KB Output is correct
32 Correct 3 ms 14936 KB Output is correct
33 Correct 3 ms 14936 KB Output is correct
34 Correct 3 ms 14936 KB Output is correct
35 Correct 3 ms 15028 KB Output is correct
36 Correct 3 ms 15448 KB Output is correct
37 Correct 3 ms 15192 KB Output is correct
38 Correct 3 ms 15192 KB Output is correct
39 Correct 3 ms 14936 KB Output is correct
40 Correct 3 ms 14936 KB Output is correct
41 Correct 3 ms 14936 KB Output is correct
42 Correct 2 ms 14936 KB Output is correct
43 Correct 434 ms 15192 KB Output is correct
44 Correct 575 ms 15192 KB Output is correct
45 Correct 581 ms 15192 KB Output is correct
46 Correct 519 ms 15704 KB Output is correct
47 Correct 593 ms 15192 KB Output is correct
48 Correct 607 ms 15192 KB Output is correct
49 Correct 569 ms 15448 KB Output is correct
50 Correct 548 ms 15192 KB Output is correct
51 Correct 558 ms 15448 KB Output is correct
52 Correct 581 ms 15300 KB Output is correct
53 Correct 501 ms 15960 KB Output is correct
54 Correct 574 ms 15192 KB Output is correct
55 Correct 559 ms 15192 KB Output is correct
56 Correct 568 ms 15348 KB Output is correct
57 Correct 589 ms 15192 KB Output is correct
58 Correct 581 ms 16216 KB Output is correct
59 Correct 566 ms 16472 KB Output is correct
60 Correct 569 ms 16216 KB Output is correct
61 Correct 536 ms 15448 KB Output is correct
62 Correct 565 ms 14936 KB Output is correct
63 Correct 529 ms 15192 KB Output is correct
64 Correct 534 ms 15448 KB Output is correct
65 Correct 251 ms 14936 KB Output is correct
66 Correct 599 ms 15192 KB Output is correct
67 Correct 577 ms 15192 KB Output is correct
68 Correct 525 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14984 KB Output is correct
5 Correct 2 ms 14936 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
9 Correct 2 ms 15188 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14936 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14936 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14936 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 15192 KB Output is correct
19 Correct 3 ms 15192 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
23 Correct 3 ms 14936 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 15192 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 14936 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 3 ms 14936 KB Output is correct
31 Correct 3 ms 15192 KB Output is correct
32 Correct 3 ms 14936 KB Output is correct
33 Correct 3 ms 14936 KB Output is correct
34 Correct 3 ms 14936 KB Output is correct
35 Correct 3 ms 15028 KB Output is correct
36 Correct 3 ms 15448 KB Output is correct
37 Correct 3 ms 15192 KB Output is correct
38 Correct 3 ms 15192 KB Output is correct
39 Correct 3 ms 14936 KB Output is correct
40 Correct 3 ms 14936 KB Output is correct
41 Correct 3 ms 14936 KB Output is correct
42 Correct 2 ms 14936 KB Output is correct
43 Correct 401 ms 17296 KB Output is correct
44 Correct 629 ms 19324 KB Output is correct
45 Correct 579 ms 19320 KB Output is correct
46 Correct 608 ms 19380 KB Output is correct
47 Correct 505 ms 17360 KB Output is correct
48 Correct 700 ms 20080 KB Output is correct
49 Correct 677 ms 20320 KB Output is correct
50 Correct 607 ms 19332 KB Output is correct
51 Correct 285 ms 14936 KB Output is correct
52 Correct 584 ms 15192 KB Output is correct
53 Correct 610 ms 15192 KB Output is correct
54 Correct 588 ms 15192 KB Output is correct
55 Correct 647 ms 30560 KB Output is correct
56 Correct 683 ms 30560 KB Output is correct
57 Correct 689 ms 31324 KB Output is correct
58 Correct 569 ms 27952 KB Output is correct
59 Correct 686 ms 49276 KB Output is correct
60 Correct 649 ms 49300 KB Output is correct
61 Correct 434 ms 15192 KB Output is correct
62 Correct 575 ms 15192 KB Output is correct
63 Correct 581 ms 15192 KB Output is correct
64 Correct 519 ms 15704 KB Output is correct
65 Correct 593 ms 15192 KB Output is correct
66 Correct 607 ms 15192 KB Output is correct
67 Correct 569 ms 15448 KB Output is correct
68 Correct 548 ms 15192 KB Output is correct
69 Correct 558 ms 15448 KB Output is correct
70 Correct 581 ms 15300 KB Output is correct
71 Correct 501 ms 15960 KB Output is correct
72 Correct 574 ms 15192 KB Output is correct
73 Correct 559 ms 15192 KB Output is correct
74 Correct 568 ms 15348 KB Output is correct
75 Correct 589 ms 15192 KB Output is correct
76 Correct 581 ms 16216 KB Output is correct
77 Correct 566 ms 16472 KB Output is correct
78 Correct 569 ms 16216 KB Output is correct
79 Correct 536 ms 15448 KB Output is correct
80 Correct 565 ms 14936 KB Output is correct
81 Correct 529 ms 15192 KB Output is correct
82 Correct 534 ms 15448 KB Output is correct
83 Correct 251 ms 14936 KB Output is correct
84 Correct 599 ms 15192 KB Output is correct
85 Correct 577 ms 15192 KB Output is correct
86 Correct 525 ms 15192 KB Output is correct
87 Correct 2 ms 14936 KB Output is correct
88 Correct 399 ms 30052 KB Output is correct
89 Correct 623 ms 20040 KB Output is correct
90 Correct 642 ms 19824 KB Output is correct
91 Correct 601 ms 28748 KB Output is correct
92 Correct 678 ms 30572 KB Output is correct
93 Correct 687 ms 29252 KB Output is correct
94 Correct 734 ms 31076 KB Output is correct
95 Correct 642 ms 29076 KB Output is correct
96 Correct 573 ms 25184 KB Output is correct
97 Correct 670 ms 25504 KB Output is correct
98 Correct 546 ms 39768 KB Output is correct
99 Correct 707 ms 29920 KB Output is correct
100 Correct 657 ms 26776 KB Output is correct
101 Correct 689 ms 26480 KB Output is correct
102 Correct 685 ms 27292 KB Output is correct
103 Correct 720 ms 49276 KB Output is correct
104 Correct 711 ms 51652 KB Output is correct
105 Correct 606 ms 49700 KB Output is correct
106 Correct 673 ms 24436 KB Output is correct
107 Correct 656 ms 28384 KB Output is correct
108 Correct 632 ms 27692 KB Output is correct
109 Correct 634 ms 25920 KB Output is correct