poj2761(Treap)

Feed the dogs
Time Limit: 6000MS Memory Limit: 65536K
Total 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 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2
这题其实最好最快的解法是划分树,,然而正好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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值