Application of SBT -- finding the K value in the interval -- poj2761

Source: Internet
Author: User

 

Feed the dogs

 

Time Limit:6000 MS Memory Limit:65536 K
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 shoshould 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 serous. 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 shoshould 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 shoshould contain in the pretty value of the dog who got the food in the I-th feeding.

Sample Input

7 21 5 2 6 3 7 41 5 32 7 1

Sample Output

32

 

Given n values, given m intervals and m corresponding k values, find the k percentile of this interval. We can use this method: Because the k-th percentile of m intervals is required, if we consider using the balance tree, we must ensure that the overall efficiency is O (NlogN ). Since the intervals do not overlap, we can use discretization to sort m intervals. This ensures that the subsequent intervals do not include the previous ones, there is also a part of overlap. We do not need to re-insert data for overlap, so that each data can be inserted and deleted at most once.

 

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.
 

Here we use the SBT of CQF Daniel. Of course we can use a line segment tree or a tree array. We will not introduce it here.

If you are not familiar with SBT, please: http://www.cnblogs.com/reflec94/archive/2011/01/22/1942095.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.