Submission #807584

# Submission time Handle Problem Language Result Execution time Memory
807584 2023-08-04T19:44:38 Z alex_2008 Digital Circuit (IOI22_circuit) C++17
16 / 100
885 ms 2097152 KB
#include "circuit.h"
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
int NN, MM;
const int N = 2e5 + 10, P = 1000002022;
int p[N], d[N];
int lazy[4 * N];
int a[N];
ll pref[N];
pair <ll, ll> tree[4 * N];
ll tree2[4 * N];
ll binpow(ll a, ll n) {
	if (a == 0) return 1;
	if (a % 2 == 0) {
		ll u = binpow(a, n / 2);
		return (u * u) % P;
	}
	return (a * binpow(a, n - 1)) % P;
}
bool ch = true;
void pushh(int v, int tl, int tr) {
	if (lazy[v]) {
		lazy[v] = 0;
		if (tl != tr) {
			lazy[2 * v] = (lazy[2 * v] + 1) % 2;
			lazy[2 * v + 1] = (lazy[2 * v + 1] + 1) % 2;
			swap(tree[2 * v].first, tree[2 * v].second);
			swap(tree[2 * v + 1].first, tree[2 * v + 1].second);
		}
	}
}
void build_tree(int v, int tl, int tr) {
	if (tl == tr) {
		if (a[tl] == 1) tree[v] = { 1, 0 };
		else tree[v] = { 0, 1 };
	}
	else {
		int tm = (tl + tr) / 2;
		build_tree(2 * v, tl, tm);
		build_tree(2 * v + 1, tm + 1, tr);
		ll a = tree[2 * v].first, b = tree[2 * v].second, c = tree[2 * v + 1].first, d = tree[2 * v + 1].second;
		tree[v].first = 2 * a * c + a * d + b * c;
		tree[v].second = 2 * b * d + a * d + b * c;
		tree[v].first %= P;
		tree[v].second %= P;
	}
}
void update(int v, int tl, int tr, int l, int r) {
	if (tl > r || tr < l) return;
	if (tl >= l && tr <= r) {
		lazy[v] = (lazy[v] + 1) % 2;
		swap(tree[v].first, tree[v].second);
		return;
	}
	pushh(v, tl, tr);
	int tm = (tl + tr) / 2;
	update(2 * v, tl, tm, l, r);
	update(2 * v + 1, tm + 1, tr, l, r);
	ll a = tree[2 * v].first, b = tree[2 * v].second, c = tree[2 * v + 1].first, d = tree[2 * v + 1].second;
	tree[v].first = 2 * a * c + a * d + b * c;
	tree[v].second = 2 * b * d + a * d + b * c;
	tree[v].first %= P;
	tree[v].second %= P;
}
void pushh2(int v, int tl, int tr) {
	if (lazy[v]) {
		lazy[v] = 0;
		if (tl != tr) {
			lazy[2 * v] = (lazy[2 * v] + 1) % 2;
			lazy[2 * v + 1] = (lazy[2 * v + 1] + 1) % 2;
			int tm = (tl + tr) / 2;
			ll u = pref[tm];
			if (tl) u -= pref[tl - 1];
			u %= P;
			tree2[2 * v] = (u + P - tree2[2 * v]) % P;
			u = pref[tr] - pref[tm];
			u %= P;
			tree2[2 * v + 1] = (u + P - tree2[2 * v + 1]) % P;
		}
	}
}
void build_tree2(int v, int tl, int tr) {
	if (tl == tr) {
		if (a[tl]) {
			tree2[v] = pref[tl];
			if (tl) tree2[v] -= pref[tl - 1];
		}
		tree2[v] %= P;
	}
	else {
		int tm = (tl + tr) / 2;
		build_tree2(v, tl, tm);
		build_tree2(v, tm + 1, tr);
		tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
		tree2[v] %= P;
	}
}
void update2(int v, int tl, int tr, int l, int r) {
	if (tl > r || tr < l) return;
	if (tl >= l && tr <= r) {
		lazy[v] = (lazy[v] + 1) % 2;
		ll u = pref[tr];
		if (l) u -= pref[tl - 1];
		u %= P;
		tree2[v] = (u - tree2[v] + P) % P;
		return;
	}
	pushh2(v, tl, tr);
	int tm = (tl + tr) / 2;
	update2(2 * v, tl, tm, l, r);
	update2(2 * v + 1, tm + 1, tr, l, r);
	tree2[v] = tree2[2 * v] + tree2[2 * v + 1];
	tree2[v] %= P;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
	NN = N, MM = M;
	for (int i = 0; i < N + M - 1; i++) {
		p[i] = P[i];
		if (i == 0) d[0] = 0;
		if (i > 0 && p[i] != (i - 1) / 2) ch = false;
		d[i] = d[P[i]] + 1;
	}
	for (int i = 0; i < M; i++) {
		a[i] = A[i];
	}
	if (M != N + 1) ch = false;
	int u = log2(M);
	if ((1 << u) != M) ch = false;
	if (ch) build_tree(1, 0, M - 1);
	else {
		for (int i = 0; i < M; i++) {
			pref[i] = binpow(2, N - d[i + N]);
			if (i) pref[i] += pref[i - 1];
		}
		build_tree2(1, 0, M - 1);
	}
}
int count_ways(int L, int R) {
	if (ch) {
		update(1, 0, MM - 1, L - NN, R - NN);
		return tree[1].first;
	}
	else {
		update(1, 0, MM - 1, L, R);
		return tree2[1];
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Runtime error 794 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 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 Runtime error 754 ms 2097152 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Runtime error 794 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 461 ms 2736 KB Output is correct
2 Correct 712 ms 5172 KB Output is correct
3 Correct 653 ms 5200 KB Output is correct
4 Correct 661 ms 5152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 2736 KB Output is correct
2 Correct 712 ms 5172 KB Output is correct
3 Correct 653 ms 5200 KB Output is correct
4 Correct 661 ms 5152 KB Output is correct
5 Correct 697 ms 2768 KB Output is correct
6 Correct 885 ms 5192 KB Output is correct
7 Correct 725 ms 5192 KB Output is correct
8 Correct 743 ms 5200 KB Output is correct
9 Correct 307 ms 464 KB Output is correct
10 Correct 645 ms 592 KB Output is correct
11 Correct 752 ms 592 KB Output is correct
12 Correct 718 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 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 Runtime error 754 ms 2097152 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Runtime error 794 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Runtime error 794 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -