답안 #638751

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
638751 2022-09-07T09:21:21 Z maomao90 디지털 회로 (IOI22_circuit) C++17
100 / 100
1053 ms 44476 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;

#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)

template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}

typedef long long ll;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef tuple<int, int, int> iii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

#define submod(x) if (x >= MOD) x -= MOD
#define addmod(x) if (x < 0) x += MOD

const int MAXN = 200005;
const int INF = 1000000007;
const ll LINF = 1000000000000000007;
const int MOD = 1000002022;

namespace brute {
    int n, m;
    vi p, a;
    vi adj[MAXN];

    ll ways[MAXN];
    ll dfs(int u) {
	if (u >= n) {
	    ways[u] = 1;
	    return a[u - n];
	}
	vll dp(1, 1);
	ways[u] = SZ(adj[u]);
	for (int v : adj[u]) {
	    ll tmp = dfs(v);
	    vll ndp(SZ(dp) + 1, 0);
	    REP (i, 0, SZ(dp)) {
		ndp[i + 1] += dp[i] * tmp % MOD;
		submod(ndp[i + 1]);
	    }
	    REP (i, 0, SZ(dp)) {
		ndp[i] += dp[i] * (ways[v] - tmp + MOD) % MOD;
		submod(ndp[i]);
	    }
	    dp = ndp;
	    ways[u] = ways[u] * ways[v] % MOD;
	}
	ll res = 0;
	REP (i, 1, SZ(dp)) {
	    res += dp[i] * i % MOD;
	    submod(res);
	}
	return res;
    }

    void init(int N, int M, vi P, vi A) {
	n = N, m = M, p = P, a = A;
	REP (i, 1, n + m) {
	    adj[p[i]].pb(i);
	}
    }

    int count_ways(int L, int R) {
	int l = L, r = R;
	REP (i, l - n, r + 1 - n) {
	    a[i] ^= 1;
	}
	return dfs(0);
    }
}

int n, m;
vi p, a;
vi adj[MAXN];

ll ways[MAXN];
void dfs(int u) {
    ways[u] = max(1, SZ(adj[u]));
    for (int v : adj[u]) {
	dfs(v);
	ways[u] = ways[u] * ways[v] % MOD;
    }
}
ll pw[MAXN];
void dfs2(int u) {
    if (adj[u].empty()) {
	return;
    }
    vll pp(SZ(adj[u])), sp(SZ(adj[u]));
    REP (i, 0, SZ(adj[u])) {
	pp[i] = ways[adj[u][i]];
	if (i) {
	    pp[i] = pp[i] * pp[i - 1] % MOD;
	}
    }
    RREP (i, SZ(adj[u]) - 1, 0) {
	sp[i] = ways[adj[u][i]];
	if (i != SZ(adj[u]) - 1) {
	    sp[i] = sp[i] * sp[i + 1] % MOD;
	}
    }
    REP (i, 0, SZ(adj[u])) {
	ll tmp = (i == 0 ? 1 : pp[i - 1]) * (i + 1 >= SZ(adj[u]) ? 1 : sp[i + 1]) % MOD;
	pw[adj[u][i]] = tmp * pw[u] % MOD;
	dfs2(adj[u][i]);
    }
}

#define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
int sm[MAXN * 4], lz[MAXN * 4];
ll coef[MAXN * 4];
void propo(int u, int lo, int hi) {
    if (lo == hi || lz[u] == 0) return;
    MLR;
    sm[lc] = coef[lc] - sm[lc];
    addmod(sm[lc]);
    sm[rc] = coef[rc] - sm[rc];
    addmod(sm[rc]);
    lz[lc] ^= 1;
    lz[rc] ^= 1;
    lz[u] = 0;
}
void init(int u = 1, int lo = 0, int hi = m - 1) {
    if (lo == hi) {
	coef[u] = pw[lo + n];
	sm[u] = a[lo] * coef[u] % MOD;
	lz[u] = 0;
	return;
    }
    MLR;
    init(lc, lo, mid);
    init(rc, mid + 1, hi);
    sm[u] = sm[lc] + sm[rc];
    submod(sm[u]);
    coef[u] = coef[lc] + coef[rc];
    submod(coef[u]);
}
void upd(int s, int e, int u = 1, int lo = 0, int hi = m - 1) {
    if (lo >= s && hi <= e) {
	lz[u] ^= 1;
	sm[u] = coef[u] - sm[u];
	addmod(sm[u]);
	return;
    }
    propo(u, lo, hi);
    MLR;
    if (s <= mid) {
	upd(s, e, lc, lo, mid);
    }
    if (e > mid) {
	upd(s, e, rc, mid + 1, hi);
    }
    sm[u] = sm[lc] + sm[rc];
    submod(sm[u]);
}

