poj 2761 Feed the dogs(treap+离线)
来源:互联网 发布:js splice slice 编辑:程序博客网 时间:2024/05/16 23:58
题目链接:http://poj.org/problem?id=2761
思路:求区间第k小的树,题目中有一个关键的条件即没有一个区间能够包含另外一个区间,也就是说如果[ai,bi],[aj,bj]两个区间如果ai<aj那么bi<bj。做离线处理,将区间保存,并按照区间起始和终止位置排序,需要记录区间原始编号。然后按照区间的要求向treap中插入删除节点。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=50005;struct seg{ int st,en,kth,id,ans;}d[N];int data[N*2],e,n,m,root,ans;struct node{ int l,r,val,siz,pri;}a[N*2];bool cmp(const seg a,const seg b){ if(a.en!=b.en) return a.en<b.en; return a.st<b.st;}bool cmp1(const seg a,const seg b){ return a.id<b.id;}int Newnode(int val){ int x=e++; a[x].val=val; a[x].siz=1; a[x].l=a[x].r=-1; a[x].pri=rand(); return x;}void pushup(int k){ if(k==-1) return ; a[k].siz=1; if(a[k].l!=-1) a[k].siz+=a[a[k].l].siz; if(a[k].r!=-1) a[k].siz+=a[a[k].r].siz;}void rotl(int &x){ int y=a[x].r; a[x].r=a[y].l; a[y].l=x; pushup(x); pushup(y); x=y;}void rotr(int &x){ int y=a[x].l; a[x].l=a[y].r; a[y].r=x; pushup(x); pushup(y); x=y;}void insert(int &k,int val){ if(k==-1) k=Newnode(val); else if(val<a[k].val) { insert(a[k].l,val); if(a[a[k].l].pri>a[k].pri) rotr(k); } else { insert(a[k].r,val); if(a[a[k].r].pri>a[k].pri) rotl(k); } pushup(k);}void del(int &k,int val){ if(k==-1) return ; else if(val<a[k].val) del(a[k].l,val); else if(val>a[k].val) del(a[k].r,val); else { if(a[k].l==-1&&a[k].r==-1) k=-1; else if(a[k].l==-1) k=a[k].r; else if(a[k].r==-1) k=a[k].l; else { if(a[a[k].l].pri<a[a[k].r].pri) { rotl(k); del(a[k].l,val); } else { rotr(k); del(a[k].r,val); } } } pushup(k);}void query(int k,int x){ int siz; if(a[k].l==-1) siz=0; else siz=a[a[k].l].siz; if(siz+1==x) { ans=a[k].val; return ; } else if(x<=siz) query(a[k].l,x); else query(a[k].r,x-siz-1);}void output(int k){ if(k==-1) return ; printf("k=%d val=%d siz=%d\n",k,a[k].val,a[k].siz); if(a[k].l!=-1) output(a[k].l); if(a[k].r!=-1) output(a[k].r);}int main(){ int i,a,b,c,p,q,cnt; //freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) scanf("%d",&data[i]); for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); d[i].st=a-1; d[i].en=b-1; d[i].kth=c; d[i].id=i; } e=0; sort(d,d+m,cmp); root=-1;//注为-1 p=0; q=0; cnt=0; while(cnt<m) { while(q<=d[cnt].en) { insert(root,data[q]); q++; } while(p<d[cnt].st) { del(root,data[p]); p++; } query(root,d[cnt].kth); d[cnt].ans=ans; cnt++; //output(root); //printf("\n"); } sort(d,d+m,cmp1); for(i=0;i<m;i++) printf("%d\n",d[i].ans); } return 0;}
0 0
- poj 2761 Feed the dogs 离线treap
- POJ 2761 Feed the dogs [离线+treap]
- poj 2761 Feed the dogs(treap+离线)
- POJ 2761 Feed the dogs(Treap名次树+离线处理)
- poj 2761 Feed the dogs (Treap+离线处理)
- poj 2761 Feed the dogs treap
- POJ 2761 Feed the dogs (SBT+离线处理询问)
- POJ 2761 Feed the dogs(Treap求第K小数)
- POJ 2761 Feed the dogs Treap+离散处理
- POJ-2761-Feed the dogs
- POJ 2761 Feed the dogs
- POJ 2761 Feed the dogs
- POJ 2761 Feed the dogs
- poj 2761 Feed the dogs
- poj 2761 Feed the dogs
- POJ2761 Feed the dogs(treap)
- [Treap] poj2761 Feed the dogs
- Feed the dogs POJ
- 编写程序比较Brute-Force算法与KMP算法的效果
- ArrayBuffer:类型化数组
- 命令模式
- Stealing Harry Potter's Precious(DFS+BFS)
- 自定义调用
- poj 2761 Feed the dogs(treap+离线)
- application.run 某文件夹下的窗体
- 轻松搭建hadoop1.2.1集群前奏--虚拟机安装与配置
- Customer segmentation – LifeCycle Grids, CLV and CAC with R
- zoj 3329 One Person Game 概率dp
- Customer segmentation – LifeCycle Grids with R
- ubuntu下eclipse android SDK安装
- Socket通讯
- Webkit IDL的各种自定义接口