Submission #1058832

# Submission time Handle Problem Language Result Execution time Memory
1058832 2024-08-14T14:16:31 Z dozer Digital Circuit (IOI22_circuit) C++17
100 / 100
697 ms 64468 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define LL node * 2
#define RR node * 2 + 1
#define ll long long
#define MAXN 500005

const int modulo = 1000002022;
const ll INF = 2e18 + 7;

int add(ll a, ll b){
    if (a + b < modulo) return a + b;
    return a + b - modulo;
}

int subs(ll a, ll b){
    if (a < b) return a - b + modulo;
    return a - b;
}

int mul(ll a, ll b){
    return (a * b) % modulo;
}

int fe(ll a, ll b){
    if (b == 0) return 1;
    if (b % 2) return mul(a, fe(a, b - 1));
    int tmp = fe(a, b / 2);
    return mul(tmp, tmp);
}


struct SegTree{
    vector<int> t, lazy, arr, sum;
    int n;

    void build(int node, int l, int r){
        if (l == r){
            sum[node] = arr[l];
            t[node] = 0;
            return;
        }
        int mid = (l + r) / 2;
        build(LL, l, mid);
        build(RR, mid + 1, r);
        sum[node] = add(sum[LL], sum[RR]);
        t[node] = 0;
    }

    void push(int node, int l, int r){
        if (lazy[node] == 0) return;
        t[node] = subs(sum[node], t[node]);
        lazy[node] = 0;
        if (l != r) lazy[LL] ^= 1, lazy[RR] ^= 1;
    }

    void update(int node, int l, int r, int sl, int sr){
        push(node, l, r);
        if (l > sr || r < sl) return;
        if (l >= sl && r <= sr){
            lazy[node] ^= 1;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(LL, l, mid, sl, sr);
        update(RR, mid + 1, r, sl, sr);
        t[node] = add(t[LL], t[RR]);
    }


    int query(int node, int l, int r, int sl, int sr){
        push(node, l, r);
        if (l > sr || r < sl) return 0;
        if (l >= sl && r <= sr) return t[node];
        int mid = (l + r) / 2;
        return add(query(LL, l, mid, sl, sr), query(RR, mid + 1, r, sl, sr));
    }


    SegTree(int N, vector<int> a){
        n = N;
        t.resize(4 * n + 5, 0), lazy.resize(4 * n + 5, 0), sum.resize(4 * n + 5, 0);
        arr = a;
        build(1, 0, n - 1);
    }
};

vector<int> adj[MAXN];
vector<int> pre[MAXN], suf[MAXN];
int val[MAXN], sz[MAXN], n, m;

void dfs(int node){
    sz[node] = max((int)1, (int)adj[node].size());
    pre[node].resize(adj[node].size());
    suf[node].resize(adj[node].size());
    for (auto i : adj[node]){
        dfs(i);
        sz[node] = mul(sz[node], sz[i]);
    }

    if (adj[node].empty()) return;
    pre[node][0] = sz[adj[node][0]];
    suf[node][(int)adj[node].size() - 1] = sz[adj[node].back()];

    for (int i = 1; i < adj[node].size(); i++)
        pre[node][i] = mul(pre[node][i - 1], sz[adj[node][i]]);

    for (int i = (int)adj[node].size() - 2; i >= 0; i--)
        suf[node][i] = mul(suf[node][i + 1], sz[adj[node][i]]);

}


void dfs2(int node, int x){
    val[node] = x;
    if (adj[node].empty()) return;
    for (int i = 0; i < adj[node].size(); i++){
        int m = 1;
        if (i > 0) m = mul(m, pre[node][i - 1]);
        if (i + 1 < adj[node].size()) m = mul(m, suf[node][i + 1]);
        dfs2(adj[node][i], mul(x, m));
    }
}


SegTree *T;

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N, m = M;
    for (int i = 1; i < n + m; i++)
        adj[P[i]].pb(i);

    dfs(0);
    dfs2(0, 1);

    vector<int> arr(m);
    for (int i = 0; i < m; i++) arr[i] = val[i + n];
        
    T = new SegTree(m, arr);
    for (int i = 0; i < m; i++) 
        if (A[i]) T->update(1, 0, m - 1, i, i);
}

