Submission #1057846

# Submission time Handle Problem Language Result Execution time Memory
1057846 2024-08-14T06:45:49 Z Forested Digital Circuit (IOI22_circuit) C++17
18 / 100
3000 ms 3672 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;
    }
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
}

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

i32 n, m;
V<i32> p, a;
VV<i32> chl;
V<Dp> dps;

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

int count_ways(i32 l, i32 r) {
    ++r;
    l -= n;
    r -= n;

    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;
}
# 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 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 7 ms 344 KB Output is correct
7 Correct 10 ms 344 KB Output is correct
8 Correct 7 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 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 1 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
# 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 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 7 ms 344 KB Output is correct
7 Correct 10 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 1 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 1 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 8 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 8 ms 600 KB Output is correct
38 Correct 7 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 432 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3004 ms 3672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3004 ms 3672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 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 1 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 Execution timed out 3004 ms 3672 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 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 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 7 ms 344 KB Output is correct
7 Correct 10 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 1 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 1 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 8 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 8 ms 600 KB Output is correct
38 Correct 7 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 432 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Execution timed out 3014 ms 600 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 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 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 7 ms 344 KB Output is correct
7 Correct 10 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 1 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 1 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 8 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 2 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 8 ms 600 KB Output is correct
38 Correct 7 ms 600 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 432 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Execution timed out 3004 ms 3672 KB Time limit exceeded
44 Halted 0 ms 0 KB -