HDU-2665 Kth number(主席树)
来源:互联网 发布:法院淘宝拍卖房产税费 编辑:程序博客网 时间:2024/06/05 10:50
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10810 Accepted Submission(s): 3317
Problem Description
Give you a sequence and ask you the kth big number of a inteval.
Input
The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
Output
For each test case, output m lines. Each line contains the kth big number.
Sample Input
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
Sample Output
2
题解:裸主席树
#include<bits/stdc++.h>using namespace std;const int MX = 1e5 + 5;int sum[MX*20],ls[MX*20],rs[MX*20],o[MX*20],tot;int a[MX],b[MX],sz;void build(int l,int r,int &rt){ rt=++tot; sum[rt]=0; if(l==r) return; int m=(l+r)>>1; build(l,m,ls[rt]); build(m+1,r,rs[rt]);}void PushUP(int rt){ sum[rt]=sum[ls[rt]]+sum[rs[rt]];}void update(int p,int l,int r,int prt,int &rt){ rt=++tot; if(l==r){ sum[rt]=sum[prt]+1; return; } int m=(l+r)>>1; ls[rt]=ls[prt];rs[rt]=rs[prt]; if(p<=m) update(p,l,m,ls[prt],ls[rt]); else update(p,m+1,r,rs[prt],rs[rt]); PushUP(rt);}//查询区间[l,r]内第k大的数int query(int k,int l,int r,int prt,int rt){ if(l==r) return l; int m=(l+r)>>1,cnt=sum[ls[rt]]-sum[ls[prt]]; if(k<=cnt) return query(k,l,m,ls[prt],ls[rt]); return query(k-cnt,m+1,r,rs[prt],rs[rt]);}int main(){ //freopen("in.txt","r",stdin); int T,n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+n+1); sz=unique(b+1,b+n+1)-b-1; tot=0; build(1,sz,o[0]); for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+sz+1,a[i])-b; for(int i=1;i<=n;i++) update(a[i],1,sz,o[i-1],o[i]); while(m--){ int l,r,k; scanf("%d%d%d",&l,&r,&k); printf("%d\n",b[query(k,1,sz,o[l-1],o[r])]); } }}
阅读全文
0 0
- HDU 2665 Kth number 主席树
- HDU 2665 kth number(主席树)
- Hdu-2665 Kth number(主席树)
- HDU 2665 Kth number (主席树)
- HDU-2665 Kth number(主席树)
- hdu 2665 Kth number(主席树)
- 主席树(Kth number,HDU 2665)
- hdu 2665 Kth number(主席树模板)
- HDU 2665-Kth number(主席树)
- hdu 2665 Kth number 主席树
- HDU 2665 Kth number(主席树)
- HDU 2665 Kth number 主席树,函数式线段树
- 主席树 —— HDU 2665 Kth number
- [HDU 2665] Kth number (主席树入门)
- HDU 2665 Kth number 主席树模板题
- [Hdu] 2665 Kth number(主席树模板题)
- HDU 2665 Kth number 主席树裸题
- HDOJ-2665 Kth number(主席树)
- c++编程经验总结1
- KNN和线性分类器
- caffe中去掉均值文件的classification.cpp
- 我对Java中的封装,继承,抽象类和接口的理解。
- Git安装及idea配置Git
- HDU-2665 Kth number(主席树)
- 工厂模式
- 关于CSS单位
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- windows10下配置adb.exe全目录可用的方法
- #比特币挖矿part2# 矿池协议
- 向DOS批处理文件传入任意个数的参数方法实现
- 我将进化成一条狗(5)——VR和AR
- c中main函数的参数篇