poj 2761 Feed the dogs (Treap+离线处理)

来源:互联网 发布:淘宝 串货 编辑:程序博客网 时间:2024/05/22 11:38

Feed the dogs
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 21218 Accepted: 6713

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

Source

POJ Monthly--2006.02.26,zgl & twb


用treap写的第一个题,纪念一下。

思路:因为题目说了这些数据没有包含关系,所以可以推出l1<l2&&r1<r2。所以先将输入的这些数据存起来,再按l的升序排一下,然后将要查询的左端点以左的数据删除,将要查询的区间的数据都加入。不断重复就行了。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m,size,root,ans;struct data{    int l,r,v,size,rnd,w;}tr[100005];void update(int k){    tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;}void rturn(int &k){    int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;    tr[t].size=tr[k].size;update(k);k=t;}void lturn(int &k){    int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;    tr[t].size=tr[k].size;update(k);k=t;}void insert(int &k,int x){    if(k==0)    {        size++;k=size;        tr[k].size=tr[k].w=1;tr[k].v=x;tr[k].rnd=rand();        return;    }    tr[k].size++;    if(tr[k].v==x) tr[k].w++;    else if(x>tr[k].v)    {        insert(tr[k].r,x);        if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k);    }    else    {        insert(tr[k].l,x);        if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k);    }}void del(int &k,int x){    if(k==0) return;    if(tr[k].v==x)    {        if(tr[k].w>1)        {            tr[k].w--;tr[k].size--;            return;        }        if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r;        else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd)            rturn(k),del(k,x);        else lturn(k),del(k,x);    }    else if(x>tr[k].v)        tr[k].size--,del(tr[k].r,x);    else tr[k].size--,del(tr[k].l,x);}int query_rank(int k,int x){    if(k==0) return 0;    if(tr[k].v==x) return tr[tr[k].l].size+1;    else if(x>tr[k].v)        return tr[tr[k].l].size+tr[k].w+query_rank(tr[k].r,x);    else return query_rank(tr[k].l,x);}int query_num(int k,int x){    if(k==0) return 0;    if(x<=tr[tr[k].l].size)        return query_num(tr[k].l,x);    else if(x>tr[tr[k].l].size+tr[k].w)        return query_num(tr[k].r,x-tr[tr[k].l].size-tr[k].w);    else return tr[k].v;}struct data2{    int l,r,rnk,id,ans;}q[50005];int f[100005];bool cmp1(data2 a,data2 b){    return a.l<b.l;}bool cmp2(data2 a,data2 b){    return a.id<b.id;}int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)            scanf("%d",&f[i]);        for(int i=1;i<=m;i++)            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].rnk),q[i].id=i;        sort(q+1,q+1+m,cmp1);        int l=1,r=1;        for(int i=1;i<=m;i++)        {            while(l<q[i].l)            {                if(l<r)                    del(root,f[l]);                l++;            }            if(l>r) r=l;            while(r<=q[i].r)            {                insert(root,f[r]);                r++;            }            q[i].ans=query_num(root,q[i].rnk);        }        sort(q+1,q+1+m,cmp2);        for(int i=1;i<=m;i++)            printf("%d\n",q[i].ans);    }    return 0;}

原创粉丝点击