Submission #1079169

# Submission time Handle Problem Language Result Execution time Memory
1079169 2024-08-28T11:31:23 Z Boas Digital Circuit (IOI22_circuit) C++17
62 / 100
3000 ms 47440 KB
#include "circuit.h"

// #pragma GCC optimize("trapv")

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define pb push_back
#define loop(x, i) for (int i = 0; i < x; i++)
#define rev(x, i) for (int i = (int)x - 1; i >= 0; i--)
#define ALL(x) begin(x), end(x)
#define sz(x) (int)x.size()

typedef signed i32;
typedef array<int, 2> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<i32> vi32;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;

constexpr int MOD = 1'000'002'022;

vvi children;
vi term01;
int n, m, treeN = 1;
vi vs;

struct val
{
  int v = 0, totalV = 0;
  void swap()
  {
    v = (totalV - v + MOD) % MOD;
  }
  val operator+(const val &b)
  {
    val a = *this, res;
    res.v = a.v + b.v;
    res.totalV = a.totalV + b.totalV;
    res.totalV %= MOD;
    res.v %= MOD;
    return res;
  }
};

vector<val> tree;
vb lazy;

void pull(int i)
{
  tree[i] = tree[2 * i] + tree[2 * i + 1];
}

void push(int i)
{
  if (!lazy[i])
    return;
  tree[i].swap();
  if (i < treeN)
  {
    lazy[2 * i].flip();
    lazy[2 * i + 1].flip();
  }
  lazy[i] = 0;
}

val operation(int i, int l, int r, int ql, int qr, int op)
{
  push(i);
  if (r < ql || qr < l)
    return {};
  if (ql <= l && r <= qr)
  {
    if (op == 1)
    {
      lazy[i] = 1;
      push(i);
    }
    return tree[i];
  }
  int mid = (l + r) / 2;
  val res = operation(2 * i, l, mid, ql, qr, op) +
            operation(2 * i + 1, mid + 1, r, ql, qr, op);
  pull(i);
  return res;
};

int calc01s(int i)
{
  if (i >= n)
  {
    return term01[i] = 1;
  }
  int all = sz(children[i]);
  for (int j : children[i])
  {
    int v = calc01s(j);
    all *= v;
    all %= MOD;
  }
  return term01[i] = all;
}

vi calc(int i)
{
  if (i >= n)
  {
    vs[i - n] = 1;
    return {i - n};
  }
  int c = sz(children[i]);
  vi prod, prefProd(c + 1, 1), sufProd(c + 1, 1);
  vvi leafs;
  for (int j : children[i])
  {
    leafs.pb(move(calc(j)));
    prod.pb(term01[j]);
  }
  loop(c, j)
  {
    prefProd[j + 1] = prefProd[j] * prod[j];
    prefProd[j + 1] %= MOD;
  }
  rev(c, j)
  {
    sufProd[j] = sufProd[j + 1] * prod[j];
    sufProd[j] %= MOD;
  }
  vi res;
  int largest = 0;
  loop(c, j)
  {
    if (sz(leafs[j]) > sz(leafs[largest]))
    {
      largest = j;
    }
  }
  res = leafs[largest];
  loop(c, j)
  {
    int mult = (prefProd[j] * sufProd[j + 1]) % MOD;
    for (int ix : leafs[j])
    {
      vs[ix] = (vs[ix] * mult) % MOD;
      if (j != largest)
        res.pb(ix);
    }
  }
  // return res;
  return move(res);
}

void init(i32 N, i32 M, vi32 P, vi32 A)
{
  n = N;
  m = M;
  while (treeN < m)
    treeN *= 2;
  tree.resize(2 * treeN);
  lazy.resize(2 * treeN);
  children = vvi(N);
  term01 = vi(N + M);
  vs = vi(M);
  loop(sz(P), i)
  {
    if (i == 0)
      continue;
    children[P[i]].pb(i);
  }
  calc01s(0);
  calc(0);
  loop(m, i)
  {
    tree[treeN + i].totalV = vs[i];
    if (A[i])
      tree[treeN + i].v = vs[i];
  }
  rev(treeN, i) pull(i);
}

i32 count_ways(i32 L, i32 R)
{
  operation(1, 0, treeN - 1, L - n, R - n, 1);
  return (i32)operation(1, 0, treeN - 1, 0, m - 1, 0).v;
}

Compilation message

circuit.cpp: In function 'vi calc(long long int)':
circuit.cpp:155:14: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
  155 |   return move(res);
      |          ~~~~^~~~~
