Maruf_Hasan

MKTHNUMBER.cpp

Apr 6th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. //In the Name of Allah Most Gracious, Most Merciful//
  2. /*If you want something you've never had, you have to do something you never did.*/
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. #define pb push_back
  9. #define ll long long
  10. #define pii pair<int,int>
  11. #define pll pair<ll,ll>
  12. #define M 100007
  13. #define INF 1e9
  14. #define INFL 1e18
  15. #define PI acos(-1)
  16. #define mp make_pair
  17. #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  18. #define MaxN 100010
  19.  
  20.  
  21. //const int fx[]= {+1,-1,+0,+0};
  22. //const int fy[]= {+0,+0,+1,-1};
  23. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  24. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  25. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  26. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  27.  
  28.  
  29. vector<int>tree[MaxN*4];
  30. int arr[MaxN];
  31. int n;
  32.  
  33.  
  34. void build(int node,int b,int e)
  35. {
  36. if(b==e)
  37. {
  38. // cout<<arr[b]<<endl;
  39. tree[node]=vector<int>(1,arr[b]);
  40. return;
  41. }
  42. int mid=(b+e)/2;
  43. int left=node*2;
  44. int right=node*2+1;
  45. build(left,b,mid);
  46. build(right,mid+1,e);
  47. merge(tree[left].begin(),tree[left].end(),tree[right].begin(),tree[right].end(),back_inserter(tree[node]));
  48. }
  49.  
  50. int query(int node,int b,int e,int i,int j,int val)
  51. {
  52. if(i>e || j<b)
  53. {
  54. return 0;
  55. }
  56. if(b>=i && e<=j)
  57. {
  58. int ans=upper_bound(tree[node].begin(),tree[node].end(),val-1)-tree[node].begin();
  59. return ans;
  60. }
  61. int mid=(b+e)/2;
  62. int left=node*2;
  63. int right=node*2+1;
  64. return query(left,b,mid,i,j,val)+query(right,mid+1,e,i,j,val);
  65. }
  66. int main()
  67. {
  68. fast_in_out;
  69. //freopen("input.txt","r",stdin);
  70. //freopen("output.txt","w",stdout)
  71. int q;
  72. cin>>n>>q;
  73. for(int i=1;i<=n;i++)
  74. {
  75. cin>>arr[i];
  76. }
  77. build(1,1,n);
  78. //cout<<tree[1].size()<<endl;
  79. // for(int i=0;i<14;i++)
  80. // {
  81. // for(int j=0;j<tree[i].size();j++)
  82. // {
  83. // cout<<tree[i][j]<<" ";
  84. // }
  85. // cout<<endl;
  86. // }
  87. sort(arr+1,arr+n+1);
  88. while(q--)
  89. {
  90.  
  91. int x,y,kth;
  92. cin>>x>>y>>kth;
  93. int high=n,low=1;
  94. int mid;
  95. int ans;
  96. while(high>=low)
  97. {
  98. mid=(high+low)/2;
  99. int pos=query(1,1,n,x,y,arr[mid]);
  100. cout<<arr[mid]<<" "<<pos<<endl;
  101. if(pos==kth)
  102. {
  103. // cout<<arr[mid]<<endl;
  104. // cout<<arr[high]<<endl;
  105. ans=mid;
  106. break;
  107. }
  108. else if(pos>kth)
  109. {
  110. high=mid-1;
  111. }
  112. else
  113. {
  114. low=mid+1;
  115. }
  116. }
  117. cout<<arr[ans-1]<<endl;
  118.  
  119. }
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment