답안 #964822

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964822 2024-04-17T15:37:02 Z steveonalex 디지털 회로 (IOI22_circuit) C++17
100 / 100
735 ms 31984 KB
#include <bits/stdc++.h>
#include "circuit.h"

using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69, MOD = 1e9 + 2022;

void add(int &a, int b){
    a += b;
    if (a >= MOD) a -= MOD;
}

void sub(int &a, int b){
    a -= b;
    if (a < 0) a += MOD;
}

int n, m;
vector<int> graph[N];
int prod[N], val[N];

void dfs(int u, int p){
    if (u >= n) return;
    prod[u] = graph[u].size();
    for(int v: graph[u]) if (v != p){
        dfs(v, u);
        prod[u] = 1LL * prod[u] * prod[v] % MOD;
    }
}

void go(int u, int p, int odd){
    if (u >= n){
        val[u] = odd;
        return;
    }
    vector<int> pref(graph[u].size() + 1, 1), suff(graph[u].size() + 1, 1);
    for(int i = 0; i<graph[u].size(); ++i){
        pref[i+1] = 1LL * pref[i] * prod[graph[u][i]] % MOD;
    }
    for(int i = graph[u].size() - 1; i>=0; --i){
        suff[i] = 1LL * suff[i+1] * prod[graph[u][i]] % MOD;
    }
    for(int i = 0; i<graph[u].size(); ++i){
        int cur = 1LL * pref[i] * suff[i+1] % MOD;
        go(graph[u][i], u, 1LL * odd * cur % MOD);
    }
}

struct SegmentTree{
    struct Node{
        int sum, cur, lazy;
        Node(){sum = cur = lazy = 0;}
    };

    int n;
    vector<Node> a;
    SegmentTree(int _n, vector<int> b){
        n = _n;
        if (n == 0) return;
        a.resize(n * 4 + 4);
        build_tree(0, n-1, 1, b);
    }

    Node combine(Node x, Node y){
        Node ans = x;
        add(ans.sum, y.sum);
        add(ans.cur, y.cur);
        ans.lazy = 0;
        return ans;
    }

    void flip(Node &x){
        x.lazy ^= 1;
        x.cur = x.sum-x.cur;
        if (x.cur < 0) x.cur += MOD;
    }

    void build_tree(int l, int r, int id, vector<int> &b){
        if (l == r){a[id].sum = b[l]; return;}
        int mid = (l + r) >> 1;
        build_tree(l, mid, id * 2, b);
        build_tree(mid + 1, r, id * 2 + 1, b);
        a[id] = combine(a[id * 2], a[id * 2 + 1]);
    }

    void down(int id){
        if (a[id].lazy){
            flip(a[id * 2]);
            flip(a[id * 2 + 1]);
            a[id].lazy = 1;
        }
    }

    void update(int u, int v, int l, int r, int id){
        if (u <= l && r <= v){
            flip(a[id]);
            return;
        }
        int mid = (l + r) >> 1;
        if (a[id].lazy) down(id);
        if (u <= mid) update(u, v, l, mid, id * 2);
        if (v > mid) update(u, v, mid + 1, r, id * 2 + 1); 
        a[id] = combine(a[id * 2], a[id * 2 + 1]);
    }
    void update(int u, int v){return update(u, v, 0, n-1, 1);}

    int get(){return a[1].cur;}
};

SegmentTree st(0, vector<int>(0));

void init(int _n, int _m, vector<int> P, vector<int> A) {
    n = _n, m = _m;
    for(int i = 1; i<n+m; ++i){
        graph[P[i]].push_back(i);
    }

    for(int i = 0; i<m; ++i){
        prod[i+n] = 1;
    }

    dfs(0, 0);
    go(0, 0, 1);

    vector<int> b;
    for(int i = n; i<n+m; ++i) b.push_back(val[i]);
    st = SegmentTree(m, b);
    for(int i =0; i<m; ++i) if (A[i]) {
        st.update(i, i);
    }
}   


