Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,m;
  4. int values[100005];
  5. int roots[100005];
  6. map <int,int> coordcompress;
  7. int uncompress[100005];
  8. int curval = 0;
  9. vector <int> vals;
  10. int tree[5000005];
  11. int L[5000005];
  12. int R[5000005];
  13. int root[100005];
  14. int ir = 0;
  15. int nf = 1;
  16. #ifdef DEBUG
  17. #define D(x...)cout << x << '\n'
  18. #else
  19. #define D(x...)
  20. #endif // DEBUG
  21. void build(int id = ir, int l = 0, int r = n-1)
  22. {
  23. tree[id] = 0;
  24. if(l==r)return;
  25. int mid = (l+r)/2;
  26. L[id]=nf++;
  27. R[id]=nf++;
  28. build(L[id],l,mid);
  29. build(R[id],mid+1,r);
  30. tree[id]=tree[L[id]]+tree[R[id]];
  31. }
  32. int upd(int p, int id, int l=0, int r = n-1)
  33. {
  34. int ID = nf++;
  35. tree[ID]=tree[id];
  36. if(l==r){
  37. tree[ID]++;
  38. return ID;
  39. }
  40. else{
  41. int mid = (l+r)/2;
  42. L[ID]=L[id];
  43. R[ID]=R[id];
  44. if(p<=mid){
  45. L[ID]=upd(p,L[ID],l,mid);
  46. }
  47. else{
  48. R[ID]=upd(p,R[ID],mid+1,r);
  49. }
  50. tree[ID]=tree[L[ID]]+tree[R[ID]];
  51. return ID;
  52. }
  53. }
  54. int curi, curj;
  55. int a,b,k;
  56. int query(int k,int node1=root[curj], int node2=root[curi-1], int l=0, int r = n-1)
  57. {
  58. //cout << l << " " << r << endl;
  59. int v1 = L[node1];
  60. int v2 = L[node2];
  61. int mid = (l+r)/2;
  62. //cout << tree[v1] << " " << tree[v2] << endl;
  63. //cout << l << " " << r << " " << mid << endl;
  64. if(l==r){
  65. return l;
  66. }
  67. if(tree[v1]-tree[v2]>=k){
  68. //cout << "yay" << endl;
  69. return query(k,v1,v2,l,mid);
  70. }
  71. int v3 = R[node1];
  72. int v4 = R[node2];
  73. int p = tree[v1]-tree[v2];
  74. //cout << mid+1 << " " << r << endl;
  75. return query(k-p,v3,v4,mid+1,r);
  76. }
  77. int main()
  78. {
  79. #ifdef DEBUG
  80. freopen("input.txt","r",stdin);
  81. #endif // DEBUG
  82. cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false);
  83. cin >> n >> m;
  84. for(int i=1;i<=n;i++){
  85. cin >> values[i];
  86. vals.push_back(values[i]);
  87. }
  88. sort(vals.begin(),vals.end());
  89. for(int i=0;i<vals.size();i++){
  90. coordcompress[vals[i]]=i;
  91. //cout << vals[i] << " " << coordcompress[vals[i]] << '\n';
  92. uncompress[i]=vals[i];
  93. }
  94. build();
  95. root[0] = 0;
  96. for(int i=1;i<=n;i++){
  97. root[i]=upd(coordcompress[values[i]],root[i-1]);
  98. }
  99. for(int i=0;i<m;i++){
  100. cin >> a >> b >> k;
  101. curi = a, curj = b;
  102. //cout << query(k) << '\n';
  103. cout << uncompress[query(k)] << '\n';
  104. }
  105. //cout << tree[root[7]] << '\n';*/
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement