Advertisement
a53

easyxy

a53
Jan 22nd, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vector>
  4. #include <algorithm>
  5. #define Nmax 100005
  6. #define Bmax 320
  7. #define pb push_back
  8. #define INF 2147000000
  9. using namespace std;
  10.  
  11. vector< int > v[Bmax],aux;
  12. int N,M,buc,sz[Bmax],este[Nmax];
  13. int a[Nmax],aa[Nmax],ind[Nmax],Where[Nmax];
  14.  
  15. inline int cmp(int i,int j){ return aa[i]< aa[j]; }
  16.  
  17. inline void Query(int l,int r,int k){
  18. int i,j,p1,p2;
  19.  
  20. for(i=1;i<=buc;++i){
  21. p1=lower_bound(v[i].begin(), v[i].end(), l)-v[i].begin();
  22. if( v[i][p1]<l ) p1++;
  23. p2=upper_bound(v[i].begin(), v[i].end(), r)-v[i].begin();
  24.  
  25. if( p2-p1 < k ) k -= p2-p1;
  26. else break; // elem cautat de in v[i]
  27. }
  28.  
  29. aux.clear();
  30. for(j=p1; j<p2; ++j) // j<n-1
  31. aux.pb(aa[v[i][j]]);
  32.  
  33. nth_element(aux.begin(),aux.begin()+k-1,aux.end());
  34. printf("%d\n",*(aux.begin()+k-1));
  35. }
  36.  
  37. int main()
  38. {
  39. int i,j,Sqrt,x,y,k,cate=0;
  40. freopen("easyxy.in","r",stdin);
  41. freopen("easyxy.out","w",stdout);
  42. scanf("%d%d",&N,&M);
  43. for(i=1;i<=N;++i)
  44. scanf("%d",&aa[i]),ind[i]=i,este[i]=1;
  45. sort(ind+1,ind+N+1,cmp);
  46. // normalizare
  47. for(i=1;i<=N;++i)
  48. // if(aa[ind[i]]==aa[ind[i-1]]) a[ind[i]]=a[ind[i-1]];
  49. // else
  50. a[ind[i]]=i;
  51.  
  52. //Sqrt=sqrt(N);
  53. Sqrt=900;
  54. j=1; buc=0; // repartizare indici in galeti dupa valoarea idului
  55. for(i=1; j<=N; i+=Sqrt){
  56. ++buc;
  57. for( ; j<=N && a[ind[j]] < i+Sqrt; ++j){
  58. v[buc].pb(ind[j]);
  59. sz[buc]++;
  60. Where[ind[j]]=buc;
  61. }
  62. sort(v[buc].begin(),v[buc].end());
  63. v[buc].pb(INF); // ma ajuta la update
  64. }
  65.  
  66. for(i=1;i<=buc;++i) cate+=sz[i];
  67.  
  68. for(i=1;i<=M;++i)
  69. {
  70. scanf("%d%d%d",&x,&y,&k);
  71. k=k-x+1;
  72. Query(x,y,k);
  73. }
  74.  
  75. fclose(stdin); fclose(stdout);
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement