poj2104 K-th Number
来源:互联网 发布:linux的图形界面工具 编辑:程序博客网 时间:2024/06/04 19:34
题目:给你n个数,m个查询,每次查询形如(L,R,K),询问区间[L,R]区间内第K小的数
思路:主席树
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define PI acos(-1.0)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3fconst int maxn=1e5+50;const int maxm=maxn*30;int arr[maxn];//原数组int num[maxn],siz;//num离散化之后的数组int root[maxn],lson[maxm],rson[maxm],cnt[maxm];//根节点,左儿子,右儿子,区间内的数出现次数的前缀和int tot;void init(){ tot=1; root[0]=lson[0]=rson[0]=cnt[0]=0;//建一棵空树 sort(num+1,num+siz+1);//离散化 siz=unique(num+1,num+siz+1)-num-1;}int Hash(int x){ return lower_bound(num+1,num+siz+1,x)-num;}int update(int rt,int pos,int val){ int newrt=tot++,temp=newrt; cnt[newrt]=cnt[rt]+val; int l=1,r=siz; while(l<r){ int mid=(l+r)/2; if(pos<=mid){ r=mid; lson[newrt]=tot++;rson[newrt]=rson[rt]; newrt=lson[newrt];rt=lson[rt]; } else{ l=mid+1; lson[newrt]=lson[rt];rson[newrt]=tot++; newrt=rson[newrt];rt=rson[rt]; } cnt[newrt]=cnt[rt]+val; } return temp;}int query(int left_rt,int right_rt,int k){ int l=1,r=siz; while(l<r){ int mid=(l+r)/2; int temp=cnt[lson[right_rt]]-cnt[lson[left_rt]]; if(temp>=k){ r=mid; left_rt=lson[left_rt]; right_rt=lson[right_rt]; } else{ l=mid+1; k-=temp; left_rt=rson[left_rt]; right_rt=rson[right_rt]; } } return l;}int main(){ int n,m,L,R,K; while(~scanf("%d%d",&n,&m)){ siz=0; for(int i=1;i<=n;i++){ scanf("%d",&arr[i]); num[++siz]=arr[i]; } init(); for(int i=1;i<=n;i++) root[i]=update(root[i-1],Hash(arr[i]),1); while(m--){ scanf("%d%d%d",&L,&R,&K); int ans=query(root[L-1],root[R],K); printf("%d\n",num[ans]); } } return 0;}
阅读全文
0 0
- [poj2104]K-th Number
- [Poj2104]K-th Number
- [POJ2104]K-th Number
- poj2104 K-th Number
- poj2104 K-th Number
- POJ2104-K-th Number
- poj2104 K-th Number
- POJ2104:K-th Number
- poj2104 K-th Number
- POJ2104 k-th number
- 【poj2104】K-th Number
- poj2104 K-th Number
- Poj2104 K-th Number
- poj2104 K-th Number
- POJ2104-K-th Number
- POJ2104-K-th Number
- poj2104 K-th Number
- POJ2104 K-th Number
- easyui-tree的json字符串结构(1层、2层、3层、4层)
- TP5.0.3-OUR中德双语网站的总结
- 【SQL】更新oracle数据库中的时间为表中当前时间之前的一个小时,5个小时
- 自定义控件(10)数据回传IPostBackDataHandler
- win10共享文件
- poj2104 K-th Number
- 阻止默认事件行为和事件冒泡
- 多个input文字方框中,默认字体是灰色,,输入字时,字体变成黑色
- Centos7 can't connect ot Internet but IP & ssh from local works
- java数据类型
- 计算机视觉笔记(一) 初探计算机视觉
- (9)自定义对象:四种方式
- Oracle字符集的查看查询和Oracle字符集的设置修改
- 1978-全排列