Submission #630163

# Submission time Handle Problem Language Result Execution time Memory
630163 2022-08-15T19:11:21 Z CyanForces Digital Circuit (IOI22_circuit) C++17
100 / 100
1127 ms 42520 KB
#include "circuit.h"

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

vi a;
int n,m;
vector<ll> contrib;
vector<ll> contrib_pref;
const ll mod = ll(1e9) + 2022;


//from atcoder

int ceil_pow2(int n) {
  int x = 0;
  while ((1U << x) < (unsigned int)(n)) x++;
  return x;
}

template <class S,
         S (*op)(S, S),
         S (*e)(),
         class F,
         S (*mapping)(F, S),
         F (*composition)(F, F),
         F (*id)()>
         struct lazy_segtree {
           public:
             lazy_segtree() : lazy_segtree(0) {}
             lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
             lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
               log = ceil_pow2(_n);
               size = 1 << log;
               d = std::vector<S>(2 * size, e());
               lz = std::vector<F>(size, id());
               for (int i = 0; i < _n; i++) d[size + i] = v[i];
               for (int i = size - 1; i >= 1; i--) {
                 update(i);
               }
             }

             void set(int p, S x) {
               assert(0 <= p && p < _n);
               p += size;
               for (int i = log; i >= 1; i--) push(p >> i);
               d[p] = x;
               for (int i = 1; i <= log; i++) update(p >> i);
             }

             S get(int p) {
               assert(0 <= p && p < _n);
               p += size;
               for (int i = log; i >= 1; i--) push(p >> i);
               return d[p];
             }

             S prod(int l, int r) {
               assert(0 <= l && l <= r && r <= _n);
               if (l == r) return e();

               l += size;
               r += size;

               for (int i = log; i >= 1; i--) {
                 if (((l >> i) << i) != l) push(l >> i);
                 if (((r >> i) << i) != r) push(r >> i);
               }

               S sml = e(), smr = e();
               while (l < r) {
                 if (l & 1) sml = op(sml, d[l++]);
                 if (r & 1) smr = op(d[--r], smr);
                 l >>= 1;
                 r >>= 1;
               }

               return op(sml, smr);
             }

             S all_prod() { return d[1]; }

             void apply(int p, F f) {
               assert(0 <= p && p < _n);
               p += size;
               for (int i = log; i >= 1; i--) push(p >> i);
               d[p] = mapping(f, d[p]);
               for (int i = 1; i <= log; i++) update(p >> i);
             }
             void apply(int l, int r, F f) {
               assert(0 <= l && l <= r && r <= _n);
               if (l == r) return;

               l += size;
               r += size;

               for (int i = log; i >= 1; i--) {
                 if (((l >> i) << i) != l) push(l >> i);
                 if (((r >> i) << i) != r) push((r - 1) >> i);
               }

               {
                 int l2 = l, r2 = r;
                 while (l < r) {
                   if (l & 1) all_apply(l++, f);
                   if (r & 1) all_apply(--r, f);
                   l >>= 1;
                   r >>= 1;
                 }
                 l = l2;
                 r = r2;
               }

               for (int i = 1; i <= log; i++) {
                 if (((l >> i) << i) != l) update(l >> i);
                 if (((r >> i) << i) != r) update((r - 1) >> i);
               }
             }

             template <bool (*g)(S)> int max_right(int l) {
               return max_right(l, [](S x) { return g(x); });
             }
             template <class G> int max_right(int l, G g) {
               assert(0 <= l && l <= _n);
               assert(g(e()));
               if (l == _n) return _n;
               l += size;
               for (int i = log; i >= 1; i--) push(l >> i);
               S sm = e();
               do {
                 while (l % 2 == 0) l >>= 1;
                 if (!g(op(sm, d[l]))) {
                   while (l < size) {
                     push(l);
                     l = (2 * l);
                     if (g(op(sm, d[l]))) {
                       sm = op(sm, d[l]);
                       l++;
                     }
                   }
                   return l - size;
                 }
                 sm = op(sm, d[l]);
                 l++;
               } while ((l & -l) != l);
               return _n;
             }

             template <bool (*g)(S)> int min_left(int r) {
               return min_left(r, [](S x) { return g(x); });
             }
             template <class G> int min_left(int r, G g) {
               assert(0 <= r && r <= _n);
               assert(g(e()));
               if (r == 0) return 0;
               r += size;
               for (int i = log; i >= 1; i--) push((r - 1) >> i);
               S sm = e();
               do {
                 r--;
                 while (r > 1 && (r % 2)) r >>= 1;
                 if (!g(op(d[r], sm))) {
                   while (r < size) {
                     push(r);
                     r = (2 * r + 1);
                     if (g(op(d[r], sm))) {
                       sm = op(d[r], sm);
                       r--;
                     }
                   }
                   return r + 1 - size;
                 }
                 sm = op(d[r], sm);
               } while ((r & -r) != r);
               return 0;
             }

           private:
             int _n, size, log;
             std::vector<S> d;
             std::vector<F> lz;

             void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
             void all_apply(int k, F f) {
               d[k] = mapping(f, d[k]);
               if (k < size) lz[k] = composition(f, lz[k]);
             }
             void push(int k) {
               all_apply(2 * k, lz[k]);
               all_apply(2 * k + 1, lz[k]);
               lz[k] = id();
             }
         };


