poj2761(Treap)

来源:互联网 发布:2016淘宝客怎么推广 编辑:程序博客网 时间:2024/05/22 15:46
Feed the dogs
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 17589 Accepted: 5524

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 21 5 2 6 3 7 41 5 32 7 1

Sample Output

32
这题其实最好最快的解法是划分树,,然而正好treap也能搞,就弄了一下,就是求区间第K小,把输入的每组查询存一下,按照左边排序然后依次维护区间就行了。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int M=100000+100;int a[M],ans[M];struct Node{    int size;    int rank;    int key;    Node *son[2];    Node(int key):key(key)    {        size=1;        rank=rand();        son[0]=son[1]=NULL;    }    bool operator < (const Node &a)const    {        return rank<a.rank;    }    int cmp(int x)const    {        if(x==key) return -1;        return x<key?0:1;    }    void update()    {        size=1;        if(son[0]!=NULL) size+=son[0]->size;        if(son[1]!=NULL) size+=son[1]->size;    }};struct Treap{    inline void rotate(Node* &o,int d)    {        Node *k=o->son[d^1];        o->son[d^1]=k->son[d];        k->son[d]=o;        o->update();        k->update();        o=k;    }    inline void build(Node* &o,int x)    {        if(o==NULL)            o=new Node(x);        else        {            int d=x<o->key?0:1;            build(o->son[d],x);            o->update();            if(o->rank<o->son[d]->rank)                rotate(o,d^1);        }        o->update();    }    inline void del(Node* &o,int x)    {        int d=o->cmp(x);        if(d==-1)        {            Node *u=o;            if(o->son[0]!=NULL && o->son[1]!=NULL)            {                int d2=(o->son[0]->rank>o->son[1]->rank?1:0);                rotate(o,d2);del(o->son[d2],x);            }            else            {                if(o->son[0]==NULL)                    o=o->son[1];                else                    o=o->son[0];                delete u;            }        }        else            del(o->son[d],x);        if(o!=NULL) o->update();    }    inline int kth(Node* o,int k)    {        if(o==NULL||k<=0||k>o->size) return 0;        int s=o->son[1]==NULL?0:o->son[1]->size;        if(k==s+1) return o->key;        else if(k<=s) return kth(o->son[1],k);        else return kth(o->son[0],k-s-1);    }} treap;struct sss{    int l,r,k,id;    bool operator < (const sss &a) const    {        return l<a.l;    }} query[M];int main(){    int n,m;    scanf("%d%d",&n,&m);    Node *root=NULL;    for(int i=1; i<=n; i++)        scanf("%d",&a[i]);    for(int i=1; i<=m; i++)    {        scanf("%d%d%d",&query[i].l,&query[i].r,&query[i].k);        query[i].k=query[i].r-query[i].l+2-query[i].k;        query[i].id=i;    }    sort(query+1,query+1+m);    int l=query[1].l,r=query[1].l;    for(int i=1; i<=m; i++)    {        while(l<query[i].l)        {            treap.del(root,a[l]);            l++;        }        while(r<=query[i].r)        {            treap.build(root,a[r]);            r++;        }        ans[query[i].id]=treap.kth(root,query[i].k);    }    for(int i=1; i<=m; i++)        printf("%d\n",ans[i]);    return 0;}


0 0