답안 #936581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936581 2024-03-02T09:10:49 Z GrindMachine 디지털 회로 (IOI22_circuit) C++17
100 / 100
873 ms 49340 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

*/

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 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 3 ms 6744 KB Output is correct
4 Correct 4 ms 6744 KB Output is correct
5 Correct 3 ms 6744 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 4 ms 7000 KB Output is correct
4 Correct 2 ms 6744 KB Output is correct
5 Correct 3 ms 6784 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 2 ms 6740 KB Output is correct
9 Correct 4 ms 6744 KB Output is correct
10 Correct 3 ms 7000 KB Output is correct
11 Correct 3 ms 7000 KB Output is correct
12 Correct 2 ms 6744 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 6744 KB Output is correct
4 Correct 4 ms 6744 KB Output is correct
5 Correct 3 ms 6744 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 4 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 3 ms 6784 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 2 ms 6744 KB Output is correct
16 Correct 2 ms 6740 KB Output is correct
17 Correct 4 ms 6744 KB Output is correct
18 Correct 3 ms 7000 KB Output is correct
19 Correct 3 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 6744 KB Output is correct
23 Correct 3 ms 6744 KB Output is correct
24 Correct 2 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 3 ms 6744 KB Output is correct
27 Correct 2 ms 6744 KB Output is correct
28 Correct 3 ms 6744 KB Output is correct
29 Correct 3 ms 6744 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 3 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 3 ms 6744 KB Output is correct
36 Correct 3 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 6744 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 3 ms 6704 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 455 ms 11064 KB Output is correct
2 Correct 662 ms 15288 KB Output is correct
3 Correct 628 ms 15304 KB Output is correct
4 Correct 602 ms 15276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 455 ms 11064 KB Output is correct
2 Correct 662 ms 15288 KB Output is correct
3 Correct 628 ms 15304 KB Output is correct
4 Correct 602 ms 15276 KB Output is correct
5 Correct 484 ms 11060 KB Output is correct
6 Correct 632 ms 15320 KB Output is correct
7 Correct 633 ms 15308 KB Output is correct
8 Correct 558 ms 15272 KB Output is correct
9 Correct 267 ms 7000 KB Output is correct
10 Correct 531 ms 7284 KB Output is correct
11 Correct 579 ms 7288 KB Output is correct
12 Correct 506 ms 7292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 4 ms 7000 KB Output is correct
4 Correct 2 ms 6744 KB Output is correct
5 Correct 3 ms 6784 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 2 ms 6740 KB Output is correct
9 Correct 4 ms 6744 KB Output is correct
10 Correct 3 ms 7000 KB Output is correct
11 Correct 3 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 455 ms 11064 KB Output is correct
14 Correct 662 ms 15288 KB Output is correct
15 Correct 628 ms 15304 KB Output is correct
16 Correct 602 ms 15276 KB Output is correct
17 Correct 484 ms 11060 KB Output is correct
18 Correct 632 ms 15320 KB Output is correct
19 Correct 633 ms 15308 KB Output is correct
20 Correct 558 ms 15272 KB Output is correct
21 Correct 267 ms 7000 KB Output is correct
22 Correct 531 ms 7284 KB Output is correct
23 Correct 579 ms 7288 KB Output is correct
24 Correct 506 ms 7292 KB Output is correct
25 Correct 650 ms 21000 KB Output is correct
26 Correct 771 ms 21156 KB Output is correct
27 Correct 738 ms 21032 KB Output is correct
28 Correct 525 ms 21056 KB Output is correct
29 Correct 663 ms 47544 KB Output is correct
30 Correct 579 ms 47688 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 6744 KB Output is correct
4 Correct 4 ms 6744 KB Output is correct
5 Correct 3 ms 6744 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 4 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 3 ms 6784 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 2 ms 6744 KB Output is correct
16 Correct 2 ms 6740 KB Output is correct
17 Correct 4 ms 6744 KB Output is correct
18 Correct 3 ms 7000 KB Output is correct
19 Correct 3 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 6744 KB Output is correct
23 Correct 3 ms 6744 KB Output is correct
24 Correct 2 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 3 ms 6744 KB Output is correct
27 Correct 2 ms 6744 KB Output is correct
28 Correct 3 ms 6744 KB Output is correct
29 Correct 3 ms 6744 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 3 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 3 ms 6744 KB Output is correct
36 Correct 3 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 6744 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 3 ms 6704 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
43 Correct 381 ms 7200 KB Output is correct
44 Correct 547 ms 7232 KB Output is correct
45 Correct 592 ms 7232 KB Output is correct
46 Correct 511 ms 7580 KB Output is correct
47 Correct 525 ms 7580 KB Output is correct
48 Correct 582 ms 7580 KB Output is correct
49 Correct 541 ms 7576 KB Output is correct
50 Correct 573 ms 7584 KB Output is correct
51 Correct 512 ms 7256 KB Output is correct
52 Correct 498 ms 7256 KB Output is correct
53 Correct 472 ms 8536 KB Output is correct
54 Correct 556 ms 7680 KB Output is correct
55 Correct 599 ms 7256 KB Output is correct
56 Correct 516 ms 7256 KB Output is correct
57 Correct 544 ms 7256 KB Output is correct
58 Correct 529 ms 8536 KB Output is correct
59 Correct 520 ms 8792 KB Output is correct
60 Correct 533 ms 8792 KB Output is correct
61 Correct 597 ms 7512 KB Output is correct
62 Correct 555 ms 7212 KB Output is correct
63 Correct 532 ms 7216 KB Output is correct
64 Correct 524 ms 7256 KB Output is correct
65 Correct 267 ms 7000 KB Output is correct
66 Correct 541 ms 7288 KB Output is correct
67 Correct 528 ms 7288 KB Output is correct
68 Correct 496 ms 7288 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 6744 KB Output is correct
4 Correct 4 ms 6744 KB Output is correct
5 Correct 3 ms 6744 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 3 ms 6744 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 4 ms 7000 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 3 ms 6784 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 2 ms 6744 KB Output is correct
16 Correct 2 ms 6740 KB Output is correct
17 Correct 4 ms 6744 KB Output is correct
18 Correct 3 ms 7000 KB Output is correct
19 Correct 3 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 6744 KB Output is correct
23 Correct 3 ms 6744 KB Output is correct
24 Correct 2 ms 6744 KB Output is correct
25 Correct 3 ms 6744 KB Output is correct
26 Correct 3 ms 6744 KB Output is correct
27 Correct 2 ms 6744 KB Output is correct
28 Correct 3 ms 6744 KB Output is correct
29 Correct 3 ms 6744 KB Output is correct
30 Correct 3 ms 6744 KB Output is correct
31 Correct 3 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 3 ms 6744 KB Output is correct
36 Correct 3 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 6744 KB Output is correct
40 Correct 2 ms 6744 KB Output is correct
41 Correct 3 ms 6704 KB Output is correct
42 Correct 2 ms 6744 KB Output is correct
43 Correct 455 ms 11064 KB Output is correct
44 Correct 662 ms 15288 KB Output is correct
45 Correct 628 ms 15304 KB Output is correct
46 Correct 602 ms 15276 KB Output is correct
47 Correct 484 ms 11060 KB Output is correct
48 Correct 632 ms 15320 KB Output is correct
49 Correct 633 ms 15308 KB Output is correct
50 Correct 558 ms 15272 KB Output is correct
51 Correct 267 ms 7000 KB Output is correct
52 Correct 531 ms 7284 KB Output is correct
53 Correct 579 ms 7288 KB Output is correct
54 Correct 506 ms 7292 KB Output is correct
55 Correct 650 ms 21000 KB Output is correct
56 Correct 771 ms 21156 KB Output is correct
57 Correct 738 ms 21032 KB Output is correct
58 Correct 525 ms 21056 KB Output is correct
59 Correct 663 ms 47544 KB Output is correct
60 Correct 579 ms 47688 KB Output is correct
61 Correct 381 ms 7200 KB Output is correct
62 Correct 547 ms 7232 KB Output is correct
63 Correct 592 ms 7232 KB Output is correct
64 Correct 511 ms 7580 KB Output is correct
65 Correct 525 ms 7580 KB Output is correct
66 Correct 582 ms 7580 KB Output is correct
67 Correct 541 ms 7576 KB Output is correct
68 Correct 573 ms 7584 KB Output is correct
69 Correct 512 ms 7256 KB Output is correct
70 Correct 498 ms 7256 KB Output is correct
71 Correct 472 ms 8536 KB Output is correct
72 Correct 556 ms 7680 KB Output is correct
73 Correct 599 ms 7256 KB Output is correct
74 Correct 516 ms 7256 KB Output is correct
75 Correct 544 ms 7256 KB Output is correct
76 Correct 529 ms 8536 KB Output is correct
77 Correct 520 ms 8792 KB Output is correct
78 Correct 533 ms 8792 KB Output is correct
79 Correct 597 ms 7512 KB Output is correct
80 Correct 555 ms 7212 KB Output is correct
81 Correct 532 ms 7216 KB Output is correct
82 Correct 524 ms 7256 KB Output is correct
83 Correct 267 ms 7000 KB Output is correct
84 Correct 541 ms 7288 KB Output is correct
85 Correct 528 ms 7288 KB Output is correct
86 Correct 496 ms 7288 KB Output is correct
87 Correct 2 ms 6488 KB Output is correct
88 Correct 417 ms 20620 KB Output is correct
89 Correct 648 ms 15084 KB Output is correct
90 Correct 659 ms 15196 KB Output is correct
91 Correct 674 ms 21660 KB Output is correct
92 Correct 816 ms 21844 KB Output is correct
93 Correct 834 ms 21800 KB Output is correct
94 Correct 873 ms 21624 KB Output is correct
95 Correct 733 ms 21616 KB Output is correct
96 Correct 634 ms 17908 KB Output is correct
97 Correct 679 ms 17912 KB Output is correct
98 Correct 579 ms 43444 KB Output is correct
99 Correct 799 ms 21152 KB Output is correct
100 Correct 800 ms 20120 KB Output is correct
101 Correct 780 ms 19456 KB Output is correct
102 Correct 761 ms 18116 KB Output is correct
103 Correct 823 ms 47808 KB Output is correct
104 Correct 808 ms 49200 KB Output is correct
105 Correct 706 ms 49340 KB Output is correct
106 Correct 766 ms 20604 KB Output is correct
107 Correct 783 ms 17740 KB Output is correct
108 Correct 728 ms 18280 KB Output is correct
109 Correct 743 ms 18356 KB Output is correct