• 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. {
    11. l = low;
    12. r = high;
    13. sum = (high - low + 1);
    14. left = NULL;
    15. right = NULL;
    16. }
    17.  
    18. void extend()
    19. {
    20. if (left == NULL) {
    21. int mid = (l + r) >> 1;
    22. left = new node(l, mid);
    23. right = new node(mid + 1, r);
    24. }
    25. }
    26. };
    27.  
    28. node* Root;
    29.  
    30. void updateSegTree(int index, int delta, node* root)
    31. {
    32. if (index < root->l || index > root->r)
    33. return;
    34.  
    35. if (root->l == root->r) {
    36. root->sum += delta;
    37. return;
    38. }
    39.  
    40. root->extend();
    41. updateSegTree(index, delta, root->left);
    42. updateSegTree(index, delta, root->right);
    43. root->sum = (root->left)->sum + (root->right)->sum;
    44. }
    45.  
    46. int query(int k, node* root)
    47. {
    48. if (root->l == root->r)
    49. return root->l;
    50.  
    51. root->extend();
    52. if ((root->left)->sum >= k)
    53. return query(k, root->left);
    54. return query(k - ((root->left)->sum), root->right);
    55. }
    56.  
    57. int main()
    58. {
    59. int n, m, num;
    60. char op;
    61.  
    62. scanf("%d%d", &n, &m);
    63.  
    64. Root = new node(1, n);
    65.  
    66. for (int i = 1; i <= m; i++) {
    67. scanf(" %c %d", &op, &num);
    68. int q = query(num, Root);
    69. if (op == 'L')
    70. printf("%d\n", q);
    71. else
    72. updateSegTree(q, -1, Root);
    73. }
    74. return 0;
    75. }