답안 #1058101

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058101 2024-08-14T08:27:39 Z Forested 디지털 회로 (IOI22_circuit) C++17
34 / 100
3000 ms 10576 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_naive(const V<Dp> &chl) {
    i32 c = LEN(chl);
    V<M> dp(1, M(1));
    REP(i, c) {
        V<M> ndp(i + 2);
        REP(j, i + 1) {
            ndp[j] += dp[j] * chl[i].c0;
            ndp[j + 1] += dp[j] * chl[i].c1;
        }
        dp.swap(ndp);
    }
    V<M> sum(c + 2);
    REP(i, c + 1) { sum[i + 1] = sum[i] + dp[i]; }
    Dp ret;
    REP(i, 1, c + 1) {
        ret.c0 += sum[i];
        ret.c1 += sum.back() - sum[i];
    }
    return ret;
}

Dp merge_2(Dp l, Dp r) {
    M tmp = l.c0 * r.c1 + l.c1 * r.c0;
    return Dp{M(2) * l.c0 * r.c0 + tmp, tmp + M(2) * l.c1 * r.c1};
}

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

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(i32 n) : n(n), val(2 * n), rev(2 * n, 0) {
        REP(i, n, 2 * n) { val[i] = Dp{M(1), M(0)}; }
        PER(i, 1, n) { val[i] = merge_2(val[2 * i], val[2 * i + 1]); }
    }
    
    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;
V<Dp> dps;

LazySeg seg;
bool is_subtask_5;

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); }

    dps.resize(n + m);
    
    is_subtask_5 = true;
    i32 z = 0;
    while ((1 << z) < m) {
        ++z;
    }
    if ((1 << z) != m || n + 1 != m) {
        is_subtask_5 = false;
    } else {
        REP(i, 1, n + m) {
            if (p[i] != (i - 1) / 2) {
                is_subtask_5 = false;
            }
        }
    }
    
    if (is_subtask_5) {
        seg = LazySeg(m);
        REP(i, m) {
            if (a[i] == 1) {
                seg.apply(i, i + 1);
            }
        }
    }
}

int count_ways(i32 l, i32 r) {
    ++r;
    l -= n;
    r -= n;
    
    if (is_subtask_5) {
        seg.apply(l, r);
        return seg.get_all().c1.v;
    }

    REP(i, l, r) { a[i] ^= 1; }
    REP(i, m) {
        dps[n + i].c0 = dps[n + i].c1 = M();
        if (a[i] == 0) {
            dps[n + i].c0 = M(1);
        } else {
            dps[n + i].c1 = M(1);
        }
    }
    PER(i, n) {
        V<Dp> cd;
        for (i32 c : chl[i]) {
            cd.push_back(dps[c]);
        }
        dps[i] = merge_naive(cd);
    }
    DBG(a);
    REP(i, n + m) { DBG(dps[i].c0, dps[i].c1); }
    return dps[0].c1.v;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 7 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 1 ms 344 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 7 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 7 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 0 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 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 1 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 7 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 1 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 7 ms 600 KB Output is correct
38 Correct 8 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 596 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 353 ms 4592 KB Output is correct
2 Correct 477 ms 8788 KB Output is correct
3 Correct 462 ms 8680 KB Output is correct
4 Correct 479 ms 8904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 4592 KB Output is correct
2 Correct 477 ms 8788 KB Output is correct
3 Correct 462 ms 8680 KB Output is correct
4 Correct 479 ms 8904 KB Output is correct
5 Correct 479 ms 4696 KB Output is correct
6 Correct 630 ms 8792 KB Output is correct
7 Correct 628 ms 8792 KB Output is correct
8 Correct 556 ms 8792 KB Output is correct
9 Correct 285 ms 852 KB Output is correct
10 Correct 559 ms 932 KB Output is correct
11 Correct 554 ms 936 KB Output is correct
12 Correct 510 ms 936 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 1 ms 344 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 353 ms 4592 KB Output is correct
14 Correct 477 ms 8788 KB Output is correct
15 Correct 462 ms 8680 KB Output is correct
16 Correct 479 ms 8904 KB Output is correct
17 Correct 479 ms 4696 KB Output is correct
18 Correct 630 ms 8792 KB Output is correct
19 Correct 628 ms 8792 KB Output is correct
20 Correct 556 ms 8792 KB Output is correct
21 Correct 285 ms 852 KB Output is correct
22 Correct 559 ms 932 KB Output is correct
23 Correct 554 ms 936 KB Output is correct
24 Correct 510 ms 936 KB Output is correct
25 Execution timed out 3039 ms 10576 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 7 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 0 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 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 1 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 7 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 1 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 7 ms 600 KB Output is correct
38 Correct 8 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Execution timed out 3050 ms 600 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 7 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 0 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 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 1 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 7 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 1 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 7 ms 600 KB Output is correct
38 Correct 8 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 353 ms 4592 KB Output is correct
44 Correct 477 ms 8788 KB Output is correct
45 Correct 462 ms 8680 KB Output is correct
46 Correct 479 ms 8904 KB Output is correct
47 Correct 479 ms 4696 KB Output is correct
48 Correct 630 ms 8792 KB Output is correct
49 Correct 628 ms 8792 KB Output is correct
50 Correct 556 ms 8792 KB Output is correct
51 Correct 285 ms 852 KB Output is correct
52 Correct 559 ms 932 KB Output is correct
53 Correct 554 ms 936 KB Output is correct
54 Correct 510 ms 936 KB Output is correct
55 Execution timed out 3039 ms 10576 KB Time limit exceeded
56 Halted 0 ms 0 KB -