int count_ways(int L, int R) {
    T->update(1, 0, m - 1, L - n, R - n);
    return T->query(1, 0, m - 1, 0, m - 1);
}
/*

int main() {
    fileio();
    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:116:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |     for (int i = 1; i < adj[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(int, int)':
circuit.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |     for (int i = 0; i < adj[node].size(); i++){
      |                     ~~^~~~~~~~~~~~~~~~~~
circuit.cpp:131:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         if (i + 1 < adj[node].size()) m = mul(m, suf[node][i + 1]);
      |             ~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 5 ms 39380 KB Output is correct
4 Correct 5 ms 39324 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39256 KB Output is correct
7 Correct 5 ms 39256 KB Output is correct
8 Correct 6 ms 39256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 7 ms 39256 KB Output is correct
4 Correct 6 ms 39256 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39512 KB Output is correct
7 Correct 5 ms 39512 KB Output is correct
8 Correct 5 ms 39336 KB Output is correct
9 Correct 6 ms 39512 KB Output is correct
10 Correct 5 ms 39512 KB Output is correct
11 Correct 5 ms 39512 KB Output is correct
12 Correct 5 ms 39512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 5 ms 39380 KB Output is correct
4 Correct 5 ms 39324 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39256 KB Output is correct
7 Correct 5 ms 39256 KB Output is correct
8 Correct 6 ms 39256 KB Output is correct
9 Correct 5 ms 39256 KB Output is correct
10 Correct 5 ms 39256 KB Output is correct
11 Correct 7 ms 39256 KB Output is correct
12 Correct 6 ms 39256 KB Output is correct
13 Correct 5 ms 39256 KB Output is correct
14 Correct 5 ms 39512 KB Output is correct
15 Correct 5 ms 39512 KB Output is correct
16 Correct 5 ms 39336 KB Output is correct
17 Correct 6 ms 39512 KB Output is correct
18 Correct 5 ms 39512 KB Output is correct
19 Correct 5 ms 39512 KB Output is correct
20 Correct 5 ms 39512 KB Output is correct
21 Correct 6 ms 39512 KB Output is correct
22 Correct 5 ms 39512 KB Output is correct
23 Correct 5 ms 39512 KB Output is correct
24 Correct 6 ms 39360 KB Output is correct
25 Correct 5 ms 39512 KB Output is correct
26 Correct 5 ms 39512 KB Output is correct
27 Correct 6 ms 39512 KB Output is correct
28 Correct 5 ms 39512 KB Output is correct
29 Correct 6 ms 39256 KB Output is correct
30 Correct 5 ms 39256 KB Output is correct
31 Correct 5 ms 39512 KB Output is correct
32 Correct 5 ms 39512 KB Output is correct
33 Correct 5 ms 39512 KB Output is correct
34 Correct 5 ms 39512 KB Output is correct
35 Correct 5 ms 39256 KB Output is correct
36 Correct 6 ms 39508 KB Output is correct
37 Correct 6 ms 39512 KB Output is correct
38 Correct 5 ms 39512 KB Output is correct
39 Correct 5 ms 39512 KB Output is correct
40 Correct 5 ms 39512 KB Output is correct
41 Correct 5 ms 39512 KB Output is correct
42 Correct 5 ms 39256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 405 ms 45144 KB Output is correct
2 Correct 563 ms 51032 KB Output is correct
3 Correct 567 ms 51024 KB Output is correct
4 Correct 568 ms 51284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 405 ms 45144 KB Output is correct
2 Correct 563 ms 51032 KB Output is correct
3 Correct 567 ms 51024 KB Output is correct
4 Correct 568 ms 51284 KB Output is correct
5 Correct 466 ms 45144 KB Output is correct
6 Correct 600 ms 51032 KB Output is correct
7 Correct 580 ms 51032 KB Output is correct
8 Correct 548 ms 51032 KB Output is correct
9 Correct 293 ms 39768 KB Output is correct
10 Correct 564 ms 40024 KB Output is correct
11 Correct 576 ms 40024 KB Output is correct
12 Correct 537 ms 40024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 7 ms 39256 KB Output is correct
4 Correct 6 ms 39256 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39512 KB Output is correct
7 Correct 5 ms 39512 KB Output is correct
8 Correct 5 ms 39336 KB Output is correct
9 Correct 6 ms 39512 KB Output is correct
10 Correct 5 ms 39512 KB Output is correct
11 Correct 5 ms 39512 KB Output is correct
12 Correct 5 ms 39512 KB Output is correct
13 Correct 405 ms 45144 KB Output is correct
14 Correct 563 ms 51032 KB Output is correct
15 Correct 567 ms 51024 KB Output is correct
16 Correct 568 ms 51284 KB Output is correct
17 Correct 466 ms 45144 KB Output is correct
18 Correct 600 ms 51032 KB Output is correct
19 Correct 580 ms 51032 KB Output is correct
20 Correct 548 ms 51032 KB Output is correct
21 Correct 293 ms 39768 KB Output is correct
22 Correct 564 ms 40024 KB Output is correct
23 Correct 576 ms 40024 KB Output is correct
24 Correct 537 ms 40024 KB Output is correct
25 Correct 662 ms 56656 KB Output is correct
26 Correct 681 ms 57168 KB Output is correct
27 Correct 671 ms 57168 KB Output is correct
28 Correct 547 ms 57148 KB Output is correct
29 Correct 659 ms 63312 KB Output is correct
30 Correct 618 ms 63312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 5 ms 39380 KB Output is correct
4 Correct 5 ms 39324 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39256 KB Output is correct
7 Correct 5 ms 39256 KB Output is correct
8 Correct 6 ms 39256 KB Output is correct
9 Correct 5 ms 39256 KB Output is correct
10 Correct 5 ms 39256 KB Output is correct
11 Correct 7 ms 39256 KB Output is correct
12 Correct 6 ms 39256 KB Output is correct
13 Correct 5 ms 39256 KB Output is correct
14 Correct 5 ms 39512 KB Output is correct
15 Correct 5 ms 39512 KB Output is correct
16 Correct 5 ms 39336 KB Output is correct
17 Correct 6 ms 39512 KB Output is correct
18 Correct 5 ms 39512 KB Output is correct
19 Correct 5 ms 39512 KB Output is correct
20 Correct 5 ms 39512 KB Output is correct
21 Correct 6 ms 39512 KB Output is correct
22 Correct 5 ms 39512 KB Output is correct
23 Correct 5 ms 39512 KB Output is correct
24 Correct 6 ms 39360 KB Output is correct
25 Correct 5 ms 39512 KB Output is correct
26 Correct 5 ms 39512 KB Output is correct
27 Correct 6 ms 39512 KB Output is correct
28 Correct 5 ms 39512 KB Output is correct
29 Correct 6 ms 39256 KB Output is correct
30 Correct 5 ms 39256 KB Output is correct
31 Correct 5 ms 39512 KB Output is correct
32 Correct 5 ms 39512 KB Output is correct
33 Correct 5 ms 39512 KB Output is correct
34 Correct 5 ms 39512 KB Output is correct
35 Correct 5 ms 39256 KB Output is correct
36 Correct 6 ms 39508 KB Output is correct
37 Correct 6 ms 39512 KB Output is correct
38 Correct 5 ms 39512 KB Output is correct
39 Correct 5 ms 39512 KB Output is correct
40 Correct 5 ms 39512 KB Output is correct
41 Correct 5 ms 39512 KB Output is correct
42 Correct 5 ms 39256 KB Output is correct
43 Correct 399 ms 39768 KB Output is correct
44 Correct 562 ms 40024 KB Output is correct
45 Correct 543 ms 39768 KB Output is correct
46 Correct 519 ms 40280 KB Output is correct
47 Correct 526 ms 40280 KB Output is correct
48 Correct 524 ms 40280 KB Output is correct
49 Correct 591 ms 40280 KB Output is correct
50 Correct 560 ms 40280 KB Output is correct
51 Correct 587 ms 39768 KB Output is correct
52 Correct 555 ms 39768 KB Output is correct
53 Correct 516 ms 40024 KB Output is correct
54 Correct 552 ms 40280 KB Output is correct
55 Correct 552 ms 40024 KB Output is correct
56 Correct 567 ms 39768 KB Output is correct
57 Correct 546 ms 39768 KB Output is correct
58 Correct 580 ms 40536 KB Output is correct
59 Correct 541 ms 40536 KB Output is correct
60 Correct 524 ms 40536 KB Output is correct
61 Correct 532 ms 40024 KB Output is correct
62 Correct 583 ms 39768 KB Output is correct
63 Correct 559 ms 39768 KB Output is correct
64 Correct 598 ms 39768 KB Output is correct
65 Correct 272 ms 39768 KB Output is correct
66 Correct 542 ms 40024 KB Output is correct
67 Correct 515 ms 40124 KB Output is correct
68 Correct 514 ms 40024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 39256 KB Output is correct
2 Correct 5 ms 39256 KB Output is correct
3 Correct 5 ms 39380 KB Output is correct
4 Correct 5 ms 39324 KB Output is correct
5 Correct 5 ms 39256 KB Output is correct
6 Correct 5 ms 39256 KB Output is correct
7 Correct 5 ms 39256 KB Output is correct
8 Correct 6 ms 39256 KB Output is correct
9 Correct 5 ms 39256 KB Output is correct
10 Correct 5 ms 39256 KB Output is correct
11 Correct 7 ms 39256 KB Output is correct
12 Correct 6 ms 39256 KB Output is correct
13 Correct 5 ms 39256 KB Output is correct
14 Correct 5 ms 39512 KB Output is correct
15 Correct 5 ms 39512 KB Output is correct
16 Correct 5 ms 39336 KB Output is correct
17 Correct 6 ms 39512 KB Output is correct
18 Correct 5 ms 39512 KB Output is correct
19 Correct 5 ms 39512 KB Output is correct
20 Correct 5 ms 39512 KB Output is correct
21 Correct 6 ms 39512 KB Output is correct
22 Correct 5 ms 39512 KB Output is correct
23 Correct 5 ms 39512 KB Output is correct
24 Correct 6 ms 39360 KB Output is correct
25 Correct 5 ms 39512 KB Output is correct
26 Correct 5 ms 39512 KB Output is correct
27 Correct 6 ms 39512 KB Output is correct
28 Correct 5 ms 39512 KB Output is correct
29 Correct 6 ms 39256 KB Output is correct
30 Correct 5 ms 39256 KB Output is correct
31 Correct 5 ms 39512 KB Output is correct
32 Correct 5 ms 39512 KB Output is correct
33 Correct 5 ms 39512 KB Output is correct
34 Correct 5 ms 39512 KB Output is correct
35 Correct 5 ms 39256 KB Output is correct
36 Correct 6 ms 39508 KB Output is correct
37 Correct 6 ms 39512 KB Output is correct
38 Correct 5 ms 39512 KB Output is correct
39 Correct 5 ms 39512 KB Output is correct
40 Correct 5 ms 39512 KB Output is correct
41 Correct 5 ms 39512 KB Output is correct
42 Correct 5 ms 39256 KB Output is correct
43 Correct 405 ms 45144 KB Output is correct
44 Correct 563 ms 51032 KB Output is correct
45 Correct 567 ms 51024 KB Output is correct
46 Correct 568 ms 51284 KB Output is correct
47 Correct 466 ms 45144 KB Output is correct
48 Correct 600 ms 51032 KB Output is correct
49 Correct 580 ms 51032 KB Output is correct
50 Correct 548 ms 51032 KB Output is correct
51 Correct 293 ms 39768 KB Output is correct
52 Correct 564 ms 40024 KB Output is correct
53 Correct 576 ms 40024 KB Output is correct
54 Correct 537 ms 40024 KB Output is correct
55 Correct 662 ms 56656 KB Output is correct
56 Correct 681 ms 57168 KB Output is correct
57 Correct 671 ms 57168 KB Output is correct
58 Correct 547 ms 57148 KB Output is correct
59 Correct 659 ms 63312 KB Output is correct
60 Correct 618 ms 63312 KB Output is correct
61 Correct 399 ms 39768 KB Output is correct
62 Correct 562 ms 40024 KB Output is correct
63 Correct 543 ms 39768 KB Output is correct
64 Correct 519 ms 40280 KB Output is correct
65 Correct 526 ms 40280 KB Output is correct
66 Correct 524 ms 40280 KB Output is correct
67 Correct 591 ms 40280 KB Output is correct
68 Correct 560 ms 40280 KB Output is correct
69 Correct 587 ms 39768 KB Output is correct
70 Correct 555 ms 39768 KB Output is correct
71 Correct 516 ms 40024 KB Output is correct
72 Correct 552 ms 40280 KB Output is correct
73 Correct 552 ms 40024 KB Output is correct
74 Correct 567 ms 39768 KB Output is correct
75 Correct 546 ms 39768 KB Output is correct
76 Correct 580 ms 40536 KB Output is correct
77 Correct 541 ms 40536 KB Output is correct
78 Correct 524 ms 40536 KB Output is correct
79 Correct 532 ms 40024 KB Output is correct
80 Correct 583 ms 39768 KB Output is correct
81 Correct 559 ms 39768 KB Output is correct
82 Correct 598 ms 39768 KB Output is correct
83 Correct 272 ms 39768 KB Output is correct
84 Correct 542 ms 40024 KB Output is correct
85 Correct 515 ms 40124 KB Output is correct
86 Correct 514 ms 40024 KB Output is correct
87 Correct 5 ms 39256 KB Output is correct
88 Correct 390 ms 55260 KB Output is correct
89 Correct 637 ms 51024 KB Output is correct
90 Correct 579 ms 50512 KB Output is correct
91 Correct 622 ms 57092 KB Output is correct
92 Correct 632 ms 57168 KB Output is correct
93 Correct 636 ms 57168 KB Output is correct
94 Correct 697 ms 57164 KB Output is correct
95 Correct 675 ms 57224 KB Output is correct
96 Correct 642 ms 48336 KB Output is correct
97 Correct 625 ms 48336 KB Output is correct
98 Correct 522 ms 55896 KB Output is correct
99 Correct 626 ms 57168 KB Output is correct
100 Correct 650 ms 52052 KB Output is correct
101 Correct 607 ms 49744 KB Output is correct
102 Correct 612 ms 48216 KB Output is correct
103 Correct 646 ms 63316 KB Output is correct
104 Correct 650 ms 64464 KB Output is correct
105 Correct 633 ms 64468 KB Output is correct
106 Correct 611 ms 51800 KB Output is correct
107 Correct 608 ms 48728 KB Output is correct
108 Correct 591 ms 48464 KB Output is correct
109 Correct 610 ms 48208 KB Output is correct