#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int N, M, Q;
int Log[1 << 20];
pii mrg(pii a, pii b) {
if(a > b) swap(a, b);
if(a.first == b.first) return {a.first, a.second + b.second};
else return a;
}
struct BIT {
vector<pii> tree;
int level[20];
void init() {
tree = vector<pii>(4 * M);
build(0, M - 1, 1);
}
void build(int l, int r, int n) {
if(l == r) {
tree[n] = {0, 1};
return;
}
int m = (l + r)>>1;
build(l, m, 2*n);
build(m + 1, r, 2*n + 1);
tree[n] = mrg(tree[2*n], tree[2*n + 1]);
}
void upd(int idx, int l, int r, int n) {
if(idx < l || r < idx) return;
if(l == r) {
tree[n].first ^= 1;
return;
}
int m = (l + r)>>1;
upd(idx, l, m, 2*n);
upd(idx, m + 1, r, 2*n + 1);
int x = Log[ (m + 1) & -(m + 1) ];
if(tree[n].second != r - l + 1) {
level[x]--;
}
tree[n] = mrg(tree[2*n], tree[2*n + 1]);
if(tree[n].second != r - l + 1) {
level[x]++;
}
}
} bit1, bit2;
int main() {
for(int i = 0; i < 20; i++) Log[1 << i] = i;
scanf("%d %d", &N, &Q);
M = 1 << N;
bit1.init();
bit2.init();
for(int i = 0; i < Q; i++) {
int t, x; scanf("%d %d", &t, &x);
x--;
if(t == 0) bit1.upd(x, 0, M - 1, 1);
if(t == 1) bit2.upd(x, 0, M - 1, 1);
ll cnt = 0;
for(int j = 0; j < N; j++) {
cnt += 1LL * bit1.level[j] * (1 << (N - 1 - j));
cnt += 1LL * bit2.level[j] * (1 << (N - 1 - j));
cnt -= 1LL * bit1.level[j] * bit2.level[j];
}
printf("%lld\n", 1 + 4 * cnt);
}
}
Compilation message
collecting.cpp: In function 'int main()':
collecting.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~~
collecting.cpp:63:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int t, x; scanf("%d %d", &t, &x);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
700 KB |
Output is correct |
4 |
Correct |
3 ms |
700 KB |
Output is correct |
5 |
Correct |
3 ms |
700 KB |
Output is correct |
6 |
Correct |
4 ms |
700 KB |
Output is correct |
7 |
Correct |
3 ms |
700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
700 KB |
Output is correct |
2 |
Correct |
5 ms |
740 KB |
Output is correct |
3 |
Correct |
3 ms |
812 KB |
Output is correct |
4 |
Correct |
5 ms |
824 KB |
Output is correct |
5 |
Correct |
5 ms |
824 KB |
Output is correct |
6 |
Correct |
4 ms |
824 KB |
Output is correct |
7 |
Correct |
5 ms |
824 KB |
Output is correct |
8 |
Correct |
3 ms |
908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2467 ms |
110740 KB |
Output is correct |
2 |
Correct |
2490 ms |
125104 KB |
Output is correct |
3 |
Correct |
2480 ms |
125104 KB |
Output is correct |
4 |
Correct |
2502 ms |
125364 KB |
Output is correct |
5 |
Correct |
2447 ms |
125364 KB |
Output is correct |
6 |
Correct |
2305 ms |
125364 KB |
Output is correct |
7 |
Correct |
2812 ms |
125364 KB |
Output is correct |
8 |
Correct |
2823 ms |
125364 KB |
Output is correct |
9 |
Correct |
2340 ms |
125364 KB |
Output is correct |
10 |
Correct |
2461 ms |
125364 KB |
Output is correct |
11 |
Correct |
2727 ms |
125364 KB |
Output is correct |
12 |
Correct |
2417 ms |
141288 KB |
Output is correct |
13 |
Correct |
2725 ms |
141288 KB |
Output is correct |