Submission #1057817

# Submission time Handle Problem Language Result Execution time Memory
1057817 2024-08-14T06:26:01 Z hirayuu_oj Digital Circuit (IOI22_circuit) C++17
100 / 100
747 ms 20432 KB
#include "circuit.h"

#include <vector>

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rng(i,l,r) for(int i=(l); i<(r); i++)
#define all(x) x.begin(),x.end()
using ll=long long;

constexpr ll MOD=1'000'002'022;

template<typename T,auto op,auto e,typename F,auto mapp,auto comp,auto id>
struct LST {
    vector<T> value;
    vector<F> delay;
    int size;
    LST(int sz) {
        value.resize(sz*2,e());
        delay.resize(sz*2,id());
        size=sz;
    }
    inline T _get(const int pos) {
        return mapp(delay[pos],value[pos]);
    }
    void _delay(const int pos) {
        int k=0;
        int p=pos;
        while(p>1) {
            k++;
            p/=2;
        }
        for(int i=k; i>=1; i--) {
            p=pos>>i;
            value[p]=_get(p);
            delay[p*2]=comp(delay[p],delay[p*2]);
            delay[p*2+1]=comp(delay[p],delay[p*2+1]);
            delay[p]=id();
        }
    }
    void _calc(int pos) {
        pos/=2;
        while(pos>=1) {
            value[pos]=op(_get(pos*2),_get(pos*2+1));
            pos/=2;
        }
    }
    T get(const int pos) {
        return _get(pos+size);
    }
    void set(const int pos,const T val) {
        _delay(pos+size);
        value[pos+size]=val;
        delay[pos+size]=id();
        _calc(pos+size);
    }
    T prod(int lf,int ri) {
        lf+=size;
        ri+=size;
        _delay(lf);
        _delay(ri-1);
        T ret_lf=e(),ret_ri=e();
        while(lf<ri) {
            if(lf&1) {
                ret_lf=op(ret_lf,_get(lf));
                lf++;
            }
            if(ri&1) {
                ri--;
                ret_ri=op(_get(ri),ret_ri);
            }
            lf/=2;
            ri/=2;
        }
        return op(ret_lf,ret_ri);
    }
    void apply(int lf,int ri,F f) {
        lf+=size;
        ri+=size;
        const int update_lf=lf;
        const int update_ri=ri-1;
        _delay(update_lf);
        _delay(update_ri);
        while(lf<ri) {
            if(lf&1) {
                delay[lf]=comp(f,delay[lf]);
                lf++;
            }
            if(ri&1) {
                ri--;
                delay[ri]=comp(f,delay[ri]);
            }
            lf/=2;
            ri/=2;
        }
        _calc(update_lf);
        _calc(update_ri);
    }
};

template<typename T,auto op,auto e>
struct SegTree {
    vector<T> value;
    int size;
    SegTree(int sz) {
        value.resize(sz*2,e());
        size=sz;
    }
    inline T _get(const int pos) {
        return value[pos];
    }
    void _calc(int pos) {
        pos/=2;
        while(pos>=1) {
            value[pos]=op(_get(pos*2),_get(pos*2+1));
            pos/=2;
        }
    }
    T get(const int pos) {
        return _get(pos+size);
    }
    void set(const int pos,const T val) {
        value[pos+size]=val;
        _calc(pos+size);
    }
    T prod(int lf,int ri) {
        lf+=size;
        ri+=size;
        T ret_lf=e(),ret_ri=e();
        while(lf<ri) {
            if(lf&1) {
                ret_lf=op(ret_lf,_get(lf));
                lf++;
            }
            if(ri&1) {
                ri--;
                ret_ri=op(_get(ri),ret_ri);
            }
            lf/=2;
            ri/=2;
        }
        return op(ret_lf,ret_ri);
    }
};

ll prod_op(const ll x,const ll y) {
    return (x*y)%MOD;
}
ll prod_e() {
    return 1;
}

