답안 #1057315

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057315 2024-08-13T17:08:42 Z esomer 디지털 회로 (IOI22_circuit) C++17
100 / 100
679 ms 38012 KB
#include "circuit.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1000002022;

struct segTree{
  vector<pair<ll, ll>> v;
  vector<int> upd;
  int sz;
  void init(int n){
    sz = 1;
    while(sz < n) sz *= 2;
    v.assign(2 * sz, {0, 0});
    upd.assign(2 * sz, 0);
  }
  void set(int i, pair<ll, ll> p, int x, int lx, int rx){
    if(rx - lx == 1){
      v[x] = p;
      return;
    }
    int m = (lx + rx) / 2;
    if(i < m) set(i, p, 2 * x + 1, lx, m);
    else set(i, p, 2 * x + 2, m, rx);
    v[x].first = (v[2 * x + 1].first + v[2 * x + 2].first) % MOD;
    v[x].second = (v[2 * x + 1].second + v[2 * x + 2].second) % MOD;
  }
  void set(int i, pair<ll, ll> p){
    set(i, p, 0, 0, sz);
  }
  void push(int x){
    if(upd[x]){
      upd[2 * x + 1] ^= 1;
      swap(v[2 * x + 1].first, v[2 *  x + 1].second);
      upd[2 * x + 2] ^= 1;
      swap(v[2 * x + 2].first, v[2 *  x + 2].second);
      upd[x] = 0;
    }
  }
  void change(int l, int r, int x, int lx, int rx){
    if(lx >= l && rx <= r){
      swap(v[x].first, v[x].second);
      upd[x] ^= 1;
      return;
    }else if(lx >= r || rx <= l) return;
    int m = (lx + rx) / 2;
    push(x);
    change(l, r, 2 * x + 1, lx, m);
    change(l, r, 2 * x + 2, m, rx);
    v[x].first = (v[2 * x + 1].first + v[2 * x + 2].first) % MOD;
    v[x].second = (v[2 * x + 1].second + v[2 * x + 2].second) % MOD;
  }
  void change(int l, int r){
    change(l, r, 0, 0, sz);
  }
};

void getSub(int x, vector<vector<int>>& adj, vector<ll>& subtree){
  ll opt = (int)adj[x].size();
  subtree[x] = opt;
  for(int node : adj[x]){
    getSub(node, adj, subtree);
    if(!adj[node].empty()){
      subtree[x] *= subtree[node];
      subtree[x] %= MOD;
    }
  }
  // cout << "x " << x << " subtree " << subtree[x] << endl;
}

void DFS(int x, vector<vector<int>>& adj, vector<ll>& subtree, vector<ll>& dp, ll curr = 1){
  int sz = (int)adj[x].size();
  dp[x] = curr;
  if(!sz) return;
  vector<ll> suf(sz+1, 1);
  for(int i = sz - 1; i >= 0; i--){
    suf[i] = suf[i+1] * max(subtree[adj[x][i]], 1ll);
    suf[i] %= MOD;
  }
  ll nw = 1;
  for(int i = 0; i < sz; i++){
    ll all = nw * suf[i+1]; all %= MOD;
    DFS(adj[x][i], adj, subtree, dp, (curr * all) % MOD);
    nw *= max(subtree[adj[x][i]], 1ll); nw %= MOD;
  }
}

segTree st; 

void init(int N, int M, vector<int> P, vector<int> A) {
  vector<vector<int>> adj(N+M);
  for(int i = 1; i < N + M; i++){
    adj[P[i]].push_back(i);
  }
  vector<ll> subtree(N+M);
  getSub(0, adj, subtree);
  vector<ll> dp(N+M, 0);
  DFS(0, adj, subtree, dp);
  st.init(N+M);
  for(int i = N; i < N+M; i++){
    // cout << "i " << i << " dp " << dp[i] << "\n";
    pair<ll, ll> p = {dp[i], 0};
    if(!A[i-N]) swap(p.first, p.second);
    st.set(i, p);
  }
}

int count_ways(int L, int R) {
  st.change(L, R + 1);
  return st.v[0].first;
}

// int n;
// vector<vector<int>> Adj;
// vector<int> A;

// pair<ll, ll> getAns(int x){
//   int sz = (int)Adj[x].size();
//   if(!sz){
//     if(A[x-n]) return {1, 0};
//     else return {0, 1};
//   }
//   vector<ll> ways(sz + 1, 0); ways[0] = 1;
//   for(int node : Adj[x]){
//     pair<ll, ll> w = getAns(node);
//     for(int i = sz; i >= 0; i--){
//       if(i+1 <= sz) ways[i+1] = (ways[i+1] + ways[i] * w.first) % MOD;
//       ways[i] = (ways[i] * w.second) % MOD;
//     }
//   }
//   // cout << "x " << x << endl;
//   // for(int y : ways) cout << y << " ";
//   // cout << endl;
//   vector<ll> suf(sz+1, 0), pre(sz + 1, 0);
//   pre[0] = ways[0];
//   for(int i = 1; i <= sz; i++) pre[i] = (pre[i-1] + ways[i]) % MOD;
//   suf[sz] = ways[sz];
//   for(int i = sz - 1; i >= 0; i--) suf[i] = (suf[i+1] + ways[i]) % MOD;
//   ll good = 0, bad = 0;
//   for(int c = 1; c <= sz; c++){
//     good += suf[c];
//     bad += pre[c-1];
//     good %= MOD; bad %= MOD;
//   }
//   // cout << "x " << x << " good " << good << " bad " << bad << "\n";
//   return {good, bad};
// }

