답안 #1059474

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059474 2024-08-15T03:00:41 Z shiomusubi496 디지털 회로 (IOI22_circuit) C++17
100 / 100
643 ms 38064 KB
#include "circuit.h"
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }


constexpr ll MOD = 1000002022;

template<class A> class LazySegmentTree {
    using M = typename A::M;
    using E = typename A::E;
    using T = typename M::T;
    using U = typename E::T;

    int n, h;
    vector<T> dat;
    vector<U> laz;

    void all_apply(int k, U x) {
        dat[k] = A::op(x, dat[k]);
        if (k < n) laz[k] = E::op(laz[k], x);
    }

    void eval(int k) {
        if (laz[k] == E::id()) return;
        all_apply(k * 2, laz[k]);
        all_apply(k * 2 + 1, laz[k]);
        laz[k] = E::id();
    }

    void calc(int k) {
        dat[k] = M::op(dat[k * 2], dat[k * 2 + 1]);
    }

public:
    LazySegmentTree() = default;
    LazySegmentTree(int n_) {
        n = 1, h = 0;
        while (n < n_) n *= 2, ++h;
        dat.assign(2 * n, M::id());
        laz.assign(n, E::id());
    }
    LazySegmentTree(vector<T> v) {
        n = 1, h = 0;
        while (n < v.size()) n *= 2, ++h;
        dat.assign(2 * n, M::id());
        laz.assign(n, E::id());
        rep (i, v.size()) dat[n + i] = v[i];
        rrep2 (i, 1, n) calc(i);
    }
    void apply(int l, int r, U x) {
        l += n; r += n;
        rrep2 (i, 1, h + 1) {
            if (((l >> i) << i) != l) eval(l >> i);
            if (((r >> i) << i) != r) eval((r - 1) >> i);
        }
        for (int l2 = l, r2 = r; l2 < r2; l2 >>= 1, r2 >>= 1) {
            if (l2 & 1) all_apply(l2++, x);
            if (r2 & 1) all_apply(--r2, x);
        }
        rep2 (i, 1, h + 1) {
            if (((l >> i) << i) != l) calc(l >> i);
            if (((r >> i) << i) != r) calc((r - 1) >> i);
        }
    }
    T all_prod() const { return dat[1]; }
};

struct Act {
    struct M {
        using T = pair<ll, ll>;
        static T op(T a, T b) { return {(a.first + b.first) % MOD, (a.second + b.second) % MOD}; }
        static T id() { return {0, 0}; }
    };
    struct E {
        using T = bool;
        static T op(T a, T b) { return a ? !b : b; }
        static T id() { return false; }
    };
    static pair<ll, ll> op(bool f, pair<ll, ll> a) {
        if (!f) return a;
        return {a.second, a.first};
    }
};

int N, M;
vector<vector<int>> G;
vector<int> A;

vector<ll> coef;

LazySegmentTree<Act> seg;

void init(int N_, int M_, std::vector<int> P, std::vector<int> A_) {
    N = N_; M = M_;
    A = A_;
    G.assign(N + M, vector<int>());
    rep2 (i, 1, N + M) G[P[i]].push_back(i);
    vector<ll> B(N + M, 1);
    {
        auto dfs = [&](auto&& self, int v) -> void {
            if (v >= N) return;
            for (int c : G[v]) self(self, c);
            B[v] = G[v].size();
            for (int c : G[v]) (B[v] *= B[c]) %= MOD;
        };
        dfs(dfs, 0);
    }
    coef.assign(M, 0);
    auto dfs = [&](auto&& self, int v, ll p) -> void {
        if (v >= N) {
            coef[v - N] = p;
            return;
        }
        int n = G[v].size();
        vector<ll> cum1(n + 1, 1), cum2(n + 1, 1);
        rep (i, n) cum1[i + 1] = cum1[i] * B[G[v][i]] % MOD;
        rrep (i, n) cum2[i] = cum2[i + 1] * B[G[v][i]] % MOD;
        rep (i, n) self(self, G[v][i], p * (cum1[i] * cum2[i + 1] % MOD) % MOD);
    };
    dfs(dfs, 0, 1);

    vector<pair<ll, ll>> X(M);
    rep (i, M) {
        if (A[i] == 0) X[i] = {0, coef[i]};
        else X[i] = {coef[i], 0};
    }
    seg = LazySegmentTree<Act>(X);
}

