Submission #837205

# Submission time Handle Problem Language Result Execution time Memory
837205 2023-08-25T08:04:45 Z tengiz05 Digital Circuit (IOI22_circuit) C++17
0 / 100
19 ms 7612 KB
#include "circuit.h"

#include <vector>
#include <array>
#include <iostream>
#include <algorithm>
using namespace std;
constexpr int P = 1E9 + 2022;
int norm(int x) {
    if (x < 0) x += P;
    if (x >= P) x -= P;
    return  x;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() { return x; }
    friend Z operator+(const Z &a, const Z &b) {
        return Z(a.x + b.x);
    }
    friend Z operator-(const Z &a, const Z &b) {
        return Z(a.x - b.x);
    }
    friend Z operator*(const Z &a, const Z &b) {
        return Z(int(a.x * 1LL * b.x % P));
    }
    void operator += (const Z &o) {
        *this = *this + o;
    }
};

int n, m;
vector<int> a;
vector<vector<int>> e;
vector<array<Z, 2>> res, rev;
vector<int> lazy;

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
    n = N;
    m = M;
    e.resize(n);
    a = A;
    for (int i = 1; i < n + m; i++) {
        e[P[i]].push_back(i);
    }
    lazy.resize(n + m);
    res.resize(n + m);
    rev.resize(n + m);
    for (int i = 0; i < m; i++) {
        res[n + i][a[i]] = 1;
        rev[n + i][a[i] ^ 1] = 1;
    }
    for (int i = n - 1; i >= 0; i--) {
        vector<Z> dp(e[i].size() + 1);
        dp[0] = 1;
        for (int j : e[i]) {
            vector<Z> ndp(e[i].size() + 1);
            for (int k = e[i].size(); k >= 0; k--) {
                if (k < int(e[i].size())) {
                    ndp[k + 1] += dp[k] * res[j][1];
                }
                ndp[k] += dp[k] * res[j][0];
            }
            dp = ndp;
        }
        int c = e[i].size();
        res[i][0] = res[i][1] = 0;
        res[i][0] += dp[0] * c;
        for (int j = 1; j < int(dp.size()); j++) {
            res[i][1] += dp[j] * j;
            res[i][0] += dp[j] * (c - j);
        }
    }
    for (int i = n - 1; i >= 0; i--) {
        vector<Z> dp(e[i].size() + 1);
        dp[0] = 1;
        for (int j : e[i]) {
            vector<Z> ndp(e[i].size() + 1);
            for (int k = e[i].size(); k >= 0; k--) {
                if (k < int(e[i].size())) {
                    ndp[k + 1] += dp[k] * rev[j][1];
                }
                ndp[k] += dp[k] * rev[j][0];
            }
            dp = ndp;
        }
        int c = e[i].size();
        rev[i][0] = rev[i][1] = 0;
        rev[i][0] += dp[0] * c;
        for (int j = 1; j < int(dp.size()); j++) {
            rev[i][1] += dp[j] * j;
            rev[i][0] += dp[j] * (c - j);
        }
    }
}
int count_ways(int L, int R) {
    R++;
    L -= n;
    R -= n;
    
    auto push = [&](int p) {
        if (lazy[p]) {
            swap(res[2 * p + 1], rev[2 * p + 1]);
            swap(res[2 * p + 2], rev[2 * p + 2]);
            lazy[2 * p + 1] ^= 1;
            lazy[2 * p + 2] ^= 1;
            lazy[p] = 0;
        }
    };
    function<void(int, int, int, int, int)> rangeApply = [&](int p, int l, int r, int x, int y) {
        if (r <= x || y <= l) return;
        if (x <= l && r <= y) {
            swap(res[p], rev[p]);
            lazy[p] ^= 1;
            return;
        }
        push(p);
        rangeApply(2 * p + 1, l, m, x, y);
        rangeApply(2 * p + 2, m, r, x, y);
        {
            int i = p;
            vector<Z> dp(e[i].size() + 1);
            dp[0] = 1;
            for (int j : e[i]) {
                vector<Z> ndp(e[i].size() + 1);
                for (int k = e[i].size(); k >= 0; k--) {
                    if (k < int(e[i].size())) {
                        ndp[k + 1] += dp[k] * res[j][1];
                    }
                    ndp[k] += dp[k] * res[j][0];
                }
                dp = ndp;
            }
            int c = e[i].size();
            res[i][0] = res[i][1] = 0;
            res[i][0] += dp[0] * c;
            for (int j = 1; j < int(dp.size()); j++) {
                res[i][1] += dp[j] * j;
                res[i][0] += dp[j] * (c - j);
            }
        } {
            int i = p;
            vector<Z> dp(e[i].size() + 1);
            dp[0] = 1;
            for (int j : e[i]) {
                vector<Z> ndp(e[i].size() + 1);
                for (int k = e[i].size(); k >= 0; k--) {
                    if (k < int(e[i].size())) {
                        ndp[k + 1] += dp[k] * rev[j][1];
                    }
                    ndp[k] += dp[k] * rev[j][0];
                }
                dp = ndp;
            }
            int c = e[i].size();
            rev[i][0] = rev[i][1] = 0;
            rev[i][0] += dp[0] * c;
            for (int j = 1; j < int(dp.size()); j++) {
                rev[i][1] += dp[j] * j;
                rev[i][0] += dp[j] * (c - j);
            }
        }
    };
    
    rangeApply(0, 0, m, L, R);
    
    return res[0][1].val();
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 19 ms 7612 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 19 ms 7612 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -