#include <stdio.h>
#include <vector>
#include <iostream>
#include <cassert>
struct Node {
int sum, leftId, rightId;
Node(int low, int high, int idl = -1, int idr = -1)
: sum(high-low+1), leftId(idl), rightId(idr)
{ }
};
struct SegmentTree {
const int left, right;
std::vector<Node> tree;
void extend(int id, int l, int r)
{
// Check on estimated max size
assert(tree.size() <= (1u << 21) + (1u << 20));
if (tree[id].leftId == -1) {
int mid = (l + r) >> 1;
tree.push_back(Node(l, mid));
tree[id].leftId = (int)tree.size()-1;
tree.push_back(Node(mid+1, r));
tree[id].rightId = (int)tree.size()-1;
}
}
void update(int index, int delta, int id, int l, int r) {
if (index < l || index > r) {
return;
}
if (l == r) {
tree[id].sum += delta;
return;
}
extend(id, l, r);
const int idl = tree[id].leftId; assert(idl != -1);
const int idr = tree[id].rightId; assert(idr != -1);
const int m = (l+r)/2;
update(index, delta, idl, l, m);
update(index, delta, idr, m+1, r);
tree[id].sum = tree[idl].sum + tree[idr].sum;
}
void update(int index, int delta) {
return update(index, delta, 0, left, right);
}
int query(int k, int id, int l, int r) {
if (l == r) {
return l;
}
extend(id, l, r);
const int idl = tree[id].leftId; assert(idl != -1);
const int idr = tree[id].rightId; assert(idr != -1);
const int m = (l+r)/2;
if (tree[idl].sum >= k) {
return query(k, idl, l, m);
} else {
return query(k - tree[idl].sum, idr, m+1, r);
}
}
int query(int k) {
return query(k, 0, left, right);
}
SegmentTree(int l, int r) : left(l), right(r) {
tree.reserve((1u << 21) + (1u << 20)); // Reserve estimated max size Nodes
tree.push_back(Node(l, r));
}
};
int main() {
int n, m, num;
char op;
scanf("%d%d", &n, &m);
SegmentTree st(1, n);
for (int i = 1; i <= m; i++) {
scanf(" %c %d", &op, &num);
int q = st.query(num);
if (op == 'L') {
printf("%d\n", q);
} else {
st.update(q, -1);
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGNhc3N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ogICAgCiAgICAgICAgaWYgKGwgPT0gcikgewogICAgICAgICAgICB0cmVlW2lkXS5zdW0gKz0gZGVsdGE7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgZXh0ZW5kKGlkLCBsLCByKTsKICAgICAgICAKICAgICAgICBjb25zdCBpbnQgaWRsID0gdHJlZVtpZF0ubGVmdElkOyAgYXNzZXJ0KGlkbCAhPSAtMSk7CiAgICAgICAgY29uc3QgaW50IGlkciA9IHRyZWVbaWRdLnJpZ2h0SWQ7IGFzc2VydChpZHIgIT0gLTEpOyAgICAgICAgCiAgICAgICAgY29uc3QgaW50IG0gPSAobCtyKS8yOwogICAgICAgIAogICAgICAgIHVwZGF0ZShpbmRleCwgZGVsdGEsIGlkbCwgICBsLCBtKTsKICAgICAgICB1cGRhdGUoaW5kZXgsIGRlbHRhLCBpZHIsIG0rMSwgcik7CiAgICAKICAgICAgICB0cmVlW2lkXS5zdW0gPSB0cmVlW2lkbF0uc3VtICsgdHJlZVtpZHJdLnN1bTsKICAgIH0KICAgIAogICAgdm9pZCB1cGRhdGUoaW50IGluZGV4LCBpbnQgZGVsdGEpIHsKICAgICAgICByZXR1cm4gdXBkYXRlKGluZGV4LCBkZWx0YSwgMCwgbGVmdCwgcmlnaHQpOwogICAgfQogICAgCiAgICBpbnQgcXVlcnkoaW50IGssIGludCBpZCwgaW50IGwsIGludCByKSB7CiAgICAgICAgaWYgKGwgPT0gcikgewogICAgICAgICAgICByZXR1cm4gbDsKICAgICAgICB9CgogICAgICAgIGV4dGVuZChpZCwgbCwgcik7CiAgICAgICAgCiAgICAgICAgY29uc3QgaW50IGlkbCA9IHRyZWVbaWRdLmxlZnRJZDsgIGFzc2VydChpZGwgIT0gLTEpOwogICAgICAgIGNvbnN0IGludCBpZHIgPSB0cmVlW2lkXS5yaWdodElkOyBhc3NlcnQoaWRyICE9IC0xKTsgICAgICAgIAogICAgICAgIGNvbnN0IGludCBtID0gKGwrcikvMjsKICAgICAgICAKICAgICAgICBpZiAodHJlZVtpZGxdLnN1bSA+PSBrKSB7CiAgICAgICAgICAgIHJldHVybiBxdWVyeShrLCBpZGwsIGwsIG0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiBxdWVyeShrIC0gdHJlZVtpZGxdLnN1bSwgaWRyLCBtKzEsIHIpOwogICAgICAgIH0KICAgIH0KICAgIAogICAgaW50IHF1ZXJ5KGludCBrKSB7CiAgICAgICAgcmV0dXJuIHF1ZXJ5KGssIDAsIGxlZnQsIHJpZ2h0KTsKICAgIH0KICAgIAogICAgU2VnbWVudFRyZWUoaW50IGwsIGludCByKSA6IGxlZnQobCksIHJpZ2h0KHIpIHsKICAgICAgICB0cmVlLnJlc2VydmUoKDF1IDw8IDIxKSArICgxdSA8PCAyMCkpOyAvLyBSZXNlcnZlIGVzdGltYXRlZCBtYXggc2l6ZSBOb2RlcwogICAgICAgIHRyZWUucHVzaF9iYWNrKE5vZGUobCwgcikpOwogICAgfQp9OwoKaW50IG1haW4oKSB7CiAgICBpbnQgbiwgbSwgbnVtOwogICAgY2hhciBvcDsKCiAgICBzY2FuZigiJWQlZCIsICZuLCAmbSk7CgogICAgU2VnbWVudFRyZWUgc3QoMSwgbik7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbTsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiAlYyAlZCIsICZvcCwgJm51bSk7CiAgICAgICAgaW50IHEgPSBzdC5xdWVyeShudW0pOwogICAgICAgIGlmIChvcCA9PSAnTCcpIHsKICAgICAgICAgICAgcHJpbnRmKCIlZFxuIiwgcSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc3QudXBkYXRlKHEsIC0xKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQ==