deque 模拟下每次加入数删除数即可,记录数是否被删除,数在左que还是右que,que中没被删除的数的个数
const int maxn = 2e7 + 7;
int n, t, m, x, sizl, sizr;
bool vis[maxn], pls[maxn];
struct node {
int num[maxn], l = maxn / 2, r = maxn / 2 - 1;
bool empty() const {
return r < l;
}
void push_back(int x) {
num[++r] = x;
}
void push_front(int x) {
num[--l] = x;
}
int front() {
return num[l];
}
int back() {
return num[r];
}
void pop_back() {
r--;
}
void pop_front() {
l++;
}
} lq, rq;
char q;
void update() {
again:
int tot = sizr + sizl;
while (!lq.empty() && vis[lq.back()]) lq.pop_back();
while (!rq.empty() && vis[rq.front()]) rq.pop_front();
if (sizl == tot / 2 + 1 || tot == 0) return;
if (sizl > tot / 2 + 1) {
while (!lq.empty() && vis[lq.back()]) lq.pop_back();
if (!lq.empty()) {
rq.push_front(lq.back());
lq.pop_back();
pls[rq.front()] = 0;
sizr++, sizl--;
}
} else {
while (!rq.empty() && vis[rq.front()]) rq.pop_front();
if (!rq.empty()) {
lq.push_back(rq.front());
rq.pop_front();
pls[lq.back()] = 1;
sizr--, sizl++;
}
}
goto again;
}
void solve() {
scanf("%d", &n);
for (int i = 1, pl = 1; i <= n; i++) {
scanf(" %c", &q);
if (q == 'Q') {
update();
printf("%d\n", lq.back());
} else {
if (q == 'L') {
lq.push_front(pl);
pls[pl++] = 1, sizl++;
} else if (q == 'R') {
rq.push_back(pl);
pls[pl++] = 0, sizr++;
} else {
scanf("%d", &x);
if (pls[x] == 1) sizl--, vis[x] = 1;
else sizr--, vis[x] = 1;
}
update();
}
}