Submission #798010

# Submission time Handle Problem Language Result Execution time Memory
798010 2023-07-30T09:04:20 Z gesgha Digital Circuit (IOI22_circuit) C++17
34 / 100
3000 ms 16556 KB
#include "circuit.h"
#include <bits/stdc++.h>
 
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define fe(x, y) for (auto& x : y)
 
#define fi first
#define se second
#define pb push_back
 
#define all(x) x.begin(), x.end()
#define pw(x) (1LL << (x))
#define sz(x) (int)x.size()
 
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <typename T>
using ve = vector <T>;
 
template <typename T>
bool umx (T& a, T b) { return a < b ? a = b, 1 : 0; }
 
template <typename T>
bool umn (T& a, T b) { return a > b ? a = b, 1 : 0; }
 
using ll = long long;
using ld = long double;
using pll = pair <ll, ll>;
using pii = pair <int, int>;
using ull = unsigned long long;
 
const int oo = 1e9;
const ll OO = 1e18;
const int N = 2e5 + 10;
const int M = 5e3 + 100;
const int mod = 1e9 + 2022;

int a[N];
int dp[N][2];
int dp1[N][2];
bool rev[N];
ve <int> G[N];


void push(int v) {
    if(!rev[v]) return;
    fe (to, G[v]) {
      swap(dp1[to], dp[to]);
      rev[to] ^= 1;
    }
    rev[v] = 0;
}

int n, m;

int add(int a, int b) {
  return a + b < mod ? a + b : a + b - mod;
}
int mul (int a, int b) {
  return 1LL * a * b % mod;
}

int tin[N];
int tout[N];
int tim;
int mn[N];
int mx[N];
bool isupper(int u, int v) {
  return tin[u] <= tin[v] && tout[v] <= tout[u];
}

void dfs(int u) {
  tin[u] = tim++;
  if (!sz(G[u])) {
    mn[u] = u;
    mx[u] = u;
    if(a[u]) {
      dp[u][0] = 0;
      dp[u][1] = 1;
      dp1[u][0] = 1;
      dp1[u][1] = 0;
    } else {
      dp[u][0] = 1;
      dp[u][1] = 0;
      dp1[u][1] = 1;
      dp1[u][0] = 0;
    }
    return;
  }
  mx[u] = -oo;
  mn[u] = oo;
  for (auto to : G[u]) {
    dfs(to);
    mx[u] = max(mx[u], mx[to]);
    mn[u] = min(mn[u], mn[to]);
  }
  ve <ve <int>> d(sz(G[u]) + 1);
  ve <ve <int>> d1(sz(G[u]) + 1);

  fe (x, d) x.resize(sz(G[u]) + 1);
  fe (x, d1) x.resize(sz(G[u]) + 1);
  d[0][0] = 1;
  d1[0][0] = 1;
  for (int i = 0; i < sz(G[u]); i++) {
    int to = G[u][i];
    for (int cnt_al = 0; cnt_al <= i; cnt_al++) {
      d[i + 1][cnt_al + 1] = add(d[i + 1][cnt_al + 1], mul(d[i][cnt_al], dp[to][1]));
      d[i + 1][cnt_al] = add(d[i + 1][cnt_al], mul(d[i][cnt_al], dp[to][0]));
      d1[i + 1][cnt_al + 1] = add(d1[i + 1][cnt_al + 1], mul(d1[i][cnt_al], dp1[to][1]));
      d1[i + 1][cnt_al] = add(d1[i + 1][cnt_al], mul(d1[i][cnt_al], dp1[to][0]));
    }
  }
  dp[u][0] = dp[u][1] = 0;
  for (int cnt_al = 0; cnt_al <= sz(G[u]); cnt_al++) {
    dp[u][1] = add(dp[u][1], mul(d[sz(G[u])][cnt_al], max(0, cnt_al)));
    dp[u][0] = add(dp[u][0], mul(d[sz(G[u])][cnt_al], sz(G[u]) - cnt_al));
  }
  dp1[u][0] = dp1[u][1] = 0;
  for (int cnt_al = 0; cnt_al <= sz(G[u]); cnt_al++) {
    dp1[u][1] = add(dp1[u][1], mul(d1[sz(G[u])][cnt_al], max(0, cnt_al)));
    dp1[u][0] = add(dp1[u][0], mul(d1[sz(G[u])][cnt_al], sz(G[u]) - cnt_al));
  }
  tout[u] = tim;
}


void calc(int u, int L, int R) {
  umx(L, mn[u]);
  umn(R, mx[u]);

  if (L > mx[u] || R < mn[u] || L > R) return;
  if (L == mn[u] && R == mx[u]) {
    rev[u] ^= 1;
    swap(dp[u], dp1[u]);
    return;
  }
  push(u);
  for (auto to : G[u]) calc(to, L, R);
  
  ve <ve <int>> d(sz(G[u]) + 1);
  ve <ve <int>> d1(sz(G[u]) + 1);

  fe (x, d) x.resize(sz(G[u]) + 1);
  fe (x, d1) x.resize(sz(G[u]) + 1);
  d[0][0] = 1;
  d1[0][0] = 1;
  for (int i = 0; i < sz(G[u]); i++) {
    int to = G[u][i];
    for (int cnt_al = 0; cnt_al <= i; cnt_al++) {
      d[i + 1][cnt_al + 1] = add(d[i + 1][cnt_al + 1], mul(d[i][cnt_al], dp[to][1]));
      d[i + 1][cnt_al] = add(d[i + 1][cnt_al], mul(d[i][cnt_al], dp[to][0]));
      d1[i + 1][cnt_al + 1] = add(d1[i + 1][cnt_al + 1], mul(d1[i][cnt_al], dp1[to][1]));
      d1[i + 1][cnt_al] = add(d1[i + 1][cnt_al], mul(d1[i][cnt_al], dp1[to][0]));
    }
  }
  dp[u][0] = dp[u][1] = 0;
  for (int cnt_al = 0; cnt_al <= sz(G[u]); cnt_al++) {
    dp[u][1] = add(dp[u][1], mul(d[sz(G[u])][cnt_al], max(0, cnt_al)));
    dp[u][0] = add(dp[u][0], mul(d[sz(G[u])][cnt_al], sz(G[u]) - cnt_al));
  }
  dp1[u][0] = dp1[u][1] = 0;
  for (int cnt_al = 0; cnt_al <= sz(G[u]); cnt_al++) {
    dp1[u][1] = add(dp1[u][1], mul(d1[sz(G[u])][cnt_al], max(0, cnt_al)));
    dp1[u][0] = add(dp1[u][0], mul(d1[sz(G[u])][cnt_al], sz(G[u]) - cnt_al));
  }
}


void init(int N, int M, vector<int> P, vector<int> A) {
  n = N;
  m = M;
  for (int i = 0; i < M; i++) a[i + N] = A[i];
  for (int i = 1; i < N + M; i++) G[P[i]].pb(i);
  dfs(0);
}

