Submission #763759

# Submission time Handle Problem Language Result Execution time Memory
763759 2023-06-22T19:04:27 Z raysh07 Digital Circuit (IOI22_circuit) C++17
100 / 100
897 ms 29552 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 2e5 + 69;
const int mod = 1e9 + 2022;
vector <int> adj[N];
int n, m;
int w1[N], tot[N], ok[N], aa[N];
pair <int, int> seg[4 * N];
int lazy[4 * N];

void Build(int l, int r, int pos){
    if (l == r){
        seg[pos].first = aa[l];
        seg[pos].second = w1[l] * aa[l];
        lazy[pos] = 0;
        return;
    }
    
    int mid = (l + r)/2;
    Build(l, mid, pos*2);
    Build(mid + 1, r, pos*2 + 1);
    
    seg[pos].first = seg[pos * 2].first + seg[pos * 2 + 1].first;
    seg[pos].second = seg[pos * 2].second + seg[pos * 2 + 1].second;
}

void updlz(int l, int r, int pos){
    seg[pos].second = seg[pos].first - seg[pos].second;
    lazy[pos] = 0;
    if (l == r) return;
    lazy[pos * 2] ^= 1;
    lazy[pos * 2 + 1] ^= 1;
}

void upd(int l, int r, int pos, int ql, int qr){
    if (lazy[pos] != 0) updlz(l, r, pos);
    if (l >= ql && r <= qr){
        seg[pos].second = seg[pos].first - seg[pos].second;
        if (l == r) return;
        
        lazy[pos * 2] ^= 1;
        lazy[pos * 2 + 1] ^= 1;
        return;
    } else if (l > qr || r < ql){
        return;
    }
    
    int mid = (l + r)/2;
    upd(l, mid, pos*2, ql, qr);
    upd(mid + 1, r, pos*2 + 1, ql, qr);
    
    seg[pos].first = seg[pos * 2].first + seg[pos * 2 + 1].first;
    seg[pos].second = seg[pos * 2].second + seg[pos * 2 + 1].second;
}

int power(int x, int y){
    if (y==0) return 1;
    
    int v = power(x, y/2);
    v *= v; v %= mod;
    
    if (y & 1) return v * x % mod;
    else return v;
}

// void dfs(int u){
//     int cnt = 0;
//     for (int v : adj[u]){
//         dfs(v);
//         cnt++;
//     }
    
//     if (u >= n) return;
    
//     int curr = 1;
//     w1[u] = 0;
    
//     for (int v : adj[u]){
//         ok[v] = curr;
//         curr *= tot[v]; 
//         curr %= mod;
//     }
//     tot[u] = curr * cnt % mod;
//     curr = 1;
    
//     for (int i = adj[u].size() - 1; i >= 0; i--){
//         int v = adj[u][i];
//         ok[v] *= curr;
//         ok[v] %= mod;
//         curr *= tot[v];
//         curr %= mod;
//     }
    
//     for (int v : adj[u]){
//         w1[u] += w1[v] * ok[v] % mod;
//         w1[u] %= mod;
//     }
// }

void dfs2(int u){
    int cnt = 0;
    tot[u] = 1;
    for (int v : adj[u]){
        cnt++;
        dfs2(v);
        tot[u] *= tot[v];
        tot[u] %= mod;
    }
    
    if (cnt != 0)
    tot[u] *= cnt;
    tot[u] %= mod;
}

void dfs(int u){
    int curr = 1;
    for (int v : adj[u]){
        ok[v] = curr;
        curr *= tot[v];
        curr %= mod;
    }
    
    // tot[u] = curr;
    // if (cnt != 1) tot[u] *= cnt, tot[u] %= mod;
    
    reverse(adj[u].begin(), adj[u].end());
    curr = 1;
    for (int v : adj[u]){
        ok[v] *= curr; 
        ok[v] %= mod;
        ok[v] *= ok[u];
        ok[v] %= mod;
        curr *= tot[v];
        curr %= mod;
        
        dfs(v);
    }
}

void init(int32_t N, int32_t M, vector<int32_t> p, vector<int32_t> a) {
    n = N;
    m = M;
    for (int i = 1; i < n + m; i++){
        adj[p[i]].push_back(i);
    }
    
    // for (int i = n; i < n + m; i++){
    //     for (int j = n; j < n + m; j++){
    //         tot[j] = 1;
    //         if (j == i) w1[j] = 1;
    //         else w1[j] = 0;
    //     }
        
    //     dfs(0);
    //     aa[i] = w1[0];
    // }
    
    dfs2(0);
    
    // for (int i = 0; i < n + m; i++) cout << tot[i] << " ";
    // cout << "\n";
    ok[0] = 1;
    dfs(0);
    for (int i = n; i < n + m; i++) {
        aa[i] = ok[i];
       // cout << aa[i] << " ";
    }
  //  cout << "\n";
    
    for (int i = 0; i < m; i++){
        if (a[i] == 1)
        w1[i + n] = 1;
        else w1[i + n] = 0;
    }
    
    Build(n, n + m - 1, 1);
}