// void init2(int N, int M, vector<int> P, vector<int> _A) {
//   n = N;
//   Adj.clear();
//   Adj.resize(N+M);
//   for(int i = 1; i < N + M; i++){
//     Adj[P[i]].push_back(i);
//   }
//   A = _A;
// }

// int count_ways2(int L, int R) {
//   for(int i = L-n; i <= R-n; i++) A[i] ^= 1;
//   return (int)(getAns(0).first);
// }

// mt19937 gen(time(0));

// void eras(vector<int>& lft, int elem){
//   vector<int> nw;
//   for(int x : lft){
//     if(x != elem) nw.push_back(x);
//   }
//   lft = nw;
// }

// int main(){
//   for(int h = 0; h < 10000; h++){    
//     int N = gen() % 5 + 1;
//     int M = gen() % 5 + N;
//     vector<int> P(N+M);
//     P[0] = -1;
//     vector<int> lft(N);
//     for(int i = 0; i < N; i++) lft[i] = i;
//     for(int i = 1; i < N; i++){
//       P[i] = gen() % min(i, N);
//       eras(lft, P[i]);
//     }
//     for(int i = 0; i < M; i++){
//       if(lft.empty()) P[i+N] = gen() % N;
//       else{
//         int ind = gen() % (int)(lft.size());
//         P[i+N] = lft[ind];
//         eras(lft, P[i+N]);
//       }
//     }
//     vector<int> A(M);
//     for(int i = 0; i < M; i++){
//       A[i] = gen() % 2;
//     }
//     init(N, M, P, A);
//     init2(N, M, P, A);
//     vector<pair<int, int>> queries;
//     int Q = gen() % 2 + 1;
//     bool bad = false;
//     while(Q--){
//       int R = gen() % M + 1;
//       int L = gen() % R + 1;
//       L--; R--; L += N; R += N;
//       queries.push_back({L, R});
//       if(count_ways(L, R) != count_ways2(L, R)){
//         bad = true;
//         break;
//       }
//     }
//     if(bad){
//       cout << N << " " << M << " " << (int)queries.size() << endl;
//       for(int x : P) cout << x << " "; cout << endl;
//       for(int x : A) cout << x << " "; cout << endl;
//       for(pair<int, int> p : queries) cout << p.first << " " << p.second << endl;
//       return 0;
//     }
//   }
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 624 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 624 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 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 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 600 KB Output is correct
37 Correct 0 ms 600 KB Output is correct
38 Correct 0 ms 600 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 7256 KB Output is correct
2 Correct 544 ms 14168 KB Output is correct
3 Correct 575 ms 14168 KB Output is correct
4 Correct 620 ms 14168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 7256 KB Output is correct
2 Correct 544 ms 14168 KB Output is correct
3 Correct 575 ms 14168 KB Output is correct
4 Correct 620 ms 14168 KB Output is correct
5 Correct 430 ms 7256 KB Output is correct
6 Correct 590 ms 14168 KB Output is correct
7 Correct 560 ms 14156 KB Output is correct
8 Correct 546 ms 14268 KB Output is correct
9 Correct 305 ms 856 KB Output is correct
10 Correct 505 ms 1112 KB Output is correct
11 Correct 605 ms 1112 KB Output is correct
12 Correct 567 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 624 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 371 ms 7256 KB Output is correct
14 Correct 544 ms 14168 KB Output is correct
15 Correct 575 ms 14168 KB Output is correct
16 Correct 620 ms 14168 KB Output is correct
17 Correct 430 ms 7256 KB Output is correct
18 Correct 590 ms 14168 KB Output is correct
19 Correct 560 ms 14156 KB Output is correct
20 Correct 546 ms 14268 KB Output is correct
21 Correct 305 ms 856 KB Output is correct
22 Correct 505 ms 1112 KB Output is correct
23 Correct 605 ms 1112 KB Output is correct
24 Correct 567 ms 1112 KB Output is correct
25 Correct 589 ms 23632 KB Output is correct
26 Correct 620 ms 23888 KB Output is correct
27 Correct 611 ms 23996 KB Output is correct
28 Correct 498 ms 23888 KB Output is correct
29 Correct 669 ms 37464 KB Output is correct
30 Correct 627 ms 37592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 624 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 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 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 600 KB Output is correct
37 Correct 0 ms 600 KB Output is correct
38 Correct 0 ms 600 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 384 ms 1112 KB Output is correct
44 Correct 549 ms 1112 KB Output is correct
45 Correct 553 ms 1112 KB Output is correct
46 Correct 524 ms 1876 KB Output is correct
47 Correct 526 ms 1624 KB Output is correct
48 Correct 525 ms 1624 KB Output is correct
49 Correct 549 ms 1624 KB Output is correct
50 Correct 528 ms 1624 KB Output is correct
51 Correct 535 ms 1076 KB Output is correct
52 Correct 535 ms 1108 KB Output is correct
53 Correct 518 ms 1624 KB Output is correct
54 Correct 578 ms 1624 KB Output is correct
55 Correct 536 ms 1112 KB Output is correct
56 Correct 616 ms 1108 KB Output is correct
57 Correct 560 ms 1040 KB Output is correct
58 Correct 481 ms 2392 KB Output is correct
59 Correct 505 ms 2392 KB Output is correct
60 Correct 515 ms 2392 KB Output is correct
61 Correct 538 ms 1368 KB Output is correct
62 Correct 526 ms 1112 KB Output is correct
63 Correct 585 ms 1080 KB Output is correct
64 Correct 558 ms 856 KB Output is correct
65 Correct 252 ms 864 KB Output is correct
66 Correct 562 ms 1112 KB Output is correct
67 Correct 541 ms 1112 KB Output is correct
68 Correct 520 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 600 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 624 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 600 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 0 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 0 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 600 KB Output is correct
37 Correct 0 ms 600 KB Output is correct
38 Correct 0 ms 600 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 371 ms 7256 KB Output is correct
44 Correct 544 ms 14168 KB Output is correct
45 Correct 575 ms 14168 KB Output is correct
46 Correct 620 ms 14168 KB Output is correct
47 Correct 430 ms 7256 KB Output is correct
48 Correct 590 ms 14168 KB Output is correct
49 Correct 560 ms 14156 KB Output is correct
50 Correct 546 ms 14268 KB Output is correct
51 Correct 305 ms 856 KB Output is correct
52 Correct 505 ms 1112 KB Output is correct
53 Correct 605 ms 1112 KB Output is correct
54 Correct 567 ms 1112 KB Output is correct
55 Correct 589 ms 23632 KB Output is correct
56 Correct 620 ms 23888 KB Output is correct
57 Correct 611 ms 23996 KB Output is correct
58 Correct 498 ms 23888 KB Output is correct
59 Correct 669 ms 37464 KB Output is correct
60 Correct 627 ms 37592 KB Output is correct
61 Correct 384 ms 1112 KB Output is correct
62 Correct 549 ms 1112 KB Output is correct
63 Correct 553 ms 1112 KB Output is correct
64 Correct 524 ms 1876 KB Output is correct
65 Correct 526 ms 1624 KB Output is correct
66 Correct 525 ms 1624 KB Output is correct
67 Correct 549 ms 1624 KB Output is correct
68 Correct 528 ms 1624 KB Output is correct
69 Correct 535 ms 1076 KB Output is correct
70 Correct 535 ms 1108 KB Output is correct
71 Correct 518 ms 1624 KB Output is correct
72 Correct 578 ms 1624 KB Output is correct
73 Correct 536 ms 1112 KB Output is correct
74 Correct 616 ms 1108 KB Output is correct
75 Correct 560 ms 1040 KB Output is correct
76 Correct 481 ms 2392 KB Output is correct
77 Correct 505 ms 2392 KB Output is correct
78 Correct 515 ms 2392 KB Output is correct
79 Correct 538 ms 1368 KB Output is correct
80 Correct 526 ms 1112 KB Output is correct
81 Correct 585 ms 1080 KB Output is correct
82 Correct 558 ms 856 KB Output is correct
83 Correct 252 ms 864 KB Output is correct
84 Correct 562 ms 1112 KB Output is correct
85 Correct 541 ms 1112 KB Output is correct
86 Correct 520 ms 1112 KB Output is correct
87 Correct 0 ms 344 KB Output is correct
88 Correct 354 ms 22480 KB Output is correct
89 Correct 599 ms 14424 KB Output is correct
90 Correct 661 ms 13904 KB Output is correct
91 Correct 600 ms 23892 KB Output is correct
92 Correct 602 ms 23896 KB Output is correct
93 Correct 639 ms 23888 KB Output is correct
94 Correct 626 ms 24144 KB Output is correct
95 Correct 601 ms 24116 KB Output is correct
96 Correct 533 ms 11456 KB Output is correct
97 Correct 645 ms 11588 KB Output is correct
98 Correct 543 ms 27992 KB Output is correct
99 Correct 634 ms 23968 KB Output is correct
100 Correct 574 ms 20044 KB Output is correct
101 Correct 598 ms 13400 KB Output is correct
102 Correct 626 ms 11352 KB Output is correct
103 Correct 614 ms 37456 KB Output is correct
104 Correct 620 ms 37824 KB Output is correct
105 Correct 590 ms 38012 KB Output is correct
106 Correct 586 ms 17240 KB Output is correct
107 Correct 581 ms 12376 KB Output is correct
108 Correct 605 ms 12244 KB Output is correct
109 Correct 679 ms 11608 KB Output is correct