#include<stdio.h>
long long Res, D[21];
int C[2][21];
int n, Q, IT[2][2097152];
void Do(int ck, int a){
a += (1 << n);
int h1 = 0, h2 = 0, c = 0, i;
IT[ck][a] = !IT[ck][a];
while (a != 1){
c++; a >>= 1;
if (IT[ck][a] == (1 << c) || IT[ck][a] == 0)h1 = c;
IT[ck][a] = IT[ck][a * 2] + IT[ck][a * 2 + 1];
if (IT[ck][a] == (1 << c) || IT[ck][a] == 0)h2 = c;
}
if (h1){
C[ck][h1]--;
C[ck][0]++;
for (i = 0; i < h1; i++)C[ck][i]++;
}
else{
C[ck][h2]++;
C[ck][0]--;
for (i = 0; i < h2; i++)C[ck][i]--;
}
}
long long Calc(){
int i, S = 0;
long long r = 0;
for (i = n; i >= 1; i--){
S += C[1][i] << i;
r += C[0][i] * D[i] * (S >> i);
}
S = 0;
for (i = n; i >= 1; i--){
r += C[1][i] * D[i] * (S >> i);
S += C[0][i] << i;
}
return r;
}
int main()
{
int ck, a, i;
scanf("%d%d", &n, &Q);
C[0][n] = C[1][n] = 1;
for (i = 1; i <= 20; i++)D[i] = (D[i - 1] + 1) * 4;
while (Q--){
scanf("%d%d", &ck, &a);
a--;
Do(ck, a);
printf("%lld\n", D[n] + 1 - Calc());
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17472 KB |
Output is correct |
2 |
Correct |
0 ms |
17472 KB |
Output is correct |
3 |
Correct |
0 ms |
17472 KB |
Output is correct |
4 |
Correct |
0 ms |
17472 KB |
Output is correct |
5 |
Correct |
0 ms |
17472 KB |
Output is correct |
6 |
Correct |
0 ms |
17472 KB |
Output is correct |
7 |
Correct |
0 ms |
17472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17472 KB |
Output is correct |
2 |
Correct |
0 ms |
17472 KB |
Output is correct |
3 |
Correct |
0 ms |
17472 KB |
Output is correct |
4 |
Correct |
0 ms |
17472 KB |
Output is correct |
5 |
Correct |
0 ms |
17472 KB |
Output is correct |
6 |
Correct |
0 ms |
17472 KB |
Output is correct |
7 |
Correct |
0 ms |
17472 KB |
Output is correct |
8 |
Correct |
0 ms |
17472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1712 ms |
17472 KB |
Output is correct |
2 |
Correct |
1696 ms |
17472 KB |
Output is correct |
3 |
Correct |
1456 ms |
17472 KB |
Output is correct |
4 |
Correct |
1672 ms |
17472 KB |
Output is correct |
5 |
Correct |
1660 ms |
17472 KB |
Output is correct |
6 |
Correct |
1660 ms |
17472 KB |
Output is correct |
7 |
Correct |
1656 ms |
17472 KB |
Output is correct |
8 |
Correct |
1640 ms |
17472 KB |
Output is correct |
9 |
Correct |
1492 ms |
17472 KB |
Output is correct |
10 |
Correct |
1516 ms |
17472 KB |
Output is correct |
11 |
Correct |
1612 ms |
17472 KB |
Output is correct |
12 |
Correct |
1668 ms |
17472 KB |
Output is correct |
13 |
Correct |
1560 ms |
17472 KB |
Output is correct |