fork(3) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define gc getchar_unlocked
  4. #define fo(i,n) for(i=0;i<n;i++)
  5. #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  6. #define ll long long
  7. #define si(x) scanf("%d",&x)
  8. #define sl(x) scanf("%lld",&x)
  9. #define ss(s) scanf("%s",s)
  10. #define pi(x) printf("%d\n",x)
  11. #define pl(x) printf("%lld\n",x)
  12. #define ps(s) printf("%s\n",s)
  13. #define deb(x) cout << #x << "=" << x << endl
  14. #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
  15. #define pb push_back
  16. #define mp make_pair
  17. #define F first
  18. #define S second
  19. #define all(x) x.begin(), x.end()
  20. #define clr(x) memset(x, 0, sizeof(x))
  21. #define sortall(x) sort(all(x))
  22. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  23. #define PI 3.1415926535897932384626
  24. typedef pair<int, int> pii;
  25. typedef pair<ll, ll> pl;
  26. typedef vector<int> vi;
  27. typedef vector<ll> vl;
  28. typedef vector<pii> vpii;
  29. typedef vector<pl> vpl;
  30. typedef vector<vi> vvi;
  31. typedef vector<vl> vvl;
  32. int mpow(int base, int exp);
  33. void ipgraph(int m);
  34. void dfs(int u, int par);
  35. const int mod = 1000000007;
  36. const int N = 1e5, M = N;
  37. //=======================
  38. const int MAX = 1e6;
  39. vi g[N];
  40. int a[N];
  41. struct wavelet_tree{
  42. #define vi vector<int>
  43. #define pb push_back
  44. int lo, hi;
  45. wavelet_tree *l, *r;
  46. vi b;
  47.  
  48. //nos are in range [x,y]
  49. //array indices are [from, to)
  50. wavelet_tree(int *from, int *to, int x, int y){
  51. lo = x, hi = y;
  52. if(lo == hi or from >= to) return;
  53. int mid = (lo+hi)/2;
  54. auto f = [mid](int x){
  55. return x <= mid;
  56. };
  57. b.reserve(to-from+1);
  58. b.pb(0);
  59. for(auto it = from; it != to; it++)
  60. b.pb(b.back() + f(*it));
  61. //see how lambda function is used here
  62. auto pivot = stable_partition(from, to, f);
  63. l = new wavelet_tree(from, pivot, lo, mid);
  64. r = new wavelet_tree(pivot, to, mid+1, hi);
  65. }
  66.  
  67. //kth smallest element in [l, r]
  68. int kth(int l, int r, int k){
  69. if(l > r) return 0;
  70. if(lo == hi) return lo;
  71. int inLeft = b[r] - b[l-1];
  72. int lb = b[l-1]; //amt of nos in first (l-1) nos that go in left
  73. int rb = b[r]; //amt of nos in first (r) nos that go in left
  74. if(k <= inLeft) return this->l->kth(lb+1, rb , k);
  75. return this->r->kth(l-lb, r-rb, k-inLeft);
  76. }
  77.  
  78. //count of nos in [l, r] Less than or equal to k
  79. int LTE(int l, int r, int k) {
  80. if(l > r or k < lo) return 0;
  81. if(hi <= k) return r - l + 1;
  82. int lb = b[l-1], rb = b[r];
  83. return this->l->LTE(lb+1, rb, k) + this->r->LTE(l-lb, r-rb, k);
  84. }
  85.  
  86. //count of nos in [l, r] equal to k
  87. int count(int l, int r, int k) {
  88. if(l > r or k < lo or k > hi) return 0;
  89. if(lo == hi) return r - l + 1;
  90. int lb = b[l-1], rb = b[r], mid = (lo+hi)/2;
  91. if(k <= mid) return this->l->count(lb+1, rb, k);
  92. return this->r->count(l-lb, r-rb, k);
  93. }
  94. ~wavelet_tree(){
  95. delete l;
  96. delete r;
  97. }
  98. };
  99.  
  100. int Tc,n,k[N],j,q,l[N],r[N],ans[N];
  101. vector<int> v;
  102. map<int,int> mep;
  103.  
  104. int main()
  105. {
  106. int idx = 1;
  107. v.clear();
  108. mep.clear();
  109. scanf("%d %d",&n,&q);
  110. for(int i=0;i<n;i++){
  111. scanf("%d",&a[i+1]);
  112. v.push_back(a[i+1]);
  113. }
  114. for(int i=0;i<q;i++){
  115. scanf("%d %d %d",&l[i],&r[i],&k[i]);
  116. }
  117. sort(v.begin(), v.end());
  118. for(int i=0;i<v.size();i++){
  119. if(mep.find(v[i])==mep.end()) mep[v[i]] = idx, ans[idx++] = v[i];
  120. }
  121. for(int i=0;i<n;i++){
  122. a[i+1] = mep[a[i+1]];
  123. }
  124. wavelet_tree T(a+1, a+n+1, 1, MAX);
  125. for(int i=0;i<q;i++){
  126. printf("%d\n", ans[T.kth(l[i], r[i], k[i])]);
  127. }
  128. return 0;
  129. }
  130.  
  131.  
Runtime error #stdin #stdout 0s 20368KB
stdin
Standard input is empty
stdout
Standard output is empty