circuit.cpp:155:14: note: remove 'std::move' call
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 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 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 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 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 1 ms 344 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 2 ms 856 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 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 1 ms 344 KB Output is correct
6 Correct 1 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 0 ms 344 KB Output is correct
10 Correct 0 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 1 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 3 ms 856 KB Output is correct
19 Correct 2 ms 856 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 432 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 856 KB Output is correct
38 Correct 3 ms 856 KB Output is correct
39 Correct 1 ms 600 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
# Verdict Execution time Memory Grader output
1 Correct 424 ms 5460 KB Output is correct
2 Correct 720 ms 10344 KB Output is correct
3 Correct 718 ms 10232 KB Output is correct
4 Correct 709 ms 10232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 424 ms 5460 KB Output is correct
2 Correct 720 ms 10344 KB Output is correct
3 Correct 718 ms 10232 KB Output is correct
4 Correct 709 ms 10232 KB Output is correct
5 Correct 649 ms 5464 KB Output is correct
6 Correct 726 ms 10224 KB Output is correct
7 Correct 768 ms 10300 KB Output is correct
8 Correct 743 ms 10224 KB Output is correct
9 Correct 330 ms 600 KB Output is correct
10 Correct 657 ms 856 KB Output is correct
11 Correct 753 ms 856 KB Output is correct
12 Correct 670 ms 856 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 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 1 ms 344 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 2 ms 856 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 424 ms 5460 KB Output is correct
14 Correct 720 ms 10344 KB Output is correct
15 Correct 718 ms 10232 KB Output is correct
16 Correct 709 ms 10232 KB Output is correct
17 Correct 649 ms 5464 KB Output is correct
18 Correct 726 ms 10224 KB Output is correct
19 Correct 768 ms 10300 KB Output is correct
20 Correct 743 ms 10224 KB Output is correct
21 Correct 330 ms 600 KB Output is correct
22 Correct 657 ms 856 KB Output is correct
23 Correct 753 ms 856 KB Output is correct
24 Correct 670 ms 856 KB Output is correct
25 Correct 779 ms 16476 KB Output is correct
26 Correct 774 ms 17244 KB Output is correct
27 Correct 829 ms 17424 KB Output is correct
28 Correct 702 ms 17196 KB Output is correct
29 Execution timed out 3055 ms 47440 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 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 1 ms 344 KB Output is correct
6 Correct 1 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 0 ms 344 KB Output is correct
10 Correct 0 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 1 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 3 ms 856 KB Output is correct
19 Correct 2 ms 856 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 432 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 856 KB Output is correct
38 Correct 3 ms 856 KB Output is correct
39 Correct 1 ms 600 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 480 ms 856 KB Output is correct
44 Correct 696 ms 856 KB Output is correct
45 Correct 733 ms 856 KB Output is correct
46 Correct 670 ms 1368 KB Output is correct
47 Correct 674 ms 1368 KB Output is correct
48 Correct 660 ms 1368 KB Output is correct
49 Correct 690 ms 1368 KB Output is correct
50 Correct 668 ms 1352 KB Output is correct
51 Correct 703 ms 1368 KB Output is correct
52 Correct 687 ms 1620 KB Output is correct
53 Correct 596 ms 2392 KB Output is correct
54 Correct 680 ms 1368 KB Output is correct
55 Correct 724 ms 1112 KB Output is correct
56 Correct 709 ms 1112 KB Output is correct
57 Correct 731 ms 856 KB Output is correct
58 Correct 744 ms 2648 KB Output is correct
59 Correct 732 ms 3416 KB Output is correct
60 Correct 744 ms 3416 KB Output is correct
61 Correct 659 ms 1368 KB Output is correct
62 Correct 602 ms 856 KB Output is correct
63 Correct 568 ms 928 KB Output is correct
64 Correct 645 ms 1112 KB Output is correct
65 Correct 316 ms 600 KB Output is correct
66 Correct 693 ms 1108 KB Output is correct
67 Correct 693 ms 856 KB Output is correct
68 Correct 660 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 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 1 ms 344 KB Output is correct
6 Correct 1 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 0 ms 344 KB Output is correct
10 Correct 0 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 1 ms 344 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 3 ms 856 KB Output is correct
19 Correct 2 ms 856 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 432 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 856 KB Output is correct
38 Correct 3 ms 856 KB Output is correct
39 Correct 1 ms 600 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 424 ms 5460 KB Output is correct
44 Correct 720 ms 10344 KB Output is correct
45 Correct 718 ms 10232 KB Output is correct
46 Correct 709 ms 10232 KB Output is correct
47 Correct 649 ms 5464 KB Output is correct
48 Correct 726 ms 10224 KB Output is correct
49 Correct 768 ms 10300 KB Output is correct
50 Correct 743 ms 10224 KB Output is correct
51 Correct 330 ms 600 KB Output is correct
52 Correct 657 ms 856 KB Output is correct
53 Correct 753 ms 856 KB Output is correct
54 Correct 670 ms 856 KB Output is correct
55 Correct 779 ms 16476 KB Output is correct
56 Correct 774 ms 17244 KB Output is correct
57 Correct 829 ms 17424 KB Output is correct
58 Correct 702 ms 17196 KB Output is correct
59 Execution timed out 3055 ms 47440 KB Time limit exceeded
60 Halted 0 ms 0 KB -