-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBTREE.cpp
More file actions
103 lines (70 loc) · 1.11 KB
/
RBTREE.cpp
File metadata and controls
103 lines (70 loc) · 1.11 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
int q;
int red;
int black;
int lg(int n){
int c = 0;
while((1<<c) <= n){
c++;
}
return c-1;
}
int calc(int u,int v){
int nvlu = lg(u);
int nvlv = lg(v);
if(nvlu%2==0) black++;
else red++;
if(nvlv%2==0) black++;
else red++;
while(u!=v){
int paiu = u/2;
int paiv = v/2;
if(nvlu > nvlv){
u = paiu;
nvlu--;
if(nvlu%2==0) black++;
else red++;
}else{
v = paiv;
nvlv--;
if(nvlv%2==0) black++;
else red++;
}
}
if(nvlu%2==0) black--;
else red--;
return u;
}
int main(){
bool muda = false;
cin >> q;
while(q--){
black = 0;
red = 0;
string s;
cin >> s;
if(s=="Qi"){
muda = !muda;
continue;
}
int u,v;
cin >> u >> v;
calc(u,v);
if(s=="Qb"){
if(!muda){
cout << black << endl;
}else{
cout << red << endl;
}
}
if(s=="Qr"){
if(!muda){
cout << red << endl;
}else{
cout << black << endl;
}
}
}
return 0;
}