Poj2761-Feed the dogs(伸展树求名次)

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

题意:查询一段区间的第k大值,而且题目保证查询区间是不存在包含关系。

解析:因为不存在包含关系,所以直接离线排序,向右插入元素,向右删除元素。利用treap树实现名次树的功能。

代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int INF=1e9+7;
const int maxn=100005;
int A[maxn],cnt;  //A数组保存数,cnt是节点标号,我是用数组模拟的
struct treap
{
    treap* son[2];  //左右儿子
    int v,s,r;
    treap(){ v=s=r=0; son[0]=son[1]=NULL; }
    treap(int nv,int nr);
    int rk(){ return son[0]->s+1; }  //排名,第几个数
    int cmp(int k)  //比较,如果相等返回-1,小于返回0,大于1
    {
        if(k==v) return -1;
        return k<v?0:1;
    }
    void pushup(){ s=son[0]->s+son[1]->s+1; }  //更新大小
}null,tr[maxn];
treap::treap(int nv,int nr)
{
    v=nv; r=nr;
    s=1;
    son[0]=son[1]=&null;
}
treap* NewNode(int x,int r)//建新节点
{
    tr[cnt]=treap(x,r);
    return tr+cnt++;
}
struct splaytree
{
    int Size;
    treap* root;
    splaytree(){ Size=0; root=&null; }
    void Rotate(treap* &t,int d)  //翻转操作
    {
        treap* p=t->son[d^1];
        t->son[d^1]=p->son[d];
        p->son[d]=t;
        t->pushup();  //要更新
        t=p;
        t->pushup();
    }
    void Insert(treap* &t,int x,int r) //插入
    {
        if(t==&null) //插入
        {
            t=NewNode(x,r); //申请新节点
            return;
        }
        int d=t->cmp(x);
        if(d==-1) d=1; //往右边走
        Insert(t->son[d],x,r); 
        if(t->son[d]->r > t->r) Rotate(t,d^1); //旋转
        t->pushup();
    }
    void Remove(treap* &t,int x) //删除
    {
        int d=t->cmp(x);
        if(d==-1)
        {
            if(t->son[0]==&null) t=t->son[1];
            else if(t->son[1]==&null) t=t->son[0];
            else
            {
                int d2=(t->son[0]->r > t->son[1]->r ? 1: 0);
                Rotate(t,d2);
                Remove(t->son[d2],x);
            }
        }
        else Remove(t->son[d],x);
        if(t!=&null) t->pushup();
    }
    int Query(treap* &t,int kth) //查询
    {
        if(t==&null||kth<=0||kth>t->s) return -1;
        int a=t->rk();
        if(kth==a) return t->v;
        else if(kth<a) return Query(t->son[0],kth);
        else return Query(t->son[1],kth-a);
    }
};
int N,M;
struct Ques
{
    int x,y,k,id;
    Ques(int x=0,int y=0,int k=0,int id=0):x(x),y(y),k(k),id(id){}
    bool operator < (const Ques& t) const
    {
        if(x!=t.x) return x<t.x;
        return y<t.y;
    }
}q[maxn];
int ans[maxn];
int main()
{
    scanf("%d%d",&N,&M);
        splaytree spt; cnt=0;
        for(int i=1;i<=N;i++) scanf("%d",&A[i]);
        int x,y,k;
        for(int i=0;i<M;i++) //输入
        {
            scanf("%d%d%d",&x,&y,&k);
            q[i]=Ques(x,y,k,i);
        }
        sort(q,q+M); //排序
        int f=1,r=1;
        for(int i=0;i<M;i++)
        {
            Ques& t=q[i];
            int x=t.x,y=t.y,k=t.k;
            for(;f<x;f++) if(f<r) spt.Remove(spt.root,A[f]);
            if(r<f) r=f;
            for(;r<=y;r++) spt.Insert(spt.root,A[r],rand());
            ans[t.id]=spt.Query(spt.root,k);  //保存答案
        }
        for(int i=0;i<M;i++) printf("%d\n",ans[i]);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/wust-ouyangli/p/5733955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值