稍微写快一点的暴力可以过
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
int n, m;
bool room[MAX];
stack<int> last;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
char op;
int x;
room[0] = room[n + 1] = true;
for (int i = 0; i < m; i++) {
cin >> op;
if (op == 'D') {
cin >> x;
room[x] = true;
last.push(x);
} else if (op == 'R') {
if (!last.empty()) {
room[last.top()] = 0;
last.pop();
}
} else {
cin >> x;
if (room[x]) {
cout << 0 << endl;
continue;
}
int ans = 0;
for (int now = x; !room[now]; now++) ans++;
for (int now = x - 1; !room[now]; now--) ans++;
cout << ans << "\n";
}
}
return 0;
}