int count_ways(int L, int R) {
  if (n <= 1000) {
    for (int i = L; i <= R; i++) {
      a[i] = 1 - a[i];
    }
  }
  if (__builtin_popcountll(m) == 1 && n == m - 1) {
    calc(0, L, R);
  } else dfs(0);
  return dp[0][1];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 41 ms 12584 KB Output is correct
4 Correct 40 ms 13044 KB Output is correct
5 Correct 44 ms 13016 KB Output is correct
6 Correct 41 ms 13028 KB Output is correct
7 Correct 41 ms 13012 KB Output is correct
8 Correct 53 ms 13008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5080 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 4 ms 5140 KB Output is correct
8 Correct 4 ms 5072 KB Output is correct
9 Correct 5 ms 5072 KB Output is correct
10 Correct 6 ms 5328 KB Output is correct
11 Correct 5 ms 5328 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 41 ms 12584 KB Output is correct
4 Correct 40 ms 13044 KB Output is correct
5 Correct 44 ms 13016 KB Output is correct
6 Correct 41 ms 13028 KB Output is correct
7 Correct 41 ms 13012 KB Output is correct
8 Correct 53 ms 13008 KB Output is correct
9 Correct 3 ms 5028 KB Output is correct
10 Correct 3 ms 5080 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5140 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 6 ms 5328 KB Output is correct
19 Correct 5 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 4 ms 5072 KB Output is correct
24 Correct 6 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5160 KB Output is correct
27 Correct 4 ms 5056 KB Output is correct
28 Correct 7 ms 5072 KB Output is correct
29 Correct 41 ms 13024 KB Output is correct
30 Correct 41 ms 13032 KB Output is correct
31 Correct 5 ms 5200 KB Output is correct
32 Correct 6 ms 5148 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 10 ms 5492 KB Output is correct
36 Correct 4 ms 5308 KB Output is correct
37 Correct 57 ms 13264 KB Output is correct
38 Correct 45 ms 13216 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 818 ms 8768 KB Output is correct
2 Correct 1385 ms 12696 KB Output is correct
3 Correct 1246 ms 12680 KB Output is correct
4 Correct 1243 ms 12676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 818 ms 8768 KB Output is correct
2 Correct 1385 ms 12696 KB Output is correct
3 Correct 1246 ms 12680 KB Output is correct
4 Correct 1243 ms 12676 KB Output is correct
5 Correct 1118 ms 8800 KB Output is correct
6 Correct 1474 ms 12788 KB Output is correct
7 Correct 1423 ms 12616 KB Output is correct
8 Correct 1225 ms 12616 KB Output is correct
9 Correct 597 ms 5200 KB Output is correct
10 Correct 1087 ms 5524 KB Output is correct
11 Correct 1160 ms 5456 KB Output is correct
12 Correct 1042 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 5080 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 4 ms 5140 KB Output is correct
8 Correct 4 ms 5072 KB Output is correct
9 Correct 5 ms 5072 KB Output is correct
10 Correct 6 ms 5328 KB Output is correct
11 Correct 5 ms 5328 KB Output is correct
12 Correct 4 ms 5072 KB Output is correct
13 Correct 818 ms 8768 KB Output is correct
14 Correct 1385 ms 12696 KB Output is correct
15 Correct 1246 ms 12680 KB Output is correct
16 Correct 1243 ms 12676 KB Output is correct
17 Correct 1118 ms 8800 KB Output is correct
18 Correct 1474 ms 12788 KB Output is correct
19 Correct 1423 ms 12616 KB Output is correct
20 Correct 1225 ms 12616 KB Output is correct
21 Correct 597 ms 5200 KB Output is correct
22 Correct 1087 ms 5524 KB Output is correct
23 Correct 1160 ms 5456 KB Output is correct
24 Correct 1042 ms 5456 KB Output is correct
25 Execution timed out 3094 ms 16556 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 41 ms 12584 KB Output is correct
4 Correct 40 ms 13044 KB Output is correct
5 Correct 44 ms 13016 KB Output is correct
6 Correct 41 ms 13028 KB Output is correct
7 Correct 41 ms 13012 KB Output is correct
8 Correct 53 ms 13008 KB Output is correct
9 Correct 3 ms 5028 KB Output is correct
10 Correct 3 ms 5080 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5140 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 6 ms 5328 KB Output is correct
19 Correct 5 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 4 ms 5072 KB Output is correct
24 Correct 6 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5160 KB Output is correct
27 Correct 4 ms 5056 KB Output is correct
28 Correct 7 ms 5072 KB Output is correct
29 Correct 41 ms 13024 KB Output is correct
30 Correct 41 ms 13032 KB Output is correct
31 Correct 5 ms 5200 KB Output is correct
32 Correct 6 ms 5148 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 10 ms 5492 KB Output is correct
36 Correct 4 ms 5308 KB Output is correct
37 Correct 57 ms 13264 KB Output is correct
38 Correct 45 ms 13216 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
43 Execution timed out 3078 ms 5328 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 41 ms 12584 KB Output is correct
4 Correct 40 ms 13044 KB Output is correct
5 Correct 44 ms 13016 KB Output is correct
6 Correct 41 ms 13028 KB Output is correct
7 Correct 41 ms 13012 KB Output is correct
8 Correct 53 ms 13008 KB Output is correct
9 Correct 3 ms 5028 KB Output is correct
10 Correct 3 ms 5080 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 5072 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 4 ms 5140 KB Output is correct
16 Correct 4 ms 5072 KB Output is correct
17 Correct 5 ms 5072 KB Output is correct
18 Correct 6 ms 5328 KB Output is correct
19 Correct 5 ms 5328 KB Output is correct
20 Correct 4 ms 5072 KB Output is correct
21 Correct 4 ms 5072 KB Output is correct
22 Correct 3 ms 5072 KB Output is correct
23 Correct 4 ms 5072 KB Output is correct
24 Correct 6 ms 5072 KB Output is correct
25 Correct 5 ms 5072 KB Output is correct
26 Correct 4 ms 5160 KB Output is correct
27 Correct 4 ms 5056 KB Output is correct
28 Correct 7 ms 5072 KB Output is correct
29 Correct 41 ms 13024 KB Output is correct
30 Correct 41 ms 13032 KB Output is correct
31 Correct 5 ms 5200 KB Output is correct
32 Correct 6 ms 5148 KB Output is correct
33 Correct 4 ms 5072 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 10 ms 5492 KB Output is correct
36 Correct 4 ms 5308 KB Output is correct
37 Correct 57 ms 13264 KB Output is correct
38 Correct 45 ms 13216 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 3 ms 5072 KB Output is correct
41 Correct 3 ms 5072 KB Output is correct
42 Correct 4 ms 5072 KB Output is correct
43 Correct 818 ms 8768 KB Output is correct
44 Correct 1385 ms 12696 KB Output is correct
45 Correct 1246 ms 12680 KB Output is correct
46 Correct 1243 ms 12676 KB Output is correct
47 Correct 1118 ms 8800 KB Output is correct
48 Correct 1474 ms 12788 KB Output is correct
49 Correct 1423 ms 12616 KB Output is correct
50 Correct 1225 ms 12616 KB Output is correct
51 Correct 597 ms 5200 KB Output is correct
52 Correct 1087 ms 5524 KB Output is correct
53 Correct 1160 ms 5456 KB Output is correct
54 Correct 1042 ms 5456 KB Output is correct
55 Execution timed out 3094 ms 16556 KB Time limit exceeded
56 Halted 0 ms 0 KB -