void init(int N, int M, vi P, vi A) {
    n = N, m = M, p = P, a = A;
    if (N <= 1000 && M <= 1000) {
	brute::init(N, M, P, A);
	return;
    }
    REP (i, 1, n + m) {
	adj[p[i]].pb(i);
    }
    dfs(0);
    pw[0] = 1;
    dfs2(0);
    REP (i, n, n + m) {
	cerr << i << ": " << pw[i] << '\n';
    }
    init();
    brute::init(N, M, P, A);
}

int count_ways(int L, int R) {
    if (n <= 1000 && m <= 1000) {
	return brute::count_ways(L, R);
    }
    int l = L, r = R;
    upd(l - n, r - n);
    //assert(sm[1] == brute::count_ways(L, R));
    return sm[1];
}

Compilation message

circuit.cpp: In function 'void propo(int, int, int)':
circuit.cpp:126:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  126 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
circuit.cpp:131:5: note: in expansion of macro 'MLR'
  131 |     MLR;
      |     ^~~
circuit.cpp:126:17: warning: unused variable 'mid' [-Wunused-variable]
  126 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                 ^~~
circuit.cpp:131:5: note: in expansion of macro 'MLR'
  131 |     MLR;
      |     ^~~
circuit.cpp: In function 'void init(int, int, int)':
circuit.cpp:126:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  126 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
circuit.cpp:147:5: note: in expansion of macro 'MLR'
  147 |     MLR;
      |     ^~~
circuit.cpp: In function 'void upd(int, int, int, int, int)':
circuit.cpp:126:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  126 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
circuit.cpp:163:5: note: in expansion of macro 'MLR'
  163 |     MLR;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 19 ms 9872 KB Output is correct
