답안 #936583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936583 2024-03-02T09:11:57 Z GrindMachine 디지털 회로 (IOI22_circuit) C++17
100 / 100
824 ms 49720 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi (took hints from the solution to the two inputs subtask)

*/

const int MOD = 1000002022;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "circuit.h"

template<typename T>
struct lazysegtree {
    /*=======================================================*/

    struct data {
        array<ll,2> a;
    };

    struct lazy {
        ll a;
    };

    data d_neutral = {{0,0}};
    lazy l_neutral = {0};

    void merge(data &curr, data &left, data &right) {
        rep(j,2){
            curr.a[j] = (left.a[j]+right.a[j])%MOD;
        }
    }

    void create(int x, int lx, int rx, T v) {
        tr[x].a[v.ff] = v.ss;
    }

    void modify(int x, int lx, int rx, T v) {
        lz[x].a = 1;
    }

    void propagate(int x, int lx, int rx) {
        ll v = lz[x].a;
        if(!v) return;

        swap(tr[x].a[0],tr[x].a[1]);

        if(rx-lx > 1){
            lz[2*x+1].a ^= 1;
            lz[2*x+2].a ^= 1;
        }

        lz[x] = l_neutral;
    }

    /*=======================================================*/

    int siz = 1;
    vector<data> tr;
    vector<lazy> lz;

    lazysegtree() {

    }

    lazysegtree(int n) {
        while (siz < n) siz *= 2;
        tr.assign(2 * siz, d_neutral);
        lz.assign(2 * siz, l_neutral);
    }

    void build(vector<T> &a, int n, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                create(x, lx, rx, a[lx]);
            }

