• Source
    1. #include <bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. struct node{
    6. int l,r,sum;
    7. node *left, *right;
    8.  
    9. node(int low,int high){
    10. l = low;
    11. r = high;
    12. sum = (high - low + 1);
    13. left = NULL;
    14. right = NULL;
    15. }
    16.  
    17. void extend(){
    18. if(left == NULL){
    19. int mid = (l+r)>>1;
    20. left = new node(l,mid);
    21. right = new node(mid+1,r);
    22. }
    23. }
    24. };
    25.  
    26. node *Root;
    27.  
    28. void updateSegTree(int index, int delta,node *root){
    29. if(index < root->l || index > root->r) return;
    30.  
    31. if(root->l == root->r){
    32. root->sum += delta;
    33. return;
    34. }
    35.  
    36. root->extend();
    37. updateSegTree(index,delta,root->left);
    38. updateSegTree(index,delta,root->right);
    39. root->sum = (root->left)->sum + (root->right)->sum;
    40. }
    41.  
    42. int query(int k,node *root){
    43. if(root->l == root->r) return root->l;
    44.  
    45. root->extend();
    46. if((root->left)->sum >= k) return query(k,root->left);
    47. return query(k - ((root->left)->sum),root->right);
    48. }
    49.  
    50. int main(){
    51. int n,m,num;
    52. char op;
    53.  
    54. scanf("%d%d",&n,&m);
    55.  
    56. Root = new node(1,n);
    57.  
    58. for(int i=1;i<=m;i++){
    59. scanf(" %c %d",&op,&num);
    60. int q = query(num,Root);
    61. if(op == 'L') printf("%d\n",q);
    62. else updateSegTree(q,-1,Root);
    63. }
    64. return 0;
    65. }
    66.