POJ 2761(求区间第k小值)
来源:互联网 发布:华迈摄像机软件 编辑:程序博客网 时间:2024/06/06 00:24
Feed the dogs
Time Limit: 6000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
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.
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.
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
Hint
Source
POJ Monthly--2006.02.26,zgl & twb
题意:求区间的第k小值
思路:Treap,每次超出区间的删掉,不在区间内的加上,再找第k小值
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;const int maxn=100000+5;int ch[maxn][2],val[maxn],counts[maxn],r[maxn],size[maxn],tot,root;int num[maxn];struct A{ int l,r,k,id,ans;}a[50005];bool cmp1(A x,A y){ if(x.l == y.l) return x.r < y.r; return x.l < y.l;}bool cmp2(A x,A y){ return x.id < y.id;}//~~~~~~~~~~~~Treap模板~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~struct TREAP{ void newnode(int &rt,int v) { rt=++tot; val[rt]=v; ch[rt][0]=ch[rt][1]=0; counts[rt]=size[rt]=1; r[rt]=rand(); } inline void pushup(int rt) { size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+counts[rt]; } void rotate(int &x,int kind) { int y=ch[x][kind^1]; ch[x][kind^1]=ch[y][kind]; ch[y][kind]=x; pushup(x); pushup(y); x=y; } void insert(int &rt,int v) { if(rt==0) { newnode(rt,v); return ; } if(v==val[rt]) counts[rt]++; else { int kind=(v>val[rt]); insert(ch[rt][kind],v); if(r[ch[rt][kind]]<r[rt]) rotate(rt,kind^1); } pushup(rt); } int select(int rt,int k) { if(size[ch[rt][0]]>=k) return select(ch[rt][0],k); if(size[ch[rt][0]]+counts[rt]>=k) return val[rt]; return select(ch[rt][1],k-size[ch[rt][0]]-counts[rt]); } void remove(int &rt,int v) { if(val[rt]==v) { if(counts[rt]>1) counts[rt]--; else if(!ch[rt][0]&&!ch[rt][1]) {rt=0;return ;} else { int kind=r[ch[rt][0]]<r[ch[rt][1]]; rotate(rt,kind); remove(rt,v); } } else remove(ch[rt][v>val[rt]],v); pushup(rt); } void init() { ch[0][0]=ch[0][1]=0; size[0]=counts[0]=val[0]=0; tot=root=0; r[0]=(1LL<<31)-1; newnode(root,2000000001); }}treap;//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~int main(){ int n,m,lastl,lastr; while(~scanf("%d%d",&n,&m)) { treap.init(); for (int i=1; i<=n; i++) scanf("%d",&num[i]); for(int i = 1;i <=m ;i ++){ scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].k); a[i].id = i;}sort(a+1,a+1+m,cmp1);for(int i = 1;i <= m;i ++){ if(i == 1) { for(int j = a[i].l; j <= a[i].r;j ++)treap.insert(root, num[j]);a[i].ans = treap.select(root, a[i].k);lastl = a[i].l;lastr = a[i].r; } else { if (a[i].l>lastr) { for (int j=lastl; j<=lastr; j++) treap.remove(root,num[j]); for (int j=a[i].l; j<=a[i].r; j++) treap.insert(root,num[j]); } else { for (int j=lastl; j<a[i].l; j++) treap.remove(root,num[j]); for (int j=lastr+1; j<=a[i].r; j++) treap.insert(root,num[j]); } a[i].ans=treap.select(root,a[i].k); lastl=a[i].l; lastr=a[i].r; } } sort(a+1,a+1+m,cmp2); for(int i = 1;i <= m;i ++)printf("%d\n",a[i].ans); } return 0;}
0 0
- POJ 2761(求区间第k小值)
- POJ 2761-Feed the dogs(划分树)求区间内第k小的数
- POJ 2761(Treap离线求区间第k大数)
- 划分树(求区间第k小值)
- POJ 2104-K-th Number(划分树)求区间内第k小的数
- POJ 2761可以练习无数种数据结构的题——动态区间求第K小
- 区间第k小
- 求m个区间中第k小的数
- POJ2104:划分树求区间第k小
- poj 2104 (求区间第K大元素)
- POJ 1442 Black Box treap求区间第k大
- POJ 2104 (求区间第K大)
- poj 2761 Feed the dogs 求区间第k大的数
- poj 2761 多种数据结构算法求区间第k大的数
- POJ 2761 Feed the dogs 求区间第k大 划分树
- 划分树求一个数列任意区间的第k小值
- HDU 2665 求静态区间的第k小值 主席树讲解。
- poj 2104 区间第K小 主席树入门题
- 自定义控件其实很简单1/3
- 虚函数表再学习
- 女子自称教师逼小学生让座 将其书包扔出窗外
- 第三方登录(ShareSDK)
- 时分秒选择控件
- POJ 2761(求区间第k小值)
- Android USB相关广播的使用
- C#窗体中根据两点坐标绘制直线
- subst
- UItableView中编辑,删除,移动学习笔记
- 【LeetCode】(94)Binary Tree Inorder Traversal(Easy)
- 架构设计常见安全误区
- spring+activemq 发送10W消息报端口被占用的异常分析以及topic持久化订阅
- android edittext 拼写检查