int count_ways(int l, int r) {
    l -= n; r -= n;
    st.update(l, r);
    return st.get();
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     int n, m; cin >> n >> m;
//     vector<int> P(n+m, -1);
//     for(int i = 1; i<n+m; ++i) cin >> P[i];
//     vector<int> A(m); 
//     for(int i = 0; i<m; ++i) cin >> A[i];

//     init(n, m, P, A);
//     int q; cin >> q;
//     while(q--){
//         int l, r; cin >> l >> r;
//         cout << count_ways(l, r) << "\n";
//     }


//     return 0;
// }

Compilation message

circuit.cpp: In function 'void go(int, int, int)':
circuit.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for(int i = 0; i<graph[u].size(); ++i){
      |                    ~^~~~~~~~~~~~~~~~
circuit.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for(int i = 0; i<graph[u].size(); ++i){
      |                    ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6488 KB Output is correct
4 Correct 2 ms 6488 KB Output is correct
5 Correct 2 ms 6444 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 3 ms 6644 KB Output is correct
4 Correct 3 ms 6488 KB Output is correct
5 Correct 2 ms 6488 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 3 ms 6488 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6488 KB Output is correct
4 Correct 2 ms 6488 KB Output is correct
5 Correct 2 ms 6444 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 3 ms 6644 KB Output is correct
12 Correct 3 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6488 KB Output is correct
15 Correct 3 ms 6484 KB Output is correct
16 Correct 3 ms 6488 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6488 KB Output is correct
21 Correct 2 ms 6488 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 2 ms 6488 KB Output is correct
24 Correct 3 ms 6488 KB Output is correct
25 Correct 3 ms 6488 KB Output is correct
26 Correct 3 ms 6740 KB Output is correct
27 Correct 3 ms 6488 KB Output is correct
28 Correct 3 ms 6488 KB Output is correct
29 Correct 2 ms 6488 KB Output is correct
30 Correct 3 ms 6488 KB Output is correct
31 Correct 2 ms 6744 KB Output is correct
32 Correct 2 ms 6488 KB Output is correct
33 Correct 2 ms 6488 KB Output is correct
34 Correct 4 ms 6628 KB Output is correct
35 Correct 2 ms 6488 KB Output is correct
36 Correct 2 ms 6744 KB Output is correct
37 Correct 2 ms 6744 KB Output is correct
38 Correct 2 ms 6744 KB Output is correct
39 Correct 2 ms 6488 KB Output is correct
40 Correct 2 ms 6488 KB Output is correct
41 Correct 2 ms 6488 KB Output is correct
42 Correct 2 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 472 ms 10260 KB Output is correct
2 Correct 610 ms 13844 KB Output is correct
3 Correct 648 ms 13836 KB Output is correct
4 Correct 649 ms 13808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 472 ms 10260 KB Output is correct
2 Correct 610 ms 13844 KB Output is correct
3 Correct 648 ms 13836 KB Output is correct
4 Correct 649 ms 13808 KB Output is correct
5 Correct 527 ms 10072 KB Output is correct
6 Correct 638 ms 13828 KB Output is correct
7 Correct 638 ms 13788 KB Output is correct
8 Correct 618 ms 13844 KB Output is correct
9 Correct 289 ms 6748 KB Output is correct
10 Correct 562 ms 7000 KB Output is correct
11 Correct 593 ms 7000 KB Output is correct
12 Correct 576 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 3 ms 6644 KB Output is correct
4 Correct 3 ms 6488 KB Output is correct
5 Correct 2 ms 6488 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 3 ms 6488 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 472 ms 10260 KB Output is correct
14 Correct 610 ms 13844 KB Output is correct
15 Correct 648 ms 13836 KB Output is correct
16 Correct 649 ms 13808 KB Output is correct
17 Correct 527 ms 10072 KB Output is correct
18 Correct 638 ms 13828 KB Output is correct
19 Correct 638 ms 13788 KB Output is correct
20 Correct 618 ms 13844 KB Output is correct
21 Correct 289 ms 6748 KB Output is correct
22 Correct 562 ms 7000 KB Output is correct
23 Correct 593 ms 7000 KB Output is correct
24 Correct 576 ms 7000 KB Output is correct
25 Correct 719 ms 17260 KB Output is correct
26 Correct 735 ms 17536 KB Output is correct
27 Correct 720 ms 17408 KB Output is correct
28 Correct 542 ms 17544 KB Output is correct
29 Correct 689 ms 30784 KB Output is correct
30 Correct 661 ms 30756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6488 KB Output is correct
4 Correct 2 ms 6488 KB Output is correct
5 Correct 2 ms 6444 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 3 ms 6644 KB Output is correct
12 Correct 3 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6488 KB Output is correct
15 Correct 3 ms 6484 KB Output is correct
16 Correct 3 ms 6488 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6488 KB Output is correct
21 Correct 2 ms 6488 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 2 ms 6488 KB Output is correct
24 Correct 3 ms 6488 KB Output is correct
25 Correct 3 ms 6488 KB Output is correct
26 Correct 3 ms 6740 KB Output is correct
27 Correct 3 ms 6488 KB Output is correct
28 Correct 3 ms 6488 KB Output is correct
29 Correct 2 ms 6488 KB Output is correct
30 Correct 3 ms 6488 KB Output is correct
31 Correct 2 ms 6744 KB Output is correct
32 Correct 2 ms 6488 KB Output is correct
33 Correct 2 ms 6488 KB Output is correct
34 Correct 4 ms 6628 KB Output is correct
35 Correct 2 ms 6488 KB Output is correct
36 Correct 2 ms 6744 KB Output is correct
37 Correct 2 ms 6744 KB Output is correct
38 Correct 2 ms 6744 KB Output is correct
39 Correct 2 ms 6488 KB Output is correct
40 Correct 2 ms 6488 KB Output is correct
41 Correct 2 ms 6488 KB Output is correct
42 Correct 2 ms 6488 KB Output is correct
43 Correct 418 ms 6844 KB Output is correct
44 Correct 573 ms 6872 KB Output is correct
45 Correct 583 ms 6740 KB Output is correct
46 Correct 558 ms 7108 KB Output is correct
47 Correct 608 ms 7000 KB Output is correct
48 Correct 610 ms 7000 KB Output is correct
49 Correct 582 ms 7000 KB Output is correct
50 Correct 585 ms 7000 KB Output is correct
51 Correct 549 ms 6744 KB Output is correct
52 Correct 594 ms 6744 KB Output is correct
53 Correct 551 ms 7512 KB Output is correct
54 Correct 575 ms 7000 KB Output is correct
55 Correct 605 ms 7000 KB Output is correct
56 Correct 588 ms 7000 KB Output is correct
57 Correct 590 ms 6744 KB Output is correct
58 Correct 606 ms 7512 KB Output is correct
59 Correct 611 ms 7768 KB Output is correct
60 Correct 596 ms 7768 KB Output is correct
61 Correct 575 ms 7032 KB Output is correct
62 Correct 554 ms 6744 KB Output is correct
63 Correct 582 ms 6744 KB Output is correct
64 Correct 595 ms 6748 KB Output is correct
65 Correct 286 ms 6752 KB Output is correct
66 Correct 605 ms 7000 KB Output is correct
67 Correct 582 ms 7000 KB Output is correct
68 Correct 586 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6488 KB Output is correct
4 Correct 2 ms 6488 KB Output is correct
5 Correct 2 ms 6444 KB Output is correct
6 Correct 2 ms 6488 KB Output is correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 3 ms 6644 KB Output is correct
12 Correct 3 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6488 KB Output is correct
15 Correct 3 ms 6484 KB Output is correct
16 Correct 3 ms 6488 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6488 KB Output is correct
21 Correct 2 ms 6488 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 2 ms 6488 KB Output is correct
24 Correct 3 ms 6488 KB Output is correct
25 Correct 3 ms 6488 KB Output is correct
26 Correct 3 ms 6740 KB Output is correct
27 Correct 3 ms 6488 KB Output is correct
28 Correct 3 ms 6488 KB Output is correct
29 Correct 2 ms 6488 KB Output is correct
30 Correct 3 ms 6488 KB Output is correct
31 Correct 2 ms 6744 KB Output is correct
32 Correct 2 ms 6488 KB Output is correct
33 Correct 2 ms 6488 KB Output is correct
34 Correct 4 ms 6628 KB Output is correct
35 Correct 2 ms 6488 KB Output is correct
36 Correct 2 ms 6744 KB Output is correct
37 Correct 2 ms 6744 KB Output is correct
38 Correct 2 ms 6744 KB Output is correct
39 Correct 2 ms 6488 KB Output is correct
40 Correct 2 ms 6488 KB Output is correct
41 Correct 2 ms 6488 KB Output is correct
42 Correct 2 ms 6488 KB Output is correct
43 Correct 472 ms 10260 KB Output is correct
44 Correct 610 ms 13844 KB Output is correct
45 Correct 648 ms 13836 KB Output is correct
46 Correct 649 ms 13808 KB Output is correct
47 Correct 527 ms 10072 KB Output is correct
48 Correct 638 ms 13828 KB Output is correct
49 Correct 638 ms 13788 KB Output is correct
50 Correct 618 ms 13844 KB Output is correct
51 Correct 289 ms 6748 KB Output is correct
52 Correct 562 ms 7000 KB Output is correct
53 Correct 593 ms 7000 KB Output is correct
54 Correct 576 ms 7000 KB Output is correct
55 Correct 719 ms 17260 KB Output is correct
56 Correct 735 ms 17536 KB Output is correct
57 Correct 720 ms 17408 KB Output is correct
58 Correct 542 ms 17544 KB Output is correct
59 Correct 689 ms 30784 KB Output is correct
60 Correct 661 ms 30756 KB Output is correct
61 Correct 418 ms 6844 KB Output is correct
62 Correct 573 ms 6872 KB Output is correct
63 Correct 583 ms 6740 KB Output is correct
64 Correct 558 ms 7108 KB Output is correct
65 Correct 608 ms 7000 KB Output is correct
66 Correct 610 ms 7000 KB Output is correct
67 Correct 582 ms 7000 KB Output is correct
68 Correct 585 ms 7000 KB Output is correct
69 Correct 549 ms 6744 KB Output is correct
70 Correct 594 ms 6744 KB Output is correct
71 Correct 551 ms 7512 KB Output is correct
72 Correct 575 ms 7000 KB Output is correct
73 Correct 605 ms 7000 KB Output is correct
74 Correct 588 ms 7000 KB Output is correct
75 Correct 590 ms 6744 KB Output is correct
76 Correct 606 ms 7512 KB Output is correct
77 Correct 611 ms 7768 KB Output is correct
78 Correct 596 ms 7768 KB Output is correct
79 Correct 575 ms 7032 KB Output is correct
80 Correct 554 ms 6744 KB Output is correct
81 Correct 582 ms 6744 KB Output is correct
82 Correct 595 ms 6748 KB Output is correct
83 Correct 286 ms 6752 KB Output is correct
84 Correct 605 ms 7000 KB Output is correct
85 Correct 582 ms 7000 KB Output is correct
86 Correct 586 ms 7000 KB Output is correct
87 Correct 2 ms 6488 KB Output is correct
88 Correct 436 ms 16032 KB Output is correct
89 Correct 674 ms 13092 KB Output is correct
90 Correct 682 ms 13268 KB Output is correct
91 Correct 648 ms 17620 KB Output is correct
92 Correct 710 ms 17544 KB Output is correct
93 Correct 713 ms 17616 KB Output is correct
94 Correct 713 ms 17624 KB Output is correct
95 Correct 673 ms 17492 KB Output is correct
96 Correct 583 ms 14016 KB Output is correct
97 Correct 657 ms 14276 KB Output is correct
98 Correct 592 ms 28964 KB Output is correct
99 Correct 684 ms 17544 KB Output is correct
100 Correct 697 ms 15656 KB Output is correct
101 Correct 637 ms 15224 KB Output is correct
102 Correct 710 ms 14364 KB Output is correct
103 Correct 710 ms 30760 KB Output is correct
104 Correct 706 ms 31876 KB Output is correct
105 Correct 654 ms 31984 KB Output is correct
106 Correct 652 ms 16220 KB Output is correct
107 Correct 643 ms 12716 KB Output is correct
108 Correct 669 ms 13396 KB Output is correct
109 Correct 668 ms 14300 KB Output is correct