int32_t count_ways(int32_t l, int32_t r) {
    upd(n, n + m - 1, 1, l, r);
    
    int32_t ans = seg[1].second % mod;
    
    return ans % mod;
}

// int32_t main(){
//     int32_t n, m; cin >> n >> m;
    
//     vector <int32_t> p(n + m), a(m);
//     for (auto &x : p) cin >> x;
//     for (auto &x : a) cin >> x;
//     init(n, m, p, a);
    
//     int q; cin >> q;
//     while(q--){
//         int32_t l, r;
//         cin >> l >> r;
//         cout << count_ways(l, r) << "\n";
//     }
//     return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4964 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 2 ms 5072 KB Output is correct
7 Correct 2 ms 5028 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 3 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 3 ms 5072 KB Output is correct
8 Correct 3 ms 5100 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4964 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 2 ms 5072 KB Output is correct
7 Correct 2 ms 5028 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 2 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 2 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 2 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 2 ms 5072 KB Output is correct
34 Correct 2 ms 5072 KB Output is correct
35 Correct 2 ms 5016 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 2 ms 5200 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 4 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 9608 KB Output is correct
2 Correct 765 ms 14208 KB Output is correct
3 Correct 833 ms 14208 KB Output is correct
4 Correct 661 ms 14196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 9608 KB Output is correct
2 Correct 765 ms 14208 KB Output is correct
3 Correct 833 ms 14208 KB Output is correct
4 Correct 661 ms 14196 KB Output is correct
5 Correct 558 ms 9608 KB Output is correct
6 Correct 840 ms 14208 KB Output is correct
7 Correct 874 ms 14208 KB Output is correct
8 Correct 648 ms 14204 KB Output is correct
9 Correct 274 ms 5328 KB Output is correct
10 Correct 622 ms 5644 KB Output is correct
11 Correct 812 ms 5628 KB Output is correct
12 Correct 667 ms 5584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 5072 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 3 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 3 ms 5072 KB Output is correct
8 Correct 3 ms 5100 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5200 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 618 ms 9608 KB Output is correct
14 Correct 765 ms 14208 KB Output is correct
15 Correct 833 ms 14208 KB Output is correct
16 Correct 661 ms 14196 KB Output is correct
17 Correct 558 ms 9608 KB Output is correct
18 Correct 840 ms 14208 KB Output is correct
19 Correct 874 ms 14208 KB Output is correct
20 Correct 648 ms 14204 KB Output is correct
21 Correct 274 ms 5328 KB Output is correct
22 Correct 622 ms 5644 KB Output is correct
23 Correct 812 ms 5628 KB Output is correct
24 Correct 667 ms 5584 KB Output is correct
25 Correct 788 ms 20612 KB Output is correct
26 Correct 716 ms 20756 KB Output is correct
27 Correct 769 ms 20836 KB Output is correct
28 Correct 657 ms 20732 KB Output is correct
29 Correct 745 ms 28576 KB Output is correct
30 Correct 794 ms 28576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4964 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 2 ms 5072 KB Output is correct
7 Correct 2 ms 5028 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 2 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 2 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 2 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 2 ms 5072 KB Output is correct
34 Correct 2 ms 5072 KB Output is correct
35 Correct 2 ms 5016 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 2 ms 5200 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 4 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 553 ms 5456 KB Output is correct
44 Correct 751 ms 5456 KB Output is correct
45 Correct 715 ms 5544 KB Output is correct
46 Correct 796 ms 5840 KB Output is correct
47 Correct 785 ms 5840 KB Output is correct
48 Correct 604 ms 5840 KB Output is correct
49 Correct 675 ms 5968 KB Output is correct
50 Correct 716 ms 5840 KB Output is correct
51 Correct 619 ms 5732 KB Output is correct
52 Correct 758 ms 5752 KB Output is correct
53 Correct 675 ms 5584 KB Output is correct
54 Correct 751 ms 5840 KB Output is correct
55 Correct 709 ms 5840 KB Output is correct
56 Correct 701 ms 5712 KB Output is correct
57 Correct 628 ms 5584 KB Output is correct
58 Correct 725 ms 6224 KB Output is correct
59 Correct 588 ms 6408 KB Output is correct
60 Correct 683 ms 6388 KB Output is correct
61 Correct 783 ms 5584 KB Output is correct
62 Correct 641 ms 5456 KB Output is correct
63 Correct 703 ms 5456 KB Output is correct
64 Correct 700 ms 5680 KB Output is correct
65 Correct 279 ms 5328 KB Output is correct
66 Correct 736 ms 5584 KB Output is correct
67 Correct 694 ms 5584 KB Output is correct
68 Correct 728 ms 5628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4964 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 2 ms 5072 KB Output is correct
7 Correct 2 ms 5028 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 2 ms 4944 KB Output is correct
10 Correct 3 ms 5072 KB Output is correct
11 Correct 2 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 2 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5200 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 2 ms 5072 KB Output is correct
25 Correct 4 ms 5072 KB Output is correct
26 Correct 4 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 2 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 2 ms 5072 KB Output is correct
34 Correct 2 ms 5072 KB Output is correct
35 Correct 2 ms 5016 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 3 ms 5200 KB Output is correct
38 Correct 2 ms 5200 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 4 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 3 ms 5072 KB Output is correct
43 Correct 618 ms 9608 KB Output is correct
44 Correct 765 ms 14208 KB Output is correct
45 Correct 833 ms 14208 KB Output is correct
46 Correct 661 ms 14196 KB Output is correct
47 Correct 558 ms 9608 KB Output is correct
48 Correct 840 ms 14208 KB Output is correct
49 Correct 874 ms 14208 KB Output is correct
50 Correct 648 ms 14204 KB Output is correct
51 Correct 274 ms 5328 KB Output is correct
52 Correct 622 ms 5644 KB Output is correct
53 Correct 812 ms 5628 KB Output is correct
54 Correct 667 ms 5584 KB Output is correct
55 Correct 788 ms 20612 KB Output is correct
56 Correct 716 ms 20756 KB Output is correct
57 Correct 769 ms 20836 KB Output is correct
58 Correct 657 ms 20732 KB Output is correct
59 Correct 745 ms 28576 KB Output is correct
60 Correct 794 ms 28576 KB Output is correct
61 Correct 553 ms 5456 KB Output is correct
62 Correct 751 ms 5456 KB Output is correct
63 Correct 715 ms 5544 KB Output is correct
64 Correct 796 ms 5840 KB Output is correct
65 Correct 785 ms 5840 KB Output is correct
66 Correct 604 ms 5840 KB Output is correct
67 Correct 675 ms 5968 KB Output is correct
68 Correct 716 ms 5840 KB Output is correct
69 Correct 619 ms 5732 KB Output is correct
70 Correct 758 ms 5752 KB Output is correct
71 Correct 675 ms 5584 KB Output is correct
72 Correct 751 ms 5840 KB Output is correct
73 Correct 709 ms 5840 KB Output is correct
74 Correct 701 ms 5712 KB Output is correct
75 Correct 628 ms 5584 KB Output is correct
76 Correct 725 ms 6224 KB Output is correct
77 Correct 588 ms 6408 KB Output is correct
78 Correct 683 ms 6388 KB Output is correct
79 Correct 783 ms 5584 KB Output is correct
80 Correct 641 ms 5456 KB Output is correct
81 Correct 703 ms 5456 KB Output is correct
82 Correct 700 ms 5680 KB Output is correct
83 Correct 279 ms 5328 KB Output is correct
84 Correct 736 ms 5584 KB Output is correct
85 Correct 694 ms 5584 KB Output is correct
86 Correct 728 ms 5628 KB Output is correct
87 Correct 2 ms 4944 KB Output is correct
88 Correct 462 ms 20152 KB Output is correct
89 Correct 852 ms 14456 KB Output is correct
90 Correct 783 ms 14448 KB Output is correct
91 Correct 757 ms 21360 KB Output is correct
92 Correct 897 ms 21348 KB Output is correct
93 Correct 868 ms 21372 KB Output is correct
94 Correct 839 ms 21404 KB Output is correct
95 Correct 748 ms 21364 KB Output is correct
96 Correct 783 ms 16196 KB Output is correct
97 Correct 759 ms 16188 KB Output is correct
98 Correct 752 ms 18248 KB Output is correct
99 Correct 786 ms 20768 KB Output is correct
100 Correct 887 ms 19068 KB Output is correct
101 Correct 780 ms 18048 KB Output is correct
102 Correct 689 ms 16252 KB Output is correct
103 Correct 814 ms 28544 KB Output is correct
104 Correct 848 ms 29512 KB Output is correct
105 Correct 601 ms 29552 KB Output is correct
106 Correct 819 ms 15856 KB Output is correct
107 Correct 717 ms 16256 KB Output is correct
108 Correct 814 ms 16712 KB Output is correct
109 Correct 771 ms 16456 KB Output is correct