4 Correct 19 ms 9808 KB Output is correct
5 Correct 19 ms 9848 KB Output is correct
6 Correct 20 ms 9808 KB Output is correct
7 Correct 19 ms 9808 KB Output is correct
8 Correct 19 ms 9808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 7 ms 9680 KB Output is correct
3 Correct 6 ms 9680 KB Output is correct
4 Correct 5 ms 9680 KB Output is correct
5 Correct 6 ms 9680 KB Output is correct
6 Correct 6 ms 9668 KB Output is correct
7 Correct 7 ms 9764 KB Output is correct
8 Correct 6 ms 9808 KB Output is correct
9 Correct 6 ms 9680 KB Output is correct
10 Correct 6 ms 10004 KB Output is correct
11 Correct 6 ms 10012 KB Output is correct
12 Correct 6 ms 9680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 19 ms 9872 KB Output is correct
4 Correct 19 ms 9808 KB Output is correct
5 Correct 19 ms 9848 KB Output is correct
6 Correct 20 ms 9808 KB Output is correct
7 Correct 19 ms 9808 KB Output is correct
8 Correct 19 ms 9808 KB Output is correct
9 Correct 5 ms 9680 KB Output is correct
10 Correct 7 ms 9680 KB Output is correct
11 Correct 6 ms 9680 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9680 KB Output is correct
14 Correct 6 ms 9668 KB Output is correct
15 Correct 7 ms 9764 KB Output is correct
16 Correct 6 ms 9808 KB Output is correct
17 Correct 6 ms 9680 KB Output is correct
18 Correct 6 ms 10004 KB Output is correct
19 Correct 6 ms 10012 KB Output is correct
20 Correct 6 ms 9680 KB Output is correct
21 Correct 6 ms 9680 KB Output is correct
22 Correct 6 ms 9680 KB Output is correct
23 Correct 5 ms 9680 KB Output is correct
24 Correct 9 ms 9808 KB Output is correct
25 Correct 6 ms 9808 KB Output is correct
26 Correct 6 ms 9680 KB Output is correct
27 Correct 8 ms 9764 KB Output is correct
28 Correct 6 ms 9808 KB Output is correct
29 Correct 20 ms 9876 KB Output is correct
30 Correct 19 ms 9880 KB Output is correct
31 Correct 6 ms 9936 KB Output is correct
32 Correct 6 ms 9716 KB Output is correct
33 Correct 6 ms 9680 KB Output is correct
34 Correct 7 ms 9680 KB Output is correct
35 Correct 8 ms 9808 KB Output is correct
36 Correct 6 ms 9936 KB Output is correct
37 Correct 20 ms 10064 KB Output is correct
38 Correct 20 ms 10152 KB Output is correct
39 Correct 5 ms 9680 KB Output is correct
40 Correct 5 ms 9680 KB Output is correct
41 Correct 6 ms 9692 KB Output is correct
42 Correct 6 ms 9748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 578 ms 15656 KB Output is correct
2 Correct 731 ms 21460 KB Output is correct
3 Correct 891 ms 21488 KB Output is correct
4 Correct 757 ms 21512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 578 ms 15656 KB Output is correct
2 Correct 731 ms 21460 KB Output is correct
3 Correct 891 ms 21488 KB Output is correct
4 Correct 757 ms 21512 KB Output is correct
5 Correct 719 ms 15568 KB Output is correct
6 Correct 922 ms 21464 KB Output is correct
7 Correct 960 ms 21592 KB Output is correct
8 Correct 988 ms 21456 KB Output is correct
9 Correct 301 ms 10064 KB Output is correct
10 Correct 812 ms 10464 KB Output is correct
11 Correct 828 ms 10464 KB Output is correct
12 Correct 825 ms 10460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 7 ms 9680 KB Output is correct
3 Correct 6 ms 9680 KB Output is correct
4 Correct 5 ms 9680 KB Output is correct
5 Correct 6 ms 9680 KB Output is correct
6 Correct 6 ms 9668 KB Output is correct
7 Correct 7 ms 9764 KB Output is correct
8 Correct 6 ms 9808 KB Output is correct
9 Correct 6 ms 9680 KB Output is correct
10 Correct 6 ms 10004 KB Output is correct
11 Correct 6 ms 10012 KB Output is correct
12 Correct 6 ms 9680 KB Output is correct
13 Correct 578 ms 15656 KB Output is correct
14 Correct 731 ms 21460 KB Output is correct
15 Correct 891 ms 21488 KB Output is correct
16 Correct 757 ms 21512 KB Output is correct
17 Correct 719 ms 15568 KB Output is correct
18 Correct 922 ms 21464 KB Output is correct
19 Correct 960 ms 21592 KB Output is correct
20 Correct 988 ms 21456 KB Output is correct
21 Correct 301 ms 10064 KB Output is correct
22 Correct 812 ms 10464 KB Output is correct
23 Correct 828 ms 10464 KB Output is correct
24 Correct 825 ms 10460 KB Output is correct
25 Correct 967 ms 28452 KB Output is correct
26 Correct 783 ms 28784 KB Output is correct
27 Correct 822 ms 28792 KB Output is correct
28 Correct 759 ms 28744 KB Output is correct
29 Correct 904 ms 43608 KB Output is correct
30 Correct 931 ms 43632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 19 ms 9872 KB Output is correct
4 Correct 19 ms 9808 KB Output is correct
5 Correct 19 ms 9848 KB Output is correct
6 Correct 20 ms 9808 KB Output is correct
7 Correct 19 ms 9808 KB Output is correct
8 Correct 19 ms 9808 KB Output is correct
9 Correct 5 ms 9680 KB Output is correct
10 Correct 7 ms 9680 KB Output is correct
11 Correct 6 ms 9680 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9680 KB Output is correct
14 Correct 6 ms 9668 KB Output is correct
15 Correct 7 ms 9764 KB Output is correct
16 Correct 6 ms 9808 KB Output is correct
17 Correct 6 ms 9680 KB Output is correct
18 Correct 6 ms 10004 KB Output is correct
19 Correct 6 ms 10012 KB Output is correct
20 Correct 6 ms 9680 KB Output is correct
21 Correct 6 ms 9680 KB Output is correct
22 Correct 6 ms 9680 KB Output is correct
23 Correct 5 ms 9680 KB Output is correct
24 Correct 9 ms 9808 KB Output is correct
25 Correct 6 ms 9808 KB Output is correct
26 Correct 6 ms 9680 KB Output is correct
27 Correct 8 ms 9764 KB Output is correct
28 Correct 6 ms 9808 KB Output is correct
29 Correct 20 ms 9876 KB Output is correct
30 Correct 19 ms 9880 KB Output is correct
31 Correct 6 ms 9936 KB Output is correct
32 Correct 6 ms 9716 KB Output is correct
33 Correct 6 ms 9680 KB Output is correct
34 Correct 7 ms 9680 KB Output is correct
35 Correct 8 ms 9808 KB Output is correct
36 Correct 6 ms 9936 KB Output is correct
37 Correct 20 ms 10064 KB Output is correct
38 Correct 20 ms 10152 KB Output is correct
39 Correct 5 ms 9680 KB Output is correct
40 Correct 5 ms 9680 KB Output is correct
41 Correct 6 ms 9692 KB Output is correct
42 Correct 6 ms 9748 KB Output is correct
43 Correct 596 ms 10284 KB Output is correct
44 Correct 723 ms 10320 KB Output is correct
45 Correct 949 ms 10320 KB Output is correct
46 Correct 884 ms 10704 KB Output is correct
47 Correct 755 ms 10704 KB Output is correct
48 Correct 674 ms 10736 KB Output is correct
49 Correct 730 ms 10824 KB Output is correct
50 Correct 764 ms 10704 KB Output is correct
51 Correct 810 ms 10320 KB Output is correct
52 Correct 849 ms 10320 KB Output is correct
53 Correct 565 ms 10960 KB Output is correct
54 Correct 890 ms 10720 KB Output is correct
55 Correct 912 ms 10448 KB Output is correct
56 Correct 945 ms 10428 KB Output is correct
57 Correct 796 ms 10192 KB Output is correct
58 Correct 621 ms 11472 KB Output is correct
59 Correct 714 ms 11568 KB Output is correct
60 Correct 696 ms 11564 KB Output is correct
61 Correct 842 ms 10496 KB Output is correct
62 Correct 879 ms 10192 KB Output is correct
63 Correct 845 ms 10192 KB Output is correct
64 Correct 785 ms 10320 KB Output is correct
65 Correct 509 ms 10044 KB Output is correct
66 Correct 872 ms 10568 KB Output is correct
67 Correct 828 ms 10484 KB Output is correct
68 Correct 825 ms 10460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9680 KB Output is correct
2 Correct 5 ms 9680 KB Output is correct
3 Correct 19 ms 9872 KB Output is correct
4 Correct 19 ms 9808 KB Output is correct
5 Correct 19 ms 9848 KB Output is correct
6 Correct 20 ms 9808 KB Output is correct
7 Correct 19 ms 9808 KB Output is correct
8 Correct 19 ms 9808 KB Output is correct
9 Correct 5 ms 9680 KB Output is correct
10 Correct 7 ms 9680 KB Output is correct
11 Correct 6 ms 9680 KB Output is correct
12 Correct 5 ms 9680 KB Output is correct
13 Correct 6 ms 9680 KB Output is correct
14 Correct 6 ms 9668 KB Output is correct
15 Correct 7 ms 9764 KB Output is correct
16 Correct 6 ms 9808 KB Output is correct
17 Correct 6 ms 9680 KB Output is correct
18 Correct 6 ms 10004 KB Output is correct
19 Correct 6 ms 10012 KB Output is correct
20 Correct 6 ms 9680 KB Output is correct
21 Correct 6 ms 9680 KB Output is correct
22 Correct 6 ms 9680 KB Output is correct
23 Correct 5 ms 9680 KB Output is correct
24 Correct 9 ms 9808 KB Output is correct
25 Correct 6 ms 9808 KB Output is correct
26 Correct 6 ms 9680 KB Output is correct
27 Correct 8 ms 9764 KB Output is correct
28 Correct 6 ms 9808 KB Output is correct
29 Correct 20 ms 9876 KB Output is correct
30 Correct 19 ms 9880 KB Output is correct
31 Correct 6 ms 9936 KB Output is correct
32 Correct 6 ms 9716 KB Output is correct
33 Correct 6 ms 9680 KB Output is correct
34 Correct 7 ms 9680 KB Output is correct
35 Correct 8 ms 9808 KB Output is correct
36 Correct 6 ms 9936 KB Output is correct
37 Correct 20 ms 10064 KB Output is correct
38 Correct 20 ms 10152 KB Output is correct
39 Correct 5 ms 9680 KB Output is correct
40 Correct 5 ms 9680 KB Output is correct
41 Correct 6 ms 9692 KB Output is correct
42 Correct 6 ms 9748 KB Output is correct
43 Correct 578 ms 15656 KB Output is correct
44 Correct 731 ms 21460 KB Output is correct
45 Correct 891 ms 21488 KB Output is correct
46 Correct 757 ms 21512 KB Output is correct
47 Correct 719 ms 15568 KB Output is correct
48 Correct 922 ms 21464 KB Output is correct
49 Correct 960 ms 21592 KB Output is correct
50 Correct 988 ms 21456 KB Output is correct
51 Correct 301 ms 10064 KB Output is correct
52 Correct 812 ms 10464 KB Output is correct
53 Correct 828 ms 10464 KB Output is correct
54 Correct 825 ms 10460 KB Output is correct
55 Correct 967 ms 28452 KB Output is correct
56 Correct 783 ms 28784 KB Output is correct
57 Correct 822 ms 28792 KB Output is correct
58 Correct 759 ms 28744 KB Output is correct
59 Correct 904 ms 43608 KB Output is correct
60 Correct 931 ms 43632 KB Output is correct
61 Correct 596 ms 10284 KB Output is correct
62 Correct 723 ms 10320 KB Output is correct
63 Correct 949 ms 10320 KB Output is correct
64 Correct 884 ms 10704 KB Output is correct
65 Correct 755 ms 10704 KB Output is correct
66 Correct 674 ms 10736 KB Output is correct
67 Correct 730 ms 10824 KB Output is correct
68 Correct 764 ms 10704 KB Output is correct
69 Correct 810 ms 10320 KB Output is correct
70 Correct 849 ms 10320 KB Output is correct
71 Correct 565 ms 10960 KB Output is correct
72 Correct 890 ms 10720 KB Output is correct
73 Correct 912 ms 10448 KB Output is correct
74 Correct 945 ms 10428 KB Output is correct
75 Correct 796 ms 10192 KB Output is correct
76 Correct 621 ms 11472 KB Output is correct
77 Correct 714 ms 11568 KB Output is correct
78 Correct 696 ms 11564 KB Output is correct
79 Correct 842 ms 10496 KB Output is correct
80 Correct 879 ms 10192 KB Output is correct
81 Correct 845 ms 10192 KB Output is correct
82 Correct 785 ms 10320 KB Output is correct
83 Correct 509 ms 10044 KB Output is correct
84 Correct 872 ms 10568 KB Output is correct
85 Correct 828 ms 10484 KB Output is correct
86 Correct 825 ms 10460 KB Output is correct
87 Correct 5 ms 9680 KB Output is correct
88 Correct 560 ms 27336 KB Output is correct
89 Correct 989 ms 22004 KB Output is correct
90 Correct 870 ms 21300 KB Output is correct
91 Correct 836 ms 29020 KB Output is correct
92 Correct 981 ms 28936 KB Output is correct
93 Correct 868 ms 29024 KB Output is correct
94 Correct 998 ms 29032 KB Output is correct
95 Correct 955 ms 29040 KB Output is correct
96 Correct 785 ms 20276 KB Output is correct
97 Correct 815 ms 20292 KB Output is correct
98 Correct 870 ms 35912 KB Output is correct
99 Correct 848 ms 28744 KB Output is correct
100 Correct 1053 ms 23880 KB Output is correct
101 Correct 951 ms 22488 KB Output is correct
102 Correct 765 ms 19952 KB Output is correct
103 Correct 958 ms 43700 KB Output is correct
104 Correct 1037 ms 44456 KB Output is correct
105 Correct 849 ms 44476 KB Output is correct
106 Correct 962 ms 24544 KB Output is correct
107 Correct 847 ms 21256 KB Output is correct
108 Correct 782 ms 20936 KB Output is correct
109 Correct 728 ms 20168 KB Output is correct