MY*****

SBT的应用——求区间第k值——poj2761

 

                                      Feed the dogs

 

Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 5092 Accepted: 1348

 

Description

       Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other. 

       Your task is to help Jiajia calculate which dog ate the food after each feeding.

Input

      The first line contains n and m, indicates the number of dogs and the number of feedings. 

      The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier. 

      Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding. 

      You can assume that n<100001 and m<50001.

Output

      Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

Sample Input

7 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2

 

题目大意:给定n个数值,给定m个区间和m个对应的k值,求这个区间的第k大值。我们可以采用这种方法:因为要求m个区间的第k大值,如果考虑到使用平衡树,那么必须保证总的效率是O(NlogN)。既然区间不会有重合的地方,我们就可以采用离散化的方式,将m个区间排序,这是可以保证后面的一个区间要么不包括前面的区间,要么有一部分重合,对于重合我们不必重新插入数据,这样可以保证每一个数据最多被插入一次、删除一次。

 

type
  arr=array[0..200000] of longint;
var
  l,r,a,v,x,y,k,ans,s,lik:arr;
  n,m,p,t,tt,front,rear,i:longint;
procedure left(var x:longint);
var
  y:longint;
begin
  y:=r[x];
  r[x]:=l[y];
  l[y]:=x;
  v[y]:=v[x];
  v[x]:=v[l[x]]+v[r[x]]+1;
  x:=y;
end;

procedure right(var x:longint);
var
  y:longint;
begin
  y:=l[x];
  l[x]:=r[y];
  r[y]:=x;
  v[y]:=v[x];
  v[x]:=v[l[x]]+v[r[x]]+1;
  x:=y;
end;

procedure maintain(var x:longint; f:boolean);
begin
  if f=false then
    begin
      if v[l[l[x]]]>v[r[x]] then
        right(x) else
      if v[r[l[x]]]>v[r[x]] then
        begin
          left(l[x]);
          right(x);
        end
      else exit
    end
  else
    begin
      if v[r[r[x]]]>v[l[x]] then
        left(x) else
      if v[l[r[x]]]>v[l[x]] then
        begin
          right(r[x]);
          left(x);
        end
      else exit
    end;
  maintain(l[x],false);
  maintain(r[x],true);
  maintain(x,true);
  maintain(x,false);
end;

procedure insert(var x:longint; y:longint);
begin
  if x=0 then
    begin
      inc(tt);
      x:=tt;
      a[x]:=y;
      v[x]:=1;
      l[x]:=0;
      r[x]:=0;
      exit;
    end;
  inc(v[x]);
  if y<a[x] then insert(l[x],y)
            else insert(r[x],y);
  maintain(x,x>=a[x]);
end;

function delete(var x:longint; y:longint):longint;
begin
  dec(v[x]);
  if (a[x]=y)or((y>a[x])and(r[x]=0))or((y<a[x])and(l[x]=0)) then
    begin
      delete:=a[x];
      if (l[x]=0)or(r[x]=0) then
        begin
          x:=l[x]+r[x];
          exit;
        end;
      a[x]:=delete(l[x],a[x]+1);
      exit;
    end;
  if y<a[x] then delete:=delete(l[x],y)
            else delete:=delete(r[x],y);
end;

function find(x,y:longint):longint;
begin
  if v[l[x]]+1=y then exit(a[x]);
  if y<=v[l[x]] then exit(find(l[x],y));
  if y>v[l[x]] then exit(find(r[x],y-v[l[x]]-1));
end;

procedure swap(var x,y:longint);
var
  k:longint;
begin
  k:=x;
  x:=y;
  y:=k;
end;

procedure qs(s,t:longint);
var
  i,j,xx,yy:longint;
begin
  i:=s; j:=t; xx:=x[(i+j)>>1]; yy:=y[(i+j)>>1];
  repeat
    while (x[i]<xx)or((x[i]=xx)and(y[i]<yy)) do inc(i);
    while (x[j]>xx)or((x[j]=xx)and(y[j]>yy)) do dec(j);
    if i<=j then
      begin
        swap(x[i],x[j]);
        swap(y[i],y[j]);
        swap(k[i],k[j]);
        swap(lik[i],lik[j]);
        inc(i);
        dec(j);
      end;
  until i>j;
  if i<t then qs(i,t);
  if j>s then qs(s,j);
end;

begin
  readln(n,m);
  for i:=1 to n do read(s[i]); readln;
  for i:=1 to m do readln(x[i],y[i],k[i]);
  for i:=1 to m do lik[i]:=i;
  qs(1,m);
  tt:=0;
  t:=0;
  front:=x[1];
  rear:=x[1]-1;
  for i:=1 to m do
    begin
      while rear<y[i] do
        begin
          inc(rear);
          insert(t,s[rear]);
        end;
      while front<x[i] do
        begin
          delete(t,s[front]);
          inc(front);
        end;
      ans[lik[i]]:=find(t,k[i]);
    end;
  for i:=1 to m do writeln(ans[i]);
end.

 

这里采用的是CQF大牛的SBT,当然可以采用线段树或者树状数组,这里不作介绍。

如果对于SBT不太了解,请:http://www.cnblogs.com/reflec94/archive/2011/01/22/1942095.html

posted on 2011-01-22 17:40  reflec94  阅读(620)  评论(0编辑  收藏  举报

导航