Guest User

Untitled

a guest
Jan 27th, 2014
261
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Lasha Bukhnikashvili
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<iomanip>
  6. #include<algorithm>
  7. #include<vector>
  8. #include<map>
  9. #include<queue>
  10. #include<string>
  11. #define Pi 3.14159265358
  12. #define mod9 %1000000009
  13. #define INF 1000000000
  14. #define mod7 %1000000007
  15. #define LL  long long
  16. #define time clock()/(double)CLOCKS_PER_SEC
  17. using namespace std;
  18.  int i,l1,r1,mid,n,m,x,y,z,a[100001],b[100001];;
  19.  vector<int> T[400001];
  20.  void merge(int v){
  21.       int sz1=0,sz2=0,val1,val2;
  22.       while (sz1<T[2*v].size() || sz2<T[2*v+1].size()){
  23.             if (sz1<T[2*v].size()) val1=T[2*v][sz1]; else val1=2*INF+1;
  24.             if (sz2<T[2*v+1].size()) val2=T[2*v+1][sz2]; else val2=2*INF+1;
  25.             if (val1<val2){
  26.                T[v].push_back(val1);
  27.                sz1++;
  28.             } else {
  29.               T[v].push_back(val2);
  30.               sz2++;
  31.             };
  32.       };
  33.  };
  34.  int f(int v,int key){
  35.      int lx=0,midx,rx=T[v].size();
  36.      rx--;
  37.      while (lx<rx){
  38.            if (lx==rx-1){
  39.               if (T[v][rx]<=key) lx=rx;
  40.               break;
  41.            };
  42.            midx=(lx+rx)/2;
  43.            if (T[v][midx]<=key) lx=midx; else rx=midx-1;
  44.      };
  45.      if (T[v][lx]<=key) return lx+1; else return 0;
  46.  };
  47.  void BuildTree(int v,int tl,int tr){
  48.       if (tl==tr){
  49.          T[v].push_back(a[tl]);
  50.          return;
  51.       };
  52.       int tm=(tl+tr)/2;
  53.       BuildTree(2*v,tl,tm);
  54.       BuildTree(2*v+1,tm+1,tr);
  55.       merge(v);
  56.  };
  57.  int query(int v,int tl,int tr,int l,int r,int k){
  58.       if (l>r) return 0;
  59.       if (tl==l && tr==r) return f(v,k);
  60.       int tm=(tl+tr)/2;
  61.       return query(2*v,tl,tm,l,min(r,tm),k)+query(2*v+1,tm+1,tr,max(tm+1,l),r,k);
  62.  };
  63. int main(){
  64.  #ifndef ONLINE_JUDGE
  65.    freopen("input.txt","r",stdin);
  66.    freopen("output.txt","w",stdout);
  67.  #endif
  68.    scanf("%d%d",&n,&m);
  69.    for (i=1;i<=n;i++)
  70.        scanf("%d",&a[i]),b[i]=a[i];
  71.    sort(b+1,b+n+1);
  72.    BuildTree(1,1,n);
  73.    for (i=1;i<=m;i++){
  74.        scanf("%d%d%d",&x,&y,&z);
  75.        l1=1; r1=n;
  76.        while (l1<r1){
  77.              mid=(l1+r1)/2;
  78.              if (query(1,1,n,x,y,b[mid])<z) l1=mid+1; else r1=mid;
  79.        };
  80.        printf("%d\n",b[l1]);
  81.    };
  82.  return 0;
  83. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×