poj 2761 Feed the dogs 离线treap

Language:
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 15157 Accepted: 4670

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

Source




题意:给定一个n个元素的数组,m次询问,每次询问(l,r,k),即区间[l,r]中第k大的值,题目保证区间不存在包含关系,但可相交。

思路:先将m次询问存储,并对l从小到大排序,之后用treap动态维护第k大元素,存储到相应的res中。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+100;
int n,m;
int res[MAXN],a[MAXN];
struct node
{
    node *ch[2];
    int r,v,s;
    node(int v):v(v)
    {
        s=1;
        r=rand();
        ch[0]=ch[1]=NULL;
    }
    bool operator <(const node &rhs) const
    {
        return r<rhs.r;
    }
    int cmp(int x)
    {
        if(x==v)
            return -1;
        return x<v ? 0:1;
    }
    void maintain()
    {
        s=1;
        if(ch[0]!=NULL) s+=ch[0]->s;
        if(ch[1]!=NULL) s+=ch[1]->s;
    }
};
struct Treap
{
    inline void rotate(node * &o,int d)
    {
        node *k=o->ch[d^1];
        o->ch[d^1]=k->ch[d];
        k->ch[d]=o;
        o->maintain(); k->maintain();
        o=k;
    }
    inline void insert(node * &o,int x)
    {
        if(o==NULL) o=new node(x);
        else
        {
            int d=(x<o->v?0:1);
            insert(o->ch[d],x);
            if(o->ch[d]->r>o->r)
                rotate(o,d^1);
        }
        o->maintain();
    }
    inline void remove(node * &o,int x)
    {
        int d=o->cmp(x);
        if(d==-1)
        {
            node *u=o;
            if(o->ch[0]!=NULL && o->ch[1]!=NULL)
            {
                int d2=(o->ch[0]->r>o->ch[1]->r? 1:0);
                rotate(o,d2);remove(o->ch[d2],x);
            }
            else
            {
                if(o->ch[0]==NULL)
                    o=o->ch[1];
                else
                    o=o->ch[0];
                delete u;
            }
        }
        else
            remove(o->ch[d],x);
        if(o!=NULL)
            o->maintain();
    }
    inline int kth(node * o,int k)
    {
        if(o==NULL || k<=0 || k>o->s) return 0;
        int s=(o->ch[0]==NULL ? 0: o->ch[0]->s);
        if(k==s+1) return o->v;
        else if(k<=s) return kth(o->ch[0],k);
        else return kth(o->ch[1],k-s-1);
    }
}treap;
struct Q
{
    int l,r,k,id;
    bool operator < (const Q &rhs) const
    {
        return l<rhs.l;
    }
}query[MAXN];
int main()
{
        //freopen("text.txt","r",stdin);
        scanf("%d%d",&n,&m);
        node *rt=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].id=i;
        }
        sort(query+1,query+m+1);
        int l=query[1].l,r=query[1].l;
        for(int i=1;i<=m;i++)
        {
            while(l<query[i].l)
            {
                treap.remove(rt,a[l]);
                l++;
            }
            while(r<=query[i].r)
            {
                treap.insert(rt,a[r]);
                r++;
            }
            res[query[i].id]=treap.kth(rt,query[i].k);
        }
        for(int i=1;i<=m;i++)
            printf("%d\n",res[i]);
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值