pair<ll,ll> op(const pair<ll,ll> x,const pair<ll,ll> y) {
    return {x.first+y.first,x.second+y.second};
}
pair<ll,ll> e() {
    return {0,0};
}
pair<ll,ll> mapp(const bool f,const pair<ll,ll> x) {
    if(f)return {x.second-x.first,x.second};
    return x;
}
bool comp(const bool f,const bool g) {
    return f!=g;
}
bool id() {
    return false;
}
SegTree<ll,prod_op,prod_e> init_seg(0);
LST<pair<ll,ll>,op,e,bool,mapp,comp,id> seg(0);
vector<vector<int>> tree;
int n,m;
void dfs(int pos) {
    if(n<=pos) {
        seg.set(pos-n,{0,init_seg.prod(0,n)});
    }
    else {
        init_seg.set(pos,1);
        for(int i:tree[pos]) {
            dfs(i);
        }
        init_seg.set(pos,tree[pos].size());
    }
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n=N;
    m=M;
    tree.resize(N+M);
    init_seg=SegTree<ll,prod_op,prod_e>(N);
    seg=LST<pair<ll,ll>,op,e,bool,mapp,comp,id>(M);
    rng(i,1,N+M) {
        tree[P[i]].emplace_back(i);
    }
    rep(i,N) {
        init_seg.set(i,tree[i].size());
    }
    dfs(0);
    rep(i,M) {
        seg.apply(i,i+1,A[i]);
    }
}

