답안 #826752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826752 2023-08-16T02:19:16 Z amsraman 디지털 회로 (IOI22_circuit) C++17
100 / 100
920 ms 31300 KB
#include <bits/stdc++.h>

using namespace std;

template <class B>
struct LazySegTree : public B {
    using T_q = typename B::T_q;
    using T_u = typename B::T_u;
    int n, sz, log; vector<T_q> seg; vector<T_u> lazy;
    LazySegTree(int n = 0): n(n), log(__lg(max(n - 1, 1)) + 1) {
        sz = (1 << log), seg.resize(sz << 1, B::e_q), lazy.resize(sz << 1, B::e_u);
    }
    LazySegTree(vector<T_q> & init): n((int) init.size()), log(__lg(max(n - 1, 1)) + 1) {
        sz = (1 << log), seg.resize(sz << 1, B::e_q), lazy.resize(sz << 1, B::e_u);
        copy(init.begin(), init.end(), seg.begin() + sz);
        for(int i = sz - 1; i > 0; i--) refresh(i);
    }
    void refresh(int ind) {
        seg[ind] = B::comb(seg[2 * ind], seg[2 * ind + 1]);
    }
    void app(int ind, int lo, int hi, T_u delta) {
        seg[ind] = B::upd(seg[ind], delta, lo, hi);
        lazy[ind] = B::comb_upd(delta, lazy[ind]);
    }
    void push(int ind, int lo, int hi) {
        if(lo != hi) {
            int mid = (lo + hi) >> 1;
            app(2 * ind, lo, mid, lazy[ind]);
            app(2 * ind + 1, mid + 1, hi, lazy[ind]);
        }
        lazy[ind] = B::e_u;
    }
    void push_from_root(int ind) {
        ind += sz;
        for(int i = log; i > 0; i--) {
            push(ind >> i, (ind >> i << i) - sz, (((ind >> i) + 1) << i) - sz - 1);
        }
    }
    T_q qry(int lo, int hi) {
        push_from_root(lo), push_from_root(hi); T_q ret1 = B::e_q, ret2 = B::e_q;
        for(lo += sz, hi += sz; lo <= hi; lo >>= 1, hi >>= 1) {
            if(lo & 1) ret1 = B::comb(ret1, seg[lo++]);
            if(hi & 1 ^ 1) ret2 = B::comb(seg[hi--], ret2);
        }
        return B::comb(ret1, ret2);
    }
    void upd(int lo, int hi, T_u delta) {
        push_from_root(lo), push_from_root(hi); lo += sz, hi += sz;
        for(int l = lo, r = hi, lvl = 0; l <= r; l >>= 1, r >>= 1, ++lvl) {
            if(l & 1) app(l, (l << lvl) - sz, (l + 1 << lvl) - sz - 1, delta), l++;
            if(r & 1 ^ 1) app(r, (r << lvl) - sz, (r + 1 << lvl) - sz - 1, delta), r--;
        }
        for(int i = 1; i <= log; i++) {
            if(lo >> i << i != lo) refresh(lo >> i);
            if((hi + 1) >> i << i != hi + 1) refresh(hi >> i);
        }
    }
};

const int mod = 1000002022;

struct Monoid {
    using T_q = pair<int, int>; static constexpr T_q e_q = {0, 0};
    using T_u = bool; static const T_u e_u = false;
    T_q comb(T_q a, T_q b) {
        return {(a.first + b.first) % mod, (a.second + b.second) % mod};
    }
    T_q upd(T_q a, T_u b, int l, int r) {
        if(b) {
            return {(a.second - a.first + mod) % mod, a.second};
        }
        return a;
    }
    T_u comb_upd(T_u a, T_u b) {
        // a after b
        return a ^ b;
    }
};

int tot;
LazySegTree<Monoid> lst;