int count_ways(int L, int R) {
    L = L - N;
    R = R + 1 - N;
    seg.apply(L, R, true);
    return seg.all_prod().first;
}

Compilation message

circuit.cpp: In instantiation of 'LazySegmentTree<A>::LazySegmentTree(std::vector<typename A::M::T>) [with A = Act; typename A::M::T = std::pair<long long int, long long int>]':
circuit.cpp:141:33:   required from here
circuit.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         while (n < v.size()) n *= 2, ++h;
      |                ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 504 KB Output is correct
3 Correct 0 ms 504 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 504 KB Output is correct
3 Correct 0 ms 504 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 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 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 612 KB Output is correct
29 Correct 0 ms 608 KB Output is correct
30 Correct 0 ms 568 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 612 KB Output is correct
33 Correct 1 ms 356 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 356 KB Output is correct
36 Correct 0 ms 612 KB Output is correct
37 Correct 0 ms 612 KB Output is correct
38 Correct 1 ms 612 KB Output is correct
39 Correct 0 ms 352 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
# 결과 실행 시간 메모리 Grader output
1 Correct 390 ms 6744 KB Output is correct
2 Correct 515 ms 12992 KB Output is correct
3 Correct 498 ms 12880 KB Output is correct
4 Correct 558 ms 13032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 390 ms 6744 KB Output is correct
2 Correct 515 ms 12992 KB Output is correct
3 Correct 498 ms 12880 KB Output is correct
4 Correct 558 ms 13032 KB Output is correct
5 Correct 493 ms 6744 KB Output is correct
6 Correct 509 ms 12888 KB Output is correct
7 Correct 551 ms 12988 KB Output is correct
8 Correct 483 ms 12944 KB Output is correct
9 Correct 287 ms 600 KB Output is correct
10 Correct 539 ms 1204 KB Output is correct
11 Correct 584 ms 1208 KB Output is correct
12 Correct 511 ms 1216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 390 ms 6744 KB Output is correct
14 Correct 515 ms 12992 KB Output is correct
15 Correct 498 ms 12880 KB Output is correct
16 Correct 558 ms 13032 KB Output is correct
17 Correct 493 ms 6744 KB Output is correct
18 Correct 509 ms 12888 KB Output is correct
19 Correct 551 ms 12988 KB Output is correct
20 Correct 483 ms 12944 KB Output is correct
21 Correct 287 ms 600 KB Output is correct
22 Correct 539 ms 1204 KB Output is correct
23 Correct 584 ms 1208 KB Output is correct
24 Correct 511 ms 1216 KB Output is correct
25 Correct 590 ms 20276 KB Output is correct
26 Correct 530 ms 20560 KB Output is correct
27 Correct 571 ms 20548 KB Output is correct
28 Correct 490 ms 20560 KB Output is correct
29 Correct 615 ms 37708 KB Output is correct
30 Correct 570 ms 37700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 504 KB Output is correct
3 Correct 0 ms 504 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 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 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 612 KB Output is correct
29 Correct 0 ms 608 KB Output is correct
30 Correct 0 ms 568 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 612 KB Output is correct
33 Correct 1 ms 356 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 356 KB Output is correct
36 Correct 0 ms 612 KB Output is correct
37 Correct 0 ms 612 KB Output is correct
38 Correct 1 ms 612 KB Output is correct
39 Correct 0 ms 352 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 386 ms 1024 KB Output is correct
44 Correct 518 ms 856 KB Output is correct
45 Correct 508 ms 1052 KB Output is correct
46 Correct 509 ms 1492 KB Output is correct
47 Correct 569 ms 1488 KB Output is correct
48 Correct 577 ms 1492 KB Output is correct
49 Correct 548 ms 1500 KB Output is correct
50 Correct 556 ms 1492 KB Output is correct
51 Correct 507 ms 1112 KB Output is correct
52 Correct 517 ms 1112 KB Output is correct
53 Correct 407 ms 1624 KB Output is correct
54 Correct 570 ms 1488 KB Output is correct
55 Correct 541 ms 1304 KB Output is correct
56 Correct 528 ms 1232 KB Output is correct
57 Correct 569 ms 1112 KB Output is correct
58 Correct 604 ms 2340 KB Output is correct
59 Correct 540 ms 2440 KB Output is correct
60 Correct 520 ms 2448 KB Output is correct
61 Correct 545 ms 1284 KB Output is correct
62 Correct 561 ms 1016 KB Output is correct
63 Correct 541 ms 856 KB Output is correct
64 Correct 558 ms 1112 KB Output is correct
65 Correct 298 ms 600 KB Output is correct
66 Correct 551 ms 1208 KB Output is correct
67 Correct 513 ms 1212 KB Output is correct
68 Correct 531 ms 1220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 504 KB Output is correct
3 Correct 0 ms 504 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 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 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 600 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 612 KB Output is correct
29 Correct 0 ms 608 KB Output is correct
30 Correct 0 ms 568 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 612 KB Output is correct
33 Correct 1 ms 356 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 356 KB Output is correct
36 Correct 0 ms 612 KB Output is correct
37 Correct 0 ms 612 KB Output is correct
38 Correct 1 ms 612 KB Output is correct
39 Correct 0 ms 352 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 390 ms 6744 KB Output is correct
44 Correct 515 ms 12992 KB Output is correct
45 Correct 498 ms 12880 KB Output is correct
46 Correct 558 ms 13032 KB Output is correct
47 Correct 493 ms 6744 KB Output is correct
48 Correct 509 ms 12888 KB Output is correct
49 Correct 551 ms 12988 KB Output is correct
50 Correct 483 ms 12944 KB Output is correct
51 Correct 287 ms 600 KB Output is correct
52 Correct 539 ms 1204 KB Output is correct
53 Correct 584 ms 1208 KB Output is correct
54 Correct 511 ms 1216 KB Output is correct
55 Correct 590 ms 20276 KB Output is correct
56 Correct 530 ms 20560 KB Output is correct
57 Correct 571 ms 20548 KB Output is correct
58 Correct 490 ms 20560 KB Output is correct
59 Correct 615 ms 37708 KB Output is correct
60 Correct 570 ms 37700 KB Output is correct
61 Correct 386 ms 1024 KB Output is correct
62 Correct 518 ms 856 KB Output is correct
63 Correct 508 ms 1052 KB Output is correct
64 Correct 509 ms 1492 KB Output is correct
65 Correct 569 ms 1488 KB Output is correct
66 Correct 577 ms 1492 KB Output is correct
67 Correct 548 ms 1500 KB Output is correct
68 Correct 556 ms 1492 KB Output is correct
69 Correct 507 ms 1112 KB Output is correct
70 Correct 517 ms 1112 KB Output is correct
71 Correct 407 ms 1624 KB Output is correct
72 Correct 570 ms 1488 KB Output is correct
73 Correct 541 ms 1304 KB Output is correct
74 Correct 528 ms 1232 KB Output is correct
75 Correct 569 ms 1112 KB Output is correct
76 Correct 604 ms 2340 KB Output is correct
77 Correct 540 ms 2440 KB Output is correct
78 Correct 520 ms 2448 KB Output is correct
79 Correct 545 ms 1284 KB Output is correct
80 Correct 561 ms 1016 KB Output is correct
81 Correct 541 ms 856 KB Output is correct
82 Correct 558 ms 1112 KB Output is correct
83 Correct 298 ms 600 KB Output is correct
84 Correct 551 ms 1208 KB Output is correct
85 Correct 513 ms 1212 KB Output is correct
86 Correct 531 ms 1220 KB Output is correct
87 Correct 1 ms 344 KB Output is correct
88 Correct 402 ms 18436 KB Output is correct
89 Correct 590 ms 12452 KB Output is correct
90 Correct 581 ms 12368 KB Output is correct
91 Correct 593 ms 20560 KB Output is correct
92 Correct 592 ms 20560 KB Output is correct
93 Correct 643 ms 20640 KB Output is correct
94 Correct 627 ms 20560 KB Output is correct
95 Correct 596 ms 20560 KB Output is correct
96 Correct 537 ms 14000 KB Output is correct
97 Correct 559 ms 14000 KB Output is correct
98 Correct 543 ms 27736 KB Output is correct
99 Correct 600 ms 20564 KB Output is correct
100 Correct 561 ms 16976 KB Output is correct
101 Correct 588 ms 15704 KB Output is correct
102 Correct 572 ms 13912 KB Output is correct
103 Correct 609 ms 37712 KB Output is correct
104 Correct 581 ms 38064 KB Output is correct
105 Correct 575 ms 38064 KB Output is correct
106 Correct 585 ms 14936 KB Output is correct
107 Correct 591 ms 13392 KB Output is correct
108 Correct 598 ms 13656 KB Output is correct
109 Correct 594 ms 14168 KB Output is correct