2761 Feed the dogs 임의의 구간 의 k 소수 선분 트 리 + 구분 트 리 구하 기

4516 단어 acm_데이터 구조
Feed the dogs
Time Limit: 6000MS
 
Memory Limit: 65536K
Total Submissions: 8160
 
Accepted: 2218
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
http://blog.sina.com.cn/s/blog_5f5353cc0100ki2e.html
#include
#include
#include
#include
using namespace std;
const int N=101000;
const int DEEP=20;//
struct Node
{
    int l,r;
};
Node tree[N*4];//
int data[N];//
int seg[DEEP][N];//
int LessMid[DEEP][N];// [L,R] date[mid]
void buildtree(int root,int l,int r,int d)//  ,    ,  
{
    tree[root].l=l,tree[root].r=r;
    if(l==r) return ;//
    int mid=(l+r)>>1;
    int lsame=mid-l+1;// data[mid]
    for(int i=l;i<=r;i++)//
    {
        if(seg[d][i]    }
    int tl=l,tr=mid+1,same=0;//    ,tl,tr           
    for(int i=l;i<=r;i++)
    {
        if(i==l) LessMid[d][i]=0;// [L,R] date[mid]
        else LessMid[d][i]=LessMid[d][i-1];
        if(seg[d][i]        else if(seg[d][i]>data[mid]) seg[d+1][tr++]=seg[d][i];//      
        else //
        {
            if(same            else seg[d+1][tr++]=seg[d][i];
        }
    }
    buildtree(root<<1,l,mid,d+1);
    buildtree((root<<1)+1,mid+1,r,d+1);
}
// [l,r] k
int Query(int root,int l,int r,int d,int cnt)// , , , cnt
{
    if(l==r) return seg[d][l];
    int s;//   [l,r]        data[mid]   
    int ss;// [tree[root].l,l-1] data[mid]
    if(l==tree[root].l) s=LessMid[d][r],ss=0;
    else s=LessMid[d][r]-LessMid[d][l-1],ss=LessMid[d][l-1];
    if(s>=cnt) return Query(root<<1,tree[root].l+ss,tree[root].l+ss+s-1,d+1,cnt);
    else
    {
        int mid=(tree[root].l+tree[root].r)>>1;
        int bb=l-tree[root].l-ss;// [tree[root].l,l-1]
        int b=r-l+1-s;// [l,r]
        return Query((root<<1)+1,mid+bb+1,mid+bb+b,d+1,cnt-s);
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&data[i]);
            seg[1][i]=data[i];//
        }
        sort(data+1,data+1+n);
        buildtree(1,1,n,1);
        while(m--)
        {
            int l,r,k;scanf("%d%d%d",&l,&r,&k);
            int res=Query(1,l,r,1,k);// [l,r] k
            printf("%d/n",res);
        }
    }
    return 0;
}

좋은 웹페이지 즐겨찾기