void init(int n, int m, vector<int> p, vector<int> a) {
    tot = n;
    vector<vector<int>> g(n);
    vector<int> prod(n + m, 1);
    vector<pair<int, int>> val(m);
    for(int i = 1; i < n + m; i++) {
        g[p[i]].push_back(i);
    }
    auto dfs = [&](auto rec, int u) -> void {
        prod[u] = (int) g[u].size();
        for(int v: g[u]) {
            if(v < n) {
                rec(rec, v);
                prod[u] = (1LL * prod[u] * prod[v]) % mod;
            }
        }
    };
    auto dfs2 = [&](auto rec, int u, int down) {
        if(u >= n) {
            val[u - n].second = down;
            return;
        }
        int sz = (int) g[u].size(), pf = 1;
        vector<int> sf;
        for(int v: g[u]) sf.push_back(prod[v]);
        sf.push_back(1);
        for(int i = sz - 1; i >= 0; i--) sf[i] = (1LL * sf[i] * sf[i + 1]) % mod;
        for(int i = 0; i < sz; i++) {
            int other = (1LL * pf * sf[i + 1]) % mod;
            rec(rec, g[u][i], (1LL * other * down) % mod);
            pf = (1LL * pf * prod[g[u][i]]) % mod;
        }
    };
    dfs(dfs, 0);
    dfs2(dfs2, 0, 1);
    for(int i = 0; i < m; i++) {
        val[i].first = a[i] * val[i].second;
    }
    lst = LazySegTree<Monoid>(val);
}

int count_ways(int l, int r) {
    lst.upd(l - tot, r - tot, true);
    return lst.seg[1].first;
}

Compilation message

circuit.cpp: In instantiation of 'void LazySegTree<B>::upd(int, int, LazySegTree<B>::T_u) [with B = Monoid; LazySegTree<B>::T_u = bool]':
circuit.cpp:125:35:   required from here
circuit.cpp:50:50: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   50 |             if(l & 1) app(l, (l << lvl) - sz, (l + 1 << lvl) - sz - 1, delta), l++;
      |                                                ~~^~~
circuit.cpp:51:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   51 |             if(r & 1 ^ 1) app(r, (r << lvl) - sz, (r + 1 << lvl) - sz - 1, delta), r--;
      |                ~~^~~
circuit.cpp:51:54: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   51 |             if(r & 1 ^ 1) app(r, (r << lvl) - sz, (r + 1 << lvl) - sz - 1, delta), r--;
      |                                                    ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 3896 KB Output is correct
