#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <cassert>
using namespace std;
const char infile[] = "input.in";
const char outfile[] = "output.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
struct Treap {
Treap *left, *right;
int key, subTree, priority;
Treap(Treap *_left, Treap *_right, int _key, int _priority, int _subTree = 1) {
left = _left;
right = _right;
key = _key;
priority = _priority;
subTree = _subTree;
}
} *Root, *NIL;
inline void updateSubTree(Treap *& Node) {
if(Node == NIL) {
Node->subTree = 0;
return;
}
Node->subTree = 1 + Node->left->subTree + Node->right->subTree;
}
inline void rotateLeft(Treap *& Node) {
Treap *aux = Node->left;
Node->left = aux->right;
aux->right = Node;
Node = aux;
updateSubTree(Node->right);
updateSubTree(Node);
}
inline void rotateRight(Treap *& Node) {
Treap *aux = Node->right;
Node->right = aux->left;
aux->left = Node;
Node = aux;
updateSubTree(Node->left);
updateSubTree(Node);
}
inline void Balance(Treap *& Node) {
if(Node->left->priority > Node->priority)
rotateLeft(Node);
if(Node->right->priority > Node->priority)
rotateRight(Node);
}
inline void Insert(Treap *& Node, int key, int pos, int priority) {
if(Node == NIL) {
Node = new Treap(NIL, NIL, key, priority, 1);
return;
}
if(pos <= Node->left->subTree + 1)
Insert(Node->left, key, pos, priority);
else Insert(Node->right, key, pos - Node->left->subTree - 1, priority);
Balance(Node);
updateSubTree(Node);
}
inline void Erase(Treap *& Node, int pos) {
if(Node == NIL)
return;
if(Node->left->subTree + 1 == pos) {
if(Node->left == NIL && Node->right == NIL) {
delete Node;
Node = NIL;
return;
}
else {
if(Node->left->priority > Node->right->priority) {
rotateLeft(Node);
Erase(Node->right, pos - Node->left->subTree - 1);
}
else {
rotateRight(Node);
Erase(Node->left, pos);
}
}
}
else if(Node->left->subTree + 1 > pos)
Erase(Node->left, pos);
else Erase(Node->right, pos - Node->left->subTree - 1);
updateSubTree(Node);
}
inline int KthElement(Treap *& Node, int k) {
if(Node == NIL)
return 0;
if(Node->left->subTree + 1 == k)
return Node->key;
else
if(Node->left->subTree + 1 > k)
return KthElement(Node->left, k);
else
return KthElement(Node->right, k - Node->left->subTree - 1);
}
inline void printTreap( Treap *& Node ) {
if(Node == NIL)
return;
printTreap(Node->left);
printTreap(Node->right);
}
int main() {
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen(infile, "r", stdin);
freopen(outfile, "w", stdout);
#endif
srand(time(NULL));
NIL = new Treap(0, 0, 0, 0, 0);
Root = NIL;
NIL->left = NIL->right = NIL;
int N, M;
scanf("%d%d", &N, &M);
for(int i = 1 ; i <= N ; ++ i)
Insert(Root, i, i, rand() + 1);
for(int t = 1 ; t <= M ; ++ t) {
char op; int x;
scanf(" %c %d", &op, &x);
if(op == 'D') Erase(Root, x);
else printf("%d\n", KthElement(Root, x));
}
fin.close();
fout.close();
return 0;
}
I2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGJpdHNldD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlID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