-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWALKBT_AUG17.cpp
More file actions
78 lines (73 loc) · 1.56 KB
/
WALKBT_AUG17.cpp
File metadata and controls
78 lines (73 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
long long ans;
struct node{
node * left;
node * right;
};
void BinAdd(bool * X, int n, int c){
if(X[n-c] == 1)
for(int i=n-c; i>0; --i){
if(X[i] == 1)
X[i] = 0;
else{
X[i] = 1;
break;
}
}
else
X[n-c] = 1;
X[0] = 0;
}
void Insert(node * &root, bool * X, int n){
node * temp = root;
for(int i=1;i<=n;++i){
// left
if(X[i] == 0){
if(temp->left != NULL)
temp = temp->left;
else{
temp->left = new node;
temp = temp->left;
temp->left = NULL;
temp->right = NULL;
++ans;
}
}
// right
else{
if(temp->right != NULL)
temp = temp->right;
else{
temp->right = new node;
temp = temp->right;
temp->left = NULL;
temp->right = NULL;
++ans;
}
}
}
}
int main(){
int t,c,N,Q;
cin>>t;
while(t--){
cin>>N>>Q;
bool X[N+1] = {0};
node * root = new node;
root->left = NULL;
root->right = NULL;
ans = 1;
char type;
while(Q--){
cin>>type;
if(type == '?')
cout<<ans<<endl;
else{
cin>>c;
BinAdd(X,N,c);
Insert(root,X,N);
}
}
}
}