int count_ways(int L, int R) {
    seg.apply(L-n,R+1-n,true);
    ll ret=seg.prod(0,m).first%MOD;
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 428 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 428 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 0 ms 432 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 411 ms 5208 KB Output is correct
2 Correct 657 ms 10064 KB Output is correct
3 Correct 617 ms 10072 KB Output is correct
4 Correct 637 ms 10196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 411 ms 5208 KB Output is correct
2 Correct 657 ms 10064 KB Output is correct
3 Correct 617 ms 10072 KB Output is correct
4 Correct 637 ms 10196 KB Output is correct
5 Correct 510 ms 5208 KB Output is correct
6 Correct 603 ms 10016 KB Output is correct
7 Correct 657 ms 10176 KB Output is correct
8 Correct 628 ms 10204 KB Output is correct
9 Correct 265 ms 712 KB Output is correct
10 Correct 548 ms 1028 KB Output is correct
11 Correct 593 ms 1036 KB Output is correct
12 Correct 577 ms 1032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 428 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 411 ms 5208 KB Output is correct
14 Correct 657 ms 10064 KB Output is correct
15 Correct 617 ms 10072 KB Output is correct
16 Correct 637 ms 10196 KB Output is correct
17 Correct 510 ms 5208 KB Output is correct
18 Correct 603 ms 10016 KB Output is correct
19 Correct 657 ms 10176 KB Output is correct
20 Correct 628 ms 10204 KB Output is correct
21 Correct 265 ms 712 KB Output is correct
22 Correct 548 ms 1028 KB Output is correct
23 Correct 593 ms 1036 KB Output is correct
24 Correct 577 ms 1032 KB Output is correct
25 Correct 701 ms 14996 KB Output is correct
26 Correct 709 ms 15184 KB Output is correct
27 Correct 715 ms 15192 KB Output is correct
28 Correct 572 ms 15184 KB Output is correct
29 Correct 716 ms 19792 KB Output is correct
30 Correct 708 ms 19780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 428 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 0 ms 432 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 417 ms 856 KB Output is correct
44 Correct 567 ms 856 KB Output is correct
45 Correct 538 ms 856 KB Output is correct
46 Correct 551 ms 1176 KB Output is correct
47 Correct 588 ms 1176 KB Output is correct
48 Correct 545 ms 1180 KB Output is correct
49 Correct 530 ms 1176 KB Output is correct
50 Correct 556 ms 1172 KB Output is correct
51 Correct 516 ms 600 KB Output is correct
52 Correct 561 ms 856 KB Output is correct
53 Correct 507 ms 1028 KB Output is correct
54 Correct 579 ms 1164 KB Output is correct
55 Correct 606 ms 972 KB Output is correct
56 Correct 610 ms 920 KB Output is correct
57 Correct 574 ms 600 KB Output is correct
58 Correct 591 ms 1400 KB Output is correct
59 Correct 560 ms 1456 KB Output is correct
60 Correct 576 ms 1460 KB Output is correct
61 Correct 593 ms 856 KB Output is correct
62 Correct 596 ms 600 KB Output is correct
63 Correct 582 ms 600 KB Output is correct
64 Correct 559 ms 600 KB Output is correct
65 Correct 267 ms 708 KB Output is correct
66 Correct 536 ms 1032 KB Output is correct
67 Correct 559 ms 1040 KB Output is correct
68 Correct 589 ms 1028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 428 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 0 ms 432 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 411 ms 5208 KB Output is correct
44 Correct 657 ms 10064 KB Output is correct
45 Correct 617 ms 10072 KB Output is correct
46 Correct 637 ms 10196 KB Output is correct
47 Correct 510 ms 5208 KB Output is correct
48 Correct 603 ms 10016 KB Output is correct
49 Correct 657 ms 10176 KB Output is correct
50 Correct 628 ms 10204 KB Output is correct
51 Correct 265 ms 712 KB Output is correct
52 Correct 548 ms 1028 KB Output is correct
53 Correct 593 ms 1036 KB Output is correct
54 Correct 577 ms 1032 KB Output is correct
55 Correct 701 ms 14996 KB Output is correct
56 Correct 709 ms 15184 KB Output is correct
57 Correct 715 ms 15192 KB Output is correct
58 Correct 572 ms 15184 KB Output is correct
59 Correct 716 ms 19792 KB Output is correct
60 Correct 708 ms 19780 KB Output is correct
61 Correct 417 ms 856 KB Output is correct
62 Correct 567 ms 856 KB Output is correct
63 Correct 538 ms 856 KB Output is correct
64 Correct 551 ms 1176 KB Output is correct
65 Correct 588 ms 1176 KB Output is correct
66 Correct 545 ms 1180 KB Output is correct
67 Correct 530 ms 1176 KB Output is correct
68 Correct 556 ms 1172 KB Output is correct
69 Correct 516 ms 600 KB Output is correct
70 Correct 561 ms 856 KB Output is correct
71 Correct 507 ms 1028 KB Output is correct
72 Correct 579 ms 1164 KB Output is correct
73 Correct 606 ms 972 KB Output is correct
74 Correct 610 ms 920 KB Output is correct
75 Correct 574 ms 600 KB Output is correct
76 Correct 591 ms 1400 KB Output is correct
77 Correct 560 ms 1456 KB Output is correct
78 Correct 576 ms 1460 KB Output is correct
79 Correct 593 ms 856 KB Output is correct
80 Correct 596 ms 600 KB Output is correct
81 Correct 582 ms 600 KB Output is correct
82 Correct 559 ms 600 KB Output is correct
83 Correct 267 ms 708 KB Output is correct
84 Correct 536 ms 1032 KB Output is correct
85 Correct 559 ms 1040 KB Output is correct
86 Correct 589 ms 1028 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 381 ms 13392 KB Output is correct
89 Correct 705 ms 10072 KB Output is correct
90 Correct 658 ms 9540 KB Output is correct
91 Correct 670 ms 15216 KB Output is correct
92 Correct 718 ms 15448 KB Output is correct
93 Correct 713 ms 15432 KB Output is correct
94 Correct 747 ms 15184 KB Output is correct
95 Correct 704 ms 15448 KB Output is correct
96 Correct 667 ms 8064 KB Output is correct
97 Correct 617 ms 8148 KB Output is correct
98 Correct 544 ms 12736 KB Output is correct
99 Correct 706 ms 15324 KB Output is correct
100 Correct 640 ms 11400 KB Output is correct
101 Correct 661 ms 10064 KB Output is correct
102 Correct 683 ms 7768 KB Output is correct
103 Correct 745 ms 19792 KB Output is correct
104 Correct 686 ms 20432 KB Output is correct
105 Correct 677 ms 20432 KB Output is correct
106 Correct 630 ms 10440 KB Output is correct
107 Correct 631 ms 8364 KB Output is correct
108 Correct 666 ms 8280 KB Output is correct
109 Correct 676 ms 8024 KB Output is correct