2 Correct 779 ms 7496 KB Output is correct
3 Correct 746 ms 7464 KB Output is correct
4 Correct 690 ms 7472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 3896 KB Output is correct
2 Correct 779 ms 7496 KB Output is correct
3 Correct 746 ms 7464 KB Output is correct
4 Correct 690 ms 7472 KB Output is correct
5 Correct 521 ms 3920 KB Output is correct
6 Correct 651 ms 7496 KB Output is correct
7 Correct 684 ms 7504 KB Output is correct
8 Correct 733 ms 7500 KB Output is correct
9 Correct 393 ms 464 KB Output is correct
10 Correct 655 ms 720 KB Output is correct
11 Correct 613 ms 720 KB Output is correct
12 Correct 749 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 516 ms 3896 KB Output is correct
14 Correct 779 ms 7496 KB Output is correct
15 Correct 746 ms 7464 KB Output is correct
16 Correct 690 ms 7472 KB Output is correct
17 Correct 521 ms 3920 KB Output is correct
18 Correct 651 ms 7496 KB Output is correct
19 Correct 684 ms 7504 KB Output is correct
20 Correct 733 ms 7500 KB Output is correct
21 Correct 393 ms 464 KB Output is correct
22 Correct 655 ms 720 KB Output is correct
23 Correct 613 ms 720 KB Output is correct
24 Correct 749 ms 720 KB Output is correct
25 Correct 818 ms 11564 KB Output is correct
26 Correct 748 ms 11676 KB Output is correct
27 Correct 819 ms 11740 KB Output is correct
28 Correct 602 ms 11832 KB Output is correct
29 Correct 703 ms 30024 KB Output is correct
30 Correct 671 ms 30016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 483 ms 592 KB Output is correct
44 Correct 733 ms 592 KB Output is correct
45 Correct 825 ms 592 KB Output is correct
46 Correct 755 ms 848 KB Output is correct
47 Correct 730 ms 848 KB Output is correct
48 Correct 840 ms 848 KB Output is correct
49 Correct 671 ms 848 KB Output is correct
50 Correct 689 ms 848 KB Output is correct
51 Correct 627 ms 596 KB Output is correct
52 Correct 774 ms 596 KB Output is correct
53 Correct 419 ms 1616 KB Output is correct
54 Correct 738 ms 848 KB Output is correct
55 Correct 784 ms 720 KB Output is correct
56 Correct 752 ms 592 KB Output is correct
57 Correct 746 ms 524 KB Output is correct
58 Correct 789 ms 1744 KB Output is correct
59 Correct 644 ms 2000 KB Output is correct
60 Correct 733 ms 1872 KB Output is correct
61 Correct 608 ms 808 KB Output is correct
62 Correct 657 ms 592 KB Output is correct
63 Correct 640 ms 464 KB Output is correct
64 Correct 688 ms 592 KB Output is correct
65 Correct 391 ms 440 KB Output is correct
66 Correct 757 ms 720 KB Output is correct
67 Correct 727 ms 720 KB Output is correct
68 Correct 621 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 516 ms 3896 KB Output is correct
44 Correct 779 ms 7496 KB Output is correct
45 Correct 746 ms 7464 KB Output is correct
46 Correct 690 ms 7472 KB Output is correct
47 Correct 521 ms 3920 KB Output is correct
48 Correct 651 ms 7496 KB Output is correct
49 Correct 684 ms 7504 KB Output is correct
50 Correct 733 ms 7500 KB Output is correct
51 Correct 393 ms 464 KB Output is correct
52 Correct 655 ms 720 KB Output is correct
53 Correct 613 ms 720 KB Output is correct
54 Correct 749 ms 720 KB Output is correct
55 Correct 818 ms 11564 KB Output is correct
56 Correct 748 ms 11676 KB Output is correct
57 Correct 819 ms 11740 KB Output is correct
58 Correct 602 ms 11832 KB Output is correct
59 Correct 703 ms 30024 KB Output is correct
60 Correct 671 ms 30016 KB Output is correct
61 Correct 483 ms 592 KB Output is correct
62 Correct 733 ms 592 KB Output is correct
63 Correct 825 ms 592 KB Output is correct
64 Correct 755 ms 848 KB Output is correct
65 Correct 730 ms 848 KB Output is correct
66 Correct 840 ms 848 KB Output is correct
67 Correct 671 ms 848 KB Output is correct
68 Correct 689 ms 848 KB Output is correct
69 Correct 627 ms 596 KB Output is correct
70 Correct 774 ms 596 KB Output is correct
71 Correct 419 ms 1616 KB Output is correct
72 Correct 738 ms 848 KB Output is correct
73 Correct 784 ms 720 KB Output is correct
74 Correct 752 ms 592 KB Output is correct
75 Correct 746 ms 524 KB Output is correct
76 Correct 789 ms 1744 KB Output is correct
77 Correct 644 ms 2000 KB Output is correct
78 Correct 733 ms 1872 KB Output is correct
79 Correct 608 ms 808 KB Output is correct
80 Correct 657 ms 592 KB Output is correct
81 Correct 640 ms 464 KB Output is correct
82 Correct 688 ms 592 KB Output is correct
83 Correct 391 ms 440 KB Output is correct
84 Correct 757 ms 720 KB Output is correct
85 Correct 727 ms 720 KB Output is correct
86 Correct 621 ms 720 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 438 ms 10892 KB Output is correct
89 Correct 784 ms 7904 KB Output is correct
90 Correct 776 ms 7400 KB Output is correct
91 Correct 685 ms 11852 KB Output is correct
92 Correct 887 ms 11848 KB Output is correct
93 Correct 785 ms 11808 KB Output is correct
94 Correct 767 ms 11824 KB Output is correct
95 Correct 700 ms 11808 KB Output is correct
96 Correct 593 ms 5572 KB Output is correct
97 Correct 712 ms 5564 KB Output is correct
98 Correct 802 ms 27216 KB Output is correct
99 Correct 920 ms 11672 KB Output is correct
100 Correct 910 ms 8392 KB Output is correct
101 Correct 865 ms 7196 KB Output is correct
102 Correct 836 ms 5584 KB Output is correct
103 Correct 700 ms 30024 KB Output is correct
104 Correct 755 ms 31176 KB Output is correct
105 Correct 718 ms 31300 KB Output is correct
106 Correct 622 ms 11932 KB Output is correct
107 Correct 895 ms 6852 KB Output is correct
108 Correct 664 ms 6552 KB Output is correct
109 Correct 667 ms 5652 KB Output is correct