struct S { ll on, off; };
S op(S a, S b) { return S{a.on + b.on, a.off + b.off}; }
S e() { return S{0,0}; }

using F = bool;
F id() { return 0; }
F composition(F f, F g) { return f^g; }
S mapping(F f, S s) { return f ? S{s.off, s.on} : s; }

using tree = lazy_segtree<S,op,e,F,mapping,composition,id>;

unique_ptr<tree> t;

void init(int N, int M, std::vector<int> P, std::vector<int> A) {
  n = N, m = M;
  contrib.assign(m,0);
  a = A;

  vector<vi> g(n+m);
  rep(i,1,n+m) g[P[i]].emplace_back(i);



  vector<ll> ways(n);

  function<void(int,int)> dfs = [&](int x, int p) {
    ways[x] = sz(g[x]);
    for(int y : g[x]) if(y != p && y < n) {
      dfs(y,x);
      ways[x] = ways[x] * ways[y] % mod;
    }
  };
  dfs(0,-1);


  function<void(int,int,ll)> go = [&](int x, int p, ll prod) {
    if(x >= n) {
      contrib[x-n] = prod;
      return;
    }
    vector<ll> c;
    for(int y : g[x]) if(y != p && y < n) {
      c.emplace_back(ways[y]);
    }
    vector<ll> pref(sz(c)+1,1), suf(sz(c)+1,1);
    rep(i,0,sz(c)) pref[i+1] = pref[i] * c[i] % mod;
    rep(i,0,sz(c)) suf[i+1] = suf[i] * c[sz(c)-i-1] % mod;
    int before = 0, after = sz(c);
    for(int y : g[x]) if(y != p) {
      if(y < n) --after;
      ll q = pref[before] * suf[after] % mod;
      go(y, x, q * prod % mod);
      if(y < n) ++before;
    }
    assert(after == 0 && before == sz(c));
  };
  go(0,-1,1);

  t = make_unique<tree>(m);
  rep(i,0,m) t->set(i,S{0,contrib[i]});
  rep(i,0,m) if(a[i]) t->apply(i,true);
}

