nicuvlad76

Untitled

Dec 10th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 100002
  3. #define vmax 1000000000
  4. #define pb push_back
  5. using namespace std;
  6. ifstream fin("easyxy.in");
  7. ofstream fout("easyxy.out");
  8. vector <int> arb[4*nmax+55];
  9. int a[nmax];
  10. int n,m;
  11. int i;
  12. void build(int nod, int st, int dr)
  13. {
  14. if(st==dr)
  15. {
  16. arb[nod].pb(a[st]);
  17. return;
  18. }
  19. int mij=(st+dr)/2;
  20. build(2*nod,st,mij);
  21. build(2*nod+1,mij+1,dr);
  22. int i,j;
  23. for(i=0,j=0;i<(int)arb[2*nod].size() && j<(int)arb[2*nod+1].size();)
  24. {
  25. if(arb[2*nod][i]<=arb[2*nod+1][j])
  26. arb[nod].pb(arb[2*nod][i++]);
  27. else arb[nod].pb(arb[2*nod+1][j++]);
  28. }
  29. while(i<(int)arb[2*nod].size())
  30. arb[nod].pb(arb[2*nod][i++]);
  31. while(j<(int)arb[2*nod+1].size())
  32. arb[nod].pb(arb[2*nod+1][j++]);
  33. }
  34. int lwr;
  35. bool ok;
  36. void query(int nod, int st, int dr, int a, int b, int val)
  37. {
  38. if(a<=st && dr<=b)
  39. {
  40. int p=upper_bound(arb[nod].begin(),arb[nod].end(),val)-arb[nod].begin();
  41. lwr+=p;
  42. if(p!=0)
  43. if(arb[nod][p-1]==val)ok=1;
  44. return;
  45. }
  46. int mij=(st+dr)/2;
  47. if(a<=mij)
  48. query(2*nod,st,mij,a,b,val);
  49. if(b>mij)
  50. query(2*nod+1,mij+1,dr,a,b,val);
  51. }
  52. int sol(int st, int dr, int poz)
  53. {
  54. int s=1,d=n,m,best=-1;
  55. while(s<=d)
  56. {
  57. m=s+(d-s)/2;
  58. lwr=0;
  59. ok=0;
  60. query(1,1,n,st,dr,a[m]);
  61. if(st+lwr-1>=poz)
  62. {
  63. if(ok==1)
  64. {
  65. best=a[m];
  66. if(st+lwr-1==poz)return a[m];
  67. }
  68. d=m-1;
  69. }
  70. else s=m+1;
  71. }
  72. return best;
  73. }
  74. int x,y,p;
  75. int main()
  76. {
  77. fin>>n>>m;
  78. for(i=1;i<=n;i++)
  79. fin>>a[i];
  80. build(1,1,n);
  81. for(i=1;i<=n;i++)
  82. a[i]=arb[1][i-1];
  83. while(m--)
  84. {
  85. fin>>x>>y>>p;
  86. fout<<sol(x,y,p)<<"\n";
  87. }
  88. return 0;
  89. }
Add Comment
Please, Sign In to add comment