답안 #1059753

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059753 2024-08-15T07:49:30 Z Forested 디지털 회로 (IOI22_circuit) C++17
100 / 100
759 ms 15804 KB
#include <bits/stdc++.h>
using namespace std;

using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<VV<T>>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) size(x)

void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
    cerr << '(';
    dbg(p.first);
    cerr << ", ";
    dbg(p.second);
    cerr << ')';
}
template <typename T>
void dbg(V<T> arr) {
    cerr << '[';
    REP(i, LEN(arr)) {
        if (i) {
            cerr << ", ";
        }
        dbg(arr[i]);
    }
    cerr << ']';
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
    dbg(head);
    cerr << ", ";
    debug(tail...);
}
#ifdef DEBUGF
#define DBG(...)                       \
    do {                               \
        cerr << #__VA_ARGS__ << " : "; \
        debug(__VA_ARGS__);            \
    } while (false)
#else
#define DBG(...) (void)0
#endif

constexpr unsigned MOD = 1000002022;
struct M {
    unsigned v;
    M() : v(0) {}
    M(unsigned v) : v(v) {}
    M &operator+=(M rhs) {
        if ((v += rhs.v) >= MOD) {
            v -= MOD;
        }
        return *this;
    }
    M &operator-=(M rhs) {
        if ((v -= rhs.v) >= MOD) {
            v += MOD;
        }
        return *this;
    }
    M &operator*=(M rhs) {
        v = (unsigned long long)v * rhs.v % MOD;
        return *this;
    }
    friend M operator+(M lhs, M rhs) { return lhs += rhs; }
    friend M operator-(M lhs, M rhs) { return lhs -= rhs; }
    friend M operator*(M lhs, M rhs) { return lhs *= rhs; }
};

void dbg(M x) { cerr << x.v; }

#include <vector>
#include "circuit.h"

struct Dp {
    M c0, c1;
};

void dbg(Dp d) {
    cerr << '(';
    dbg(d.c0);
    cerr << ", ";
    dbg(d.c1);
    cerr << ')';
}

Dp merge_2(Dp l, Dp r) {
    return Dp{l.c0 + r.c0, l.c1 + r.c1};
}

void toggle(Dp &d) {
    swap(d.c0, d.c1);
}

i32 ceil_pow2(i32 n) {
    i32 k = 1;
    while (k < n) {
        k *= 2;
    }
    return k;
}

class LazySeg {
    i32 n;
    V<Dp> val;
    V<i32> rev;
    
    void push(i32 cur) {
        if (rev[cur]) {
            rev[cur] = 0;
            if (cur < n) {
                toggle(val[2 * cur]);
                toggle(val[2 * cur + 1]);
                rev[2 * cur] ^= 1;
                rev[2 * cur + 1] ^= 1;
            }
        }
    }
    
    void _apply(i32 cur, i32 curl, i32 curr, i32 l, i32 r) {
        if (curr <= l || r <= curl) {
            return;
        }
        push(cur);
        if (l <= curl && curr <= r) {
            toggle(val[cur]);
            rev[cur] ^= 1;
            return;
        }
        i32 curm = (curl + curr) / 2;
        _apply(2 * cur, curl, curm, l, r);
        _apply(2 * cur + 1, curm, curr, l, r);
        val[cur] = merge_2(val[2 * cur], val[2 * cur + 1]);
    }

public:
    LazySeg() = default;
    LazySeg(V<M> wt, V<i32> a) : n(ceil_pow2(LEN(wt))), val(2 * n), rev(2 * n, 0) {
        REP(i, LEN(wt)) {
            if (a[i] == 0) {
                val[n + i].c0 = wt[i];
            } else {
                val[n + i].c1 = wt[i];
            }
        }
        PER(i, 1, n) {
            val[i] = merge_2(val[2 * i], val[2 * i + 1]);
        }
        DBG(val);
    }
    
    void apply(i32 l, i32 r) {
        _apply(1, 0, n, l, r);
    }
    
    Dp get_all() const {
        return val[1];
    }
};

i32 n, m;
V<i32> p, a;
VV<i32> chl;
LazySeg seg;

void init(i32 _n, i32 _m, V<i32> _p, V<i32> _a) {
    n = _n;
    m = _m;
    p = _p;
    a = _a;

    chl.resize(n);
    REP(i, 1, n + m) { chl[p[i]].push_back(i); }
    
    V<M> prods(n + m, M(1));
    PER(i, n) {
        for (i32 j : chl[i]) {
            prods[i] *= prods[j];
        }
        prods[i] *= LEN(chl[i]);
    }
    DBG(prods);
    
    V<M> wts(n + m, M(1));
    REP(i, n) {
        i32 c = LEN(chl[i]);
        V<M> lp(c + 1, M(1)), rp(c + 1, M(1));
        REP(j, c) {
            lp[j + 1] = lp[j] * prods[chl[i][j]];
        }
        PER(j, c) {
            rp[j] = rp[j + 1] * prods[chl[i][j]];
        }
        REP(j, c) {
            wts[chl[i][j]] = wts[i] * lp[j] * rp[j + 1];
        }
    }
    DBG(wts);
    V<M> wt(m);
    REP(i, m) {
        wt[i] = wts[n + i];
    }
    DBG(wt);
    seg = LazySeg(wt, a);
}

int count_ways(i32 l, i32 r) {
    ++r;
    l -= n;
    r -= n;
    seg.apply(l, r);
    return seg.get_all().c1.v;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 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 356 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 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 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 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 356 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 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 356 KB Output is correct
28 Correct 1 ms 340 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 0 ms 344 KB Output is correct
33 Correct 0 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 0 ms 344 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 385 ms 4932 KB Output is correct
2 Correct 681 ms 9560 KB Output is correct
3 Correct 640 ms 9560 KB Output is correct
4 Correct 632 ms 9560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 385 ms 4932 KB Output is correct
2 Correct 681 ms 9560 KB Output is correct
3 Correct 640 ms 9560 KB Output is correct
4 Correct 632 ms 9560 KB Output is correct
5 Correct 559 ms 4952 KB Output is correct
6 Correct 698 ms 9668 KB Output is correct
7 Correct 689 ms 9552 KB Output is correct
8 Correct 689 ms 9560 KB Output is correct
9 Correct 319 ms 600 KB Output is correct
10 Correct 634 ms 984 KB Output is correct
11 Correct 601 ms 980 KB Output is correct
12 Correct 637 ms 988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 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 356 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 385 ms 4932 KB Output is correct
14 Correct 681 ms 9560 KB Output is correct
15 Correct 640 ms 9560 KB Output is correct
16 Correct 632 ms 9560 KB Output is correct
17 Correct 559 ms 4952 KB Output is correct
18 Correct 698 ms 9668 KB Output is correct
19 Correct 689 ms 9552 KB Output is correct
20 Correct 689 ms 9560 KB Output is correct
21 Correct 319 ms 600 KB Output is correct
22 Correct 634 ms 984 KB Output is correct
23 Correct 601 ms 980 KB Output is correct
24 Correct 637 ms 988 KB Output is correct
25 Correct 645 ms 14936 KB Output is correct
26 Correct 701 ms 15184 KB Output is correct
27 Correct 759 ms 15188 KB Output is correct
28 Correct 546 ms 15184 KB Output is correct
29 Correct 719 ms 15184 KB Output is correct
30 Correct 667 ms 15184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 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 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 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 356 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 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 356 KB Output is correct
28 Correct 1 ms 340 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 0 ms 344 KB Output is correct
33 Correct 0 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 0 ms 344 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 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 468 ms 856 KB Output is correct
44 Correct 636 ms 920 KB Output is correct
45 Correct 643 ms 856 KB Output is correct
46 Correct 639 ms 1364 KB Output is correct
47 Correct 637 ms 1208 KB Output is correct
48 Correct 635 ms 1204 KB Output is correct
49 Correct 651 ms 1212 KB Output is correct
50 Correct 636 ms 1208 KB Output is correct
51 Correct 631 ms 868 KB Output is correct
52 Correct 646 ms 872 KB Output is correct
53 Correct 627 ms 600 KB Output is correct
54 Correct 703 ms 1212 KB Output is correct
55 Correct 618 ms 856 KB Output is correct
56 Correct 591 ms 1108 KB Output is correct
57 Correct 623 ms 864 KB Output is correct
58 Correct 644 ms 1208 KB Output is correct
59 Correct 626 ms 1240 KB Output is correct
60 Correct 619 ms 1240 KB Output is correct
61 Correct 609 ms 908 KB Output is correct
62 Correct 612 ms 816 KB Output is correct
63 Correct 637 ms 776 KB Output is correct
64 Correct 659 ms 872 KB Output is correct
65 Correct 321 ms 600 KB Output is correct
66 Correct 666 ms 980 KB Output is correct
67 Correct 661 ms 988 KB Output is correct
68 Correct 629 ms 988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 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 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 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 356 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 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 356 KB Output is correct
28 Correct 1 ms 340 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 0 ms 344 KB Output is correct
33 Correct 0 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 0 ms 344 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 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 385 ms 4932 KB Output is correct
44 Correct 681 ms 9560 KB Output is correct
45 Correct 640 ms 9560 KB Output is correct
46 Correct 632 ms 9560 KB Output is correct
47 Correct 559 ms 4952 KB Output is correct
48 Correct 698 ms 9668 KB Output is correct
49 Correct 689 ms 9552 KB Output is correct
50 Correct 689 ms 9560 KB Output is correct
51 Correct 319 ms 600 KB Output is correct
52 Correct 634 ms 984 KB Output is correct
53 Correct 601 ms 980 KB Output is correct
54 Correct 637 ms 988 KB Output is correct
55 Correct 645 ms 14936 KB Output is correct
56 Correct 701 ms 15184 KB Output is correct
57 Correct 759 ms 15188 KB Output is correct
58 Correct 546 ms 15184 KB Output is correct
59 Correct 719 ms 15184 KB Output is correct
60 Correct 667 ms 15184 KB Output is correct
61 Correct 468 ms 856 KB Output is correct
62 Correct 636 ms 920 KB Output is correct
63 Correct 643 ms 856 KB Output is correct
64 Correct 639 ms 1364 KB Output is correct
65 Correct 637 ms 1208 KB Output is correct
66 Correct 635 ms 1204 KB Output is correct
67 Correct 651 ms 1212 KB Output is correct
68 Correct 636 ms 1208 KB Output is correct
69 Correct 631 ms 868 KB Output is correct
70 Correct 646 ms 872 KB Output is correct
71 Correct 627 ms 600 KB Output is correct
72 Correct 703 ms 1212 KB Output is correct
73 Correct 618 ms 856 KB Output is correct
74 Correct 591 ms 1108 KB Output is correct
75 Correct 623 ms 864 KB Output is correct
76 Correct 644 ms 1208 KB Output is correct
77 Correct 626 ms 1240 KB Output is correct
78 Correct 619 ms 1240 KB Output is correct
79 Correct 609 ms 908 KB Output is correct
80 Correct 612 ms 816 KB Output is correct
81 Correct 637 ms 776 KB Output is correct
82 Correct 659 ms 872 KB Output is correct
83 Correct 321 ms 600 KB Output is correct
84 Correct 666 ms 980 KB Output is correct
85 Correct 661 ms 988 KB Output is correct
86 Correct 629 ms 988 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 410 ms 13984 KB Output is correct
89 Correct 645 ms 9816 KB Output is correct
90 Correct 733 ms 9296 KB Output is correct
91 Correct 686 ms 15184 KB Output is correct
92 Correct 717 ms 15184 KB Output is correct
93 Correct 703 ms 15192 KB Output is correct
94 Correct 713 ms 15184 KB Output is correct
95 Correct 698 ms 15180 KB Output is correct
96 Correct 632 ms 8128 KB Output is correct
97 Correct 715 ms 8128 KB Output is correct
98 Correct 656 ms 7768 KB Output is correct
99 Correct 728 ms 15184 KB Output is correct
100 Correct 682 ms 11400 KB Output is correct
101 Correct 682 ms 10064 KB Output is correct
102 Correct 697 ms 8280 KB Output is correct
103 Correct 696 ms 15184 KB Output is correct
104 Correct 673 ms 15804 KB Output is correct
105 Correct 710 ms 15544 KB Output is correct
106 Correct 657 ms 9292 KB Output is correct
107 Correct 652 ms 9304 KB Output is correct
108 Correct 744 ms 9048 KB Output is correct
109 Correct 700 ms 8280 KB Output is correct