            return;
        }

        int mid = (lx + rx) / 2;

        build(a, n, 2 * x + 1, lx, mid);
        build(a, n, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void build(vector<T> &a, int n) {
        build(a, n, 0, 0, siz);
    }

    void rupd(int l, int r, T v, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return;
        if (lx >= l and rx <= r) {
            modify(x, lx, rx, v);
            propagate(x, lx, rx);
            return;
        }

        int mid = (lx + rx) / 2;

        rupd(l, r, v, 2 * x + 1, lx, mid);
        rupd(l, r, v, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void rupd(int l, int r, T v) {
        rupd(l, r + 1, v, 0, 0, siz);
    }

    data query(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return d_neutral;
        if (lx >= l and rx <= r) return tr[x];

        int mid = (lx + rx) / 2;

        data curr;
        data left = query(l, r, 2 * x + 1, lx, mid);
        data right = query(l, r, 2 * x + 2, mid, rx);

        merge(curr, left, right);
        return curr;
    }

    data query(int l, int r) {
        return query(l, r + 1, 0, 0, siz);
    }
};

vector<ll> adj[N];
vector<ll> sub_ways(N);

void dfs1(ll u){
    sub_ways[u] = max((ll)sz(adj[u]),1ll);
    trav(v,adj[u]){
        dfs1(v);
        sub_ways[u] = sub_ways[u]*sub_ways[v]%MOD;
    }
}

ll n,m;
vector<int> a;
vector<ll> contrib;

void dfs2(ll u, ll val){
    auto children = adj[u];
    if(children.empty()){
        contrib[u-n] = val;
        return;
    }

    ll siz = sz(children);
    children.insert(children.begin(),0);
    vector<ll> pref(siz+5), suff(siz+5);
    pref[0] = 1, suff[siz+1] = 1;
    rep1(i,siz){
        ll v = children[i];
        pref[i] = pref[i-1]*sub_ways[v]%MOD;
    }
    rev(i,siz,1){
        ll v = children[i];
        suff[i] = suff[i+1]*sub_ways[v]%MOD;
    }

    rep1(i,siz){
        ll v = children[i];
        ll new_val = val*pref[i-1]%MOD*suff[i+1]%MOD;
        dfs2(v,new_val);
    }
}

lazysegtree<pll> st;

void init(int n_, int m_, std::vector<int> P, std::vector<int> A) {
    n = n_, m = m_;
    a = A;
    contrib = vector<ll>(m);

    rep(i,n+m){
        if(P[i] != -1){
            adj[P[i]].pb(i);
        }
    }

    dfs1(0);
    dfs2(0,1);

    vector<pll> b;
    rep(i,m) b.pb({a[i],contrib[i]});

    st = lazysegtree<pll>(m);
    st.build(b,m);
}

int count_ways(int l, int r) {
    l -= n, r -= n;
    st.rupd(l,r,{1,1});
    ll ans = st.query(0,m-1).a[1];
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 3 ms 6488 KB Output is correct
3 Correct 3 ms 6716 KB Output is correct
4 Correct 3 ms 6744 KB Output is correct
5 Correct 2 ms 6740 KB Output is correct
6 Correct 3 ms 6744 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6740 KB Output is correct
3 Correct 2 ms 6740 KB Output is correct
4 Correct 3 ms 6784 KB Output is correct
5 Correct 2 ms 6744 KB Output is correct
6 Correct 2 ms 6740 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 2 ms 6744 KB Output is correct
10 Correct 2 ms 7000 KB Output is correct
11 Correct 2 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 3 ms 6488 KB Output is correct
3 Correct 3 ms 6716 KB Output is correct
4 Correct 3 ms 6744 KB Output is correct
5 Correct 2 ms 6740 KB Output is correct
6 Correct 3 ms 6744 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6740 KB Output is correct
11 Correct 2 ms 6740 KB Output is correct
12 Correct 3 ms 6784 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6740 KB Output is correct
15 Correct 3 ms 6744 KB Output is correct
16 Correct 2 ms 6744 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 7000 KB Output is correct
19 Correct 2 ms 7000 KB Output is correct
20 Correct 2 ms 6744 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 3 ms 7000 KB Output is correct
23 Correct 2 ms 6900 KB Output is correct
24 Correct 3 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 2 ms 6744 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6744 KB Output is correct
29 Correct 3 ms 6740 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 2 ms 7000 KB Output is correct
32 Correct 2 ms 6744 KB Output is correct
33 Correct 2 ms 6744 KB Output is correct
34 Correct 2 ms 6744 KB Output is correct
35 Correct 2 ms 6744 KB Output is correct
36 Correct 2 ms 7000 KB Output is correct
37 Correct 3 ms 7000 KB Output is correct
38 Correct 3 ms 7000 KB Output is correct
39 Correct 2 ms 6768 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 2 ms 6844 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 411 ms 11060 KB Output is correct
2 Correct 651 ms 15280 KB Output is correct
3 Correct 673 ms 15284 KB Output is correct
4 Correct 715 ms 15280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 411 ms 11060 KB Output is correct
2 Correct 651 ms 15280 KB Output is correct
3 Correct 673 ms 15284 KB Output is correct
4 Correct 715 ms 15280 KB Output is correct
5 Correct 571 ms 11064 KB Output is correct
6 Correct 715 ms 15276 KB Output is correct
7 Correct 701 ms 15280 KB Output is correct
8 Correct 626 ms 15268 KB Output is correct
9 Correct 291 ms 7000 KB Output is correct
10 Correct 623 ms 7292 KB Output is correct
11 Correct 653 ms 7288 KB Output is correct
12 Correct 596 ms 7284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6740 KB Output is correct
3 Correct 2 ms 6740 KB Output is correct
4 Correct 3 ms 6784 KB Output is correct
5 Correct 2 ms 6744 KB Output is correct
6 Correct 2 ms 6740 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 2 ms 6744 KB Output is correct
10 Correct 2 ms 7000 KB Output is correct
11 Correct 2 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 411 ms 11060 KB Output is correct
14 Correct 651 ms 15280 KB Output is correct
15 Correct 673 ms 15284 KB Output is correct
16 Correct 715 ms 15280 KB Output is correct
17 Correct 571 ms 11064 KB Output is correct
18 Correct 715 ms 15276 KB Output is correct
19 Correct 701 ms 15280 KB Output is correct
20 Correct 626 ms 15268 KB Output is correct
21 Correct 291 ms 7000 KB Output is correct
22 Correct 623 ms 7292 KB Output is correct
23 Correct 653 ms 7288 KB Output is correct
24 Correct 596 ms 7284 KB Output is correct
25 Correct 787 ms 21028 KB Output is correct
26 Correct 813 ms 21032 KB Output is correct
27 Correct 824 ms 21048 KB Output is correct
28 Correct 621 ms 21048 KB Output is correct
29 Correct 802 ms 47748 KB Output is correct
30 Correct 717 ms 47560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 3 ms 6488 KB Output is correct
3 Correct 3 ms 6716 KB Output is correct
4 Correct 3 ms 6744 KB Output is correct
5 Correct 2 ms 6740 KB Output is correct
6 Correct 3 ms 6744 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6740 KB Output is correct
11 Correct 2 ms 6740 KB Output is correct
12 Correct 3 ms 6784 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6740 KB Output is correct
15 Correct 3 ms 6744 KB Output is correct
16 Correct 2 ms 6744 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 7000 KB Output is correct
19 Correct 2 ms 7000 KB Output is correct
20 Correct 2 ms 6744 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 3 ms 7000 KB Output is correct
23 Correct 2 ms 6900 KB Output is correct
24 Correct 3 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 2 ms 6744 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6744 KB Output is correct
29 Correct 3 ms 6740 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 2 ms 7000 KB Output is correct
32 Correct 2 ms 6744 KB Output is correct
33 Correct 2 ms 6744 KB Output is correct
34 Correct 2 ms 6744 KB Output is correct
35 Correct 2 ms 6744 KB Output is correct
36 Correct 2 ms 7000 KB Output is correct
37 Correct 3 ms 7000 KB Output is correct
38 Correct 3 ms 7000 KB Output is correct
39 Correct 2 ms 6768 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 2 ms 6844 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
43 Correct 459 ms 7248 KB Output is correct
44 Correct 644 ms 7224 KB Output is correct
45 Correct 613 ms 7252 KB Output is correct
46 Correct 602 ms 7572 KB Output is correct
47 Correct 593 ms 7576 KB Output is correct
48 Correct 615 ms 7580 KB Output is correct
49 Correct 543 ms 7580 KB Output is correct
50 Correct 537 ms 7576 KB Output is correct
51 Correct 562 ms 7256 KB Output is correct
52 Correct 528 ms 7256 KB Output is correct
53 Correct 529 ms 8536 KB Output is correct
54 Correct 630 ms 7552 KB Output is correct
55 Correct 551 ms 7256 KB Output is correct
56 Correct 551 ms 7256 KB Output is correct
57 Correct 562 ms 7256 KB Output is correct
58 Correct 520 ms 8536 KB Output is correct
59 Correct 551 ms 8792 KB Output is correct
60 Correct 544 ms 8792 KB Output is correct
61 Correct 576 ms 7512 KB Output is correct
62 Correct 547 ms 7204 KB Output is correct
63 Correct 542 ms 7248 KB Output is correct
64 Correct 564 ms 7256 KB Output is correct
65 Correct 255 ms 7000 KB Output is correct
66 Correct 516 ms 7288 KB Output is correct
67 Correct 588 ms 7288 KB Output is correct
68 Correct 476 ms 7288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 3 ms 6488 KB Output is correct
3 Correct 3 ms 6716 KB Output is correct
4 Correct 3 ms 6744 KB Output is correct
5 Correct 2 ms 6740 KB Output is correct
6 Correct 3 ms 6744 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6740 KB Output is correct
11 Correct 2 ms 6740 KB Output is correct
12 Correct 3 ms 6784 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6740 KB Output is correct
15 Correct 3 ms 6744 KB Output is correct
16 Correct 2 ms 6744 KB Output is correct
17 Correct 2 ms 6744 KB Output is correct
18 Correct 2 ms 7000 KB Output is correct
19 Correct 2 ms 7000 KB Output is correct
20 Correct 2 ms 6744 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 3 ms 7000 KB Output is correct
23 Correct 2 ms 6900 KB Output is correct
24 Correct 3 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 2 ms 6744 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6744 KB Output is correct
29 Correct 3 ms 6740 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 2 ms 7000 KB Output is correct
32 Correct 2 ms 6744 KB Output is correct
33 Correct 2 ms 6744 KB Output is correct
34 Correct 2 ms 6744 KB Output is correct
35 Correct 2 ms 6744 KB Output is correct
36 Correct 2 ms 7000 KB Output is correct
37 Correct 3 ms 7000 KB Output is correct
38 Correct 3 ms 7000 KB Output is correct
39 Correct 2 ms 6768 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 2 ms 6844 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
43 Correct 411 ms 11060 KB Output is correct
44 Correct 651 ms 15280 KB Output is correct
45 Correct 673 ms 15284 KB Output is correct
46 Correct 715 ms 15280 KB Output is correct
47 Correct 571 ms 11064 KB Output is correct
48 Correct 715 ms 15276 KB Output is correct
49 Correct 701 ms 15280 KB Output is correct
50 Correct 626 ms 15268 KB Output is correct
51 Correct 291 ms 7000 KB Output is correct
52 Correct 623 ms 7292 KB Output is correct
53 Correct 653 ms 7288 KB Output is correct
54 Correct 596 ms 7284 KB Output is correct
55 Correct 787 ms 21028 KB Output is correct
56 Correct 813 ms 21032 KB Output is correct
57 Correct 824 ms 21048 KB Output is correct
58 Correct 621 ms 21048 KB Output is correct
59 Correct 802 ms 47748 KB Output is correct
60 Correct 717 ms 47560 KB Output is correct
61 Correct 459 ms 7248 KB Output is correct
62 Correct 644 ms 7224 KB Output is correct
63 Correct 613 ms 7252 KB Output is correct
64 Correct 602 ms 7572 KB Output is correct
65 Correct 593 ms 7576 KB Output is correct
66 Correct 615 ms 7580 KB Output is correct
67 Correct 543 ms 7580 KB Output is correct
68 Correct 537 ms 7576 KB Output is correct
69 Correct 562 ms 7256 KB Output is correct
70 Correct 528 ms 7256 KB Output is correct
71 Correct 529 ms 8536 KB Output is correct
72 Correct 630 ms 7552 KB Output is correct
73 Correct 551 ms 7256 KB Output is correct
74 Correct 551 ms 7256 KB Output is correct
75 Correct 562 ms 7256 KB Output is correct
76 Correct 520 ms 8536 KB Output is correct
77 Correct 551 ms 8792 KB Output is correct
78 Correct 544 ms 8792 KB Output is correct
79 Correct 576 ms 7512 KB Output is correct
80 Correct 547 ms 7204 KB Output is correct
81 Correct 542 ms 7248 KB Output is correct
82 Correct 564 ms 7256 KB Output is correct
83 Correct 255 ms 7000 KB Output is correct
84 Correct 516 ms 7288 KB Output is correct
85 Correct 588 ms 7288 KB Output is correct
86 Correct 476 ms 7288 KB Output is correct
87 Correct 3 ms 6648 KB Output is correct
88 Correct 394 ms 20628 KB Output is correct
89 Correct 667 ms 15052 KB Output is correct
90 Correct 603 ms 15212 KB Output is correct
91 Correct 606 ms 21820 KB Output is correct
92 Correct 644 ms 21772 KB Output is correct
93 Correct 711 ms 21804 KB Output is correct
94 Correct 691 ms 21796 KB Output is correct
95 Correct 613 ms 21796 KB Output is correct
96 Correct 577 ms 17940 KB Output is correct
97 Correct 650 ms 17940 KB Output is correct
98 Correct 530 ms 43348 KB Output is correct
99 Correct 710 ms 21188 KB Output is correct
100 Correct 662 ms 20188 KB Output is correct
101 Correct 741 ms 19468 KB Output is correct
102 Correct 609 ms 18120 KB Output is correct
103 Correct 674 ms 47688 KB Output is correct
104 Correct 721 ms 49184 KB Output is correct
105 Correct 642 ms 49720 KB Output is correct
106 Correct 660 ms 20388 KB Output is correct
107 Correct 610 ms 17736 KB Output is correct
108 Correct 613 ms 18188 KB Output is correct
109 Correct 648 ms 18352 KB Output is correct