/**
* HDU: 1540 Tunnel Warface
* 题目大意: 有N个连通地道,接下来有M个操作,
* D x -> 炸掉x号地道
* Q k -> 查询包括k的最长地道
* R -> 修复最近依次被炸毁的地道
* 数据范围:N, M <= 50000
**/
/**
* HDU: 1540 Tunnel Warface
* 题目大意: 有N个连通地道,接下来有M个操作,
* D x -> 炸掉x号地道
* Q k -> 查询包括k的最长地道
* R -> 修复最近依次被炸毁的地道
* 数据范围:N, M <= 50000
**/
#include <array>
#include <stack>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <functional>
constexpr auto __MAX__ { static_cast<int>(5.0 * 1e5) };
std::array<int, __MAX__ << 2 | 1> extend {};
std::array<int, __MAX__ << 2 | 1> lstretch {};
std::array<int, __MAX__ << 2 | 1> rstretch {};
std::stack<int> recent {};
int N {}, M {};
constexpr int left(int x) noexcept { return x << 1; }
constexpr int right(int x) noexcept { return x << 1| 1; }
void update(int first, int last, int index) noexcept {
extend[index] = std::ranges::max({
extend[left(index)],
extend[right(index)],
std::plus<>{} (
lstretch[right(index)],
rstretch[left(index)])
});
int middle { (first + last) / 2 };
lstretch[index] = lstretch[left(index)];
if(lstretch[left(index)] == middle - first)
lstretch[index] += lstretch[right(index)];
rstretch[index] = rstretch[left(index)];
if(rstretch[left(index)] == last - middle)
rstretch[index] += rstretch[left(index)];
}
void change(int position, int value, int first, int last, int index) noexcept {
if(first + 1== last)
return (void) (lstretch[index] = rstretch[index] = extend[index] = value);
int middle { (first + last) / 2 };
if(position < middle) change(position, value, first, middle, left(index));
else change(position, value, middle, last, right(index));
update(first, last, index);
}
void build(int first, int last, int index) noexcept {
if(first + 1 == last)
return (void) (extend[index] = lstretch[index] = rstretch[index] = 1);
int middle { (first + last) / 2 };
build(first, middle, left(index));
build(middle, last, right(index));
update(first, last, index);
}
int query(int key, int first, int last, int index) noexcept {
if(first + 1 == last) return extend[index];
int middle { (first + middle) / 2 };
if(key < middle) {
if(middle - rstretch[left(index)] <= key)
return rstretch[left(index)] + lstretch[right(index)];
else
return query(key, first, middle, left(index));
}else {
if(key <= middle + lstretch[right(index)] - 1)
return rstretch[left(index)] + lstretch[right(index)];
else return query(key, middle, last, right(index));
}
}
void handle_for_once(std::string & operation) {
std::getline(std::cin, operation);
if(operation.at(0) == 'R') {
change(recent.top(), 1, 1, N + 1, 1);
recent.pop();
}else if(operation.at(0) == 'D') {
int x {};
std::scanf("%d", &x);
change(x, 0, 1, N + 1, 1);
recent.push(x);
}else {
int k {};
std::scanf("%d", &k);
std::printf("%d\n", query(k, 1, N + 1, 1));
}
}
int main() {
while(~std::scanf("%d%d", &N, &M) && !(N || M)) {
build(1, N + 1, 1);
std::string operation;
while(M --)
handle_for_once(operation);
}
}