int count_ways(int L, int R) {
  int l = L - n;
  int r = R - n + 1;
  t->apply(l,r,true);
  ll ans = t->all_prod().on;
  return int(ans % mod);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 2 ms 412 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 472 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 2 ms 600 KB Output is correct
39 Correct 1 ms 344 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 537 ms 5816 KB Output is correct
2 Correct 1022 ms 11496 KB Output is correct
3 Correct 1047 ms 11336 KB Output is correct
4 Correct 994 ms 11332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 537 ms 5816 KB Output is correct
2 Correct 1022 ms 11496 KB Output is correct
3 Correct 1047 ms 11336 KB Output is correct
4 Correct 994 ms 11332 KB Output is correct
5 Correct 671 ms 5772 KB Output is correct
6 Correct 1127 ms 11304 KB Output is correct
7 Correct 893 ms 11304 KB Output is correct
8 Correct 956 ms 11284 KB Output is correct
9 Correct 334 ms 592 KB Output is correct
10 Correct 924 ms 976 KB Output is correct
11 Correct 797 ms 948 KB Output is correct
12 Correct 694 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 537 ms 5816 KB Output is correct
14 Correct 1022 ms 11496 KB Output is correct
15 Correct 1047 ms 11336 KB Output is correct
16 Correct 994 ms 11332 KB Output is correct
17 Correct 671 ms 5772 KB Output is correct
18 Correct 1127 ms 11304 KB Output is correct
19 Correct 893 ms 11304 KB Output is correct
20 Correct 956 ms 11284 KB Output is correct
21 Correct 334 ms 592 KB Output is correct
22 Correct 924 ms 976 KB Output is correct
23 Correct 797 ms 948 KB Output is correct
24 Correct 694 ms 976 KB Output is correct
25 Correct 998 ms 17852 KB Output is correct
26 Correct 914 ms 18120 KB Output is correct
27 Correct 1004 ms 18100 KB Output is correct
28 Correct 779 ms 18104 KB Output is correct
29 Correct 986 ms 42056 KB Output is correct
30 Correct 1025 ms 42056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 2 ms 412 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 472 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 2 ms 600 KB Output is correct
39 Correct 1 ms 344 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 593 ms 720 KB Output is correct
44 Correct 845 ms 848 KB Output is correct
45 Correct 927 ms 860 KB Output is correct
46 Correct 795 ms 1232 KB Output is correct
47 Correct 939 ms 1232 KB Output is correct
48 Correct 829 ms 1232 KB Output is correct
49 Correct 909 ms 1232 KB Output is correct
50 Correct 974 ms 1232 KB Output is correct
51 Correct 805 ms 848 KB Output is correct
52 Correct 823 ms 848 KB Output is correct
53 Correct 805 ms 2128 KB Output is correct
54 Correct 939 ms 1232 KB Output is correct
55 Correct 892 ms 976 KB Output is correct
56 Correct 917 ms 976 KB Output is correct
57 Correct 755 ms 848 KB Output is correct
58 Correct 786 ms 2384 KB Output is correct
59 Correct 888 ms 2384 KB Output is correct
60 Correct 954 ms 2384 KB Output is correct
61 Correct 582 ms 1104 KB Output is correct
62 Correct 901 ms 720 KB Output is correct
63 Correct 905 ms 720 KB Output is correct
64 Correct 916 ms 848 KB Output is correct
65 Correct 454 ms 592 KB Output is correct
66 Correct 851 ms 976 KB Output is correct
67 Correct 725 ms 976 KB Output is correct
68 Correct 763 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 472 KB Output is correct
28 Correct 2 ms 412 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 472 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 2 ms 600 KB Output is correct
39 Correct 1 ms 344 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 537 ms 5816 KB Output is correct
44 Correct 1022 ms 11496 KB Output is correct
45 Correct 1047 ms 11336 KB Output is correct
46 Correct 994 ms 11332 KB Output is correct
47 Correct 671 ms 5772 KB Output is correct
48 Correct 1127 ms 11304 KB Output is correct
49 Correct 893 ms 11304 KB Output is correct
50 Correct 956 ms 11284 KB Output is correct
51 Correct 334 ms 592 KB Output is correct
52 Correct 924 ms 976 KB Output is correct
53 Correct 797 ms 948 KB Output is correct
54 Correct 694 ms 976 KB Output is correct
55 Correct 998 ms 17852 KB Output is correct
56 Correct 914 ms 18120 KB Output is correct
57 Correct 1004 ms 18100 KB Output is correct
58 Correct 779 ms 18104 KB Output is correct
59 Correct 986 ms 42056 KB Output is correct
60 Correct 1025 ms 42056 KB Output is correct
61 Correct 593 ms 720 KB Output is correct
62 Correct 845 ms 848 KB Output is correct
63 Correct 927 ms 860 KB Output is correct
64 Correct 795 ms 1232 KB Output is correct
65 Correct 939 ms 1232 KB Output is correct
66 Correct 829 ms 1232 KB Output is correct
67 Correct 909 ms 1232 KB Output is correct
68 Correct 974 ms 1232 KB Output is correct
69 Correct 805 ms 848 KB Output is correct
70 Correct 823 ms 848 KB Output is correct
71 Correct 805 ms 2128 KB Output is correct
72 Correct 939 ms 1232 KB Output is correct
73 Correct 892 ms 976 KB Output is correct
74 Correct 917 ms 976 KB Output is correct
75 Correct 755 ms 848 KB Output is correct
76 Correct 786 ms 2384 KB Output is correct
77 Correct 888 ms 2384 KB Output is correct
78 Correct 954 ms 2384 KB Output is correct
79 Correct 582 ms 1104 KB Output is correct
80 Correct 901 ms 720 KB Output is correct
81 Correct 905 ms 720 KB Output is correct
82 Correct 916 ms 848 KB Output is correct
83 Correct 454 ms 592 KB Output is correct
84 Correct 851 ms 976 KB Output is correct
85 Correct 725 ms 976 KB Output is correct
86 Correct 763 ms 976 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 633 ms 16524 KB Output is correct
89 Correct 1041 ms 11168 KB Output is correct
90 Correct 864 ms 10932 KB Output is correct
91 Correct 786 ms 18248 KB Output is correct
92 Correct 1020 ms 18120 KB Output is correct
93 Correct 795 ms 18200 KB Output is correct
94 Correct 940 ms 18204 KB Output is correct
95 Correct 921 ms 18208 KB Output is correct
96 Correct 719 ms 11568 KB Output is correct
97 Correct 905 ms 11592 KB Output is correct
98 Correct 933 ms 36976 KB Output is correct
99 Correct 998 ms 18120 KB Output is correct
100 Correct 1028 ms 14664 KB Output is correct
101 Correct 827 ms 13256 KB Output is correct
102 Correct 857 ms 11464 KB Output is correct
103 Correct 908 ms 42168 KB Output is correct
104 Correct 793 ms 42472 KB Output is correct
105 Correct 928 ms 42520 KB Output is correct
106 Correct 1002 ms 16352 KB Output is correct
107 Correct 655 ms 11720 KB Output is correct
108 Correct 938 ms 11720 KB Output is correct
109 Correct 1012 ms 11600 KB Output is correct