forked from hackerkid/LightOJ-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1087-Diablo.cpp
156 lines (113 loc) · 1.86 KB
/
1087-Diablo.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[2000005];
int tree[2000005];
int vis[2000005];
int n;
int update(int i, int val)
{
while(i <= n) {
tree[i] += val;
i = i + (i & (-i));
}
}
long long query(int i)
{
long long res = 0;
while(i > 0) {
res = res + tree[i];
i = i - (i & (-i));
}
return res;
}
int search(int i, int j, int x)
{
int mid;
int val;
mid = (i + j) / 2;
if(i > j ) {
return -1;
}
val = query(mid);
// cout <<"mid => " <<mid << "val => " << val << "x = > " << x << endl;
if(val > x) {
return search(i, mid-1, x);
}
if(val < x) {
return search(mid+1, j, x);
}
if(val == x) {
if(vis[mid] == 1) {
return mid;
}
else {
return search(i, mid-1, x);
}
}
}
int main()
{
int t;
int m;
int count;
char ch;
int x;
int k;
int nn;
scanf("%d", &t);
n = 100000;
for (int cs = 1; cs <= t;cs++) {
scanf("%d %d", &nn, &m);
n = nn + m + 1;
count = 0;
memset(a, 0, sizeof a);
memset(tree, 0, sizeof tree);
memset(vis, 0, sizeof vis);
printf("Case %d:\n", cs);
for (int i = 1; i <= nn; i++) {
scanf("%d", a + i);
vis[i] = 1;
count++;
update(i, 1);
}
for (int i = 0; i < m; i++) {
getchar();
scanf("%c %d", &ch, &x);
//cout << ch << " " << x << endl;
if(ch == 'a') {
nn++;
a[nn] = x;
vis[nn] = 1;
update(nn, 1);
count++;
}
if(ch == 'c') {
if(x > count) {
printf("none\n");
continue;
}
k = search(1, nn, x);
update(k, -1);
vis[k] = 0;
count--;
printf("%d\n", a[k]);
}
/*
for (int i = 1; i <= nn; i++) {
cout << query(i) << " ";
}
cout << endl;
for (int i = 1; i <= nn; i++) {
cout << a[i] << " ";
}
cout << endl;
for (int i = 1; i <= nn; i++) {
cout << vis[i] << " ";
}
cout << endl;
*/
}
}
}