hdu 2665 Kth number
来源:互联网 发布:亲和素淘宝店 编辑:程序博客网 时间:2024/06/05 10:37
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3329 Accepted Submission(s): 1107
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
分析:询问区间[s,t]内第k大的数
划分树:
#include<cstdio>#include<algorithm>using namespace std;const int N=100001;template<class T>inline bool scan_d(T &ret){ char c;bool sgn; if(c=getchar(),c==EOF)return 0; while(c!='-'&&(c<'0'||c>'9'))c=getchar(); sgn=(c=='-'); ret=sgn?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0'); if(sgn)ret=-ret; return 1;}int arr[N],sorted[N],val[18][N],toleft[18][N];void build(int l,int r,int d){ if(l==r)return; int m=(l+r)>>1; int same=m-l+1,i=l,j=m+1,k; for(k=l;k<=r;k++)if(val[d][k]<sorted[m])same--; for(k=l;k<=r;k++){ if(val[d][k]<sorted[m])val[d+1][i++]=val[d][k]; else if(val[d][k]==sorted[m]&&same>0){ val[d+1][i++]=val[d][k]; same--; }else val[d+1][j++]=val[d][k]; toleft[d][k]=toleft[d][l-1]+i-l; } build(l,m,d+1); build(m+1,r,d+1);}int query(int L,int R,int l,int r,int d,int k){ if(l==r)return val[d][l]; int m=(L+R)>>1; int cnt=toleft[d][r]-toleft[d][l-1]; if(cnt>=k){ int nl=L+toleft[d][l-1]-toleft[d][L-1]; int nr=nl+cnt-1; return query(L,m,nl,nr,d+1,k); }else{ int nr=r+toleft[d][R]-toleft[d][r]; int nl=nr-(r-l-cnt); return query(m+1,R,nl,nr,d+1,k-cnt); }}int main(){ int T,n,m,s,t,k,i; scanf("%d",&T); while(T--){ scan_d(n),scan_d(m); for(i=1;i<=n;i++){ scan_d(val[0][i]); sorted[i]=val[0][i]; } sort(sorted+1,sorted+n+1); build(1,n,0); while(m--){ scan_d(s),scan_d(t),scan_d(k); printf("%d\n",query(1,n,s,t,0,k)); } } return 0;}
归并树(但超时)
#include<cstdio>const int N=100002;struct MergeTree{ int l,r,m;}a[N<<2];int arr[N],val[18][N];void build(int l,int r,int d,int rt){ a[rt].l=l,a[rt].r=r,a[rt].m=(l+r)>>1; if(l==r){val[d][l]=arr[l];return ;} int m=a[rt].m; build(l,m,d+1,rt<<1); build(m+1,r,d+1,rt<<1|1); int i=l,j=m+1,k=l; while(i<=m&&j<=r){ if(val[d+1][i]<val[d+1][j])val[d][k++]=val[d+1][i++]; else val[d][k++]=val[d+1][j++]; } while(i<=m)val[d][k++]=val[d+1][i++]; while(j<=r)val[d][k++]=val[d+1][j++]; }int L,R;int lowb(int key,int d,int rt){ if(a[rt].l==a[rt].r){ return key<=val[d][a[rt].m]?a[rt].m:a[rt].m+1; } if(key<=val[d][a[rt].m])return lowb(key,d,rt<<1); else return lowb(key,d,rt<<1|1);}int find(int key,int d,int rt){ if(L<=a[rt].l&&R>=a[rt].r)return lowb(key,d,rt)-a[rt].l; int ans=0; if(L<=a[rt].m)ans+=find(key,d+1,rt<<1); if(R>a[rt].m)ans+=find(key,d+1,rt<<1|1); return ans;}int query(int l,int r,int k){ int m,tmp; while(l<r){ m=(l+r+1)>>1; tmp=find(val[0][m],0,1); if(tmp<k)l=m; else r=m-1; } return l;}int main(){ int T,n,m,i,k; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%d",arr+i); build(1,n,0,1); while(m--){ scanf("%d%d%d",&L,&R,&k); printf("%d\n",val[0][query(1,n,k)]); } } return 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
- 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 划分树
- hdu 2665Kth-number 划分树
- HDU 2665 Kth number 划分树
- 黑马程序员 java高新技术笔记之反射
- 服务器备份与还原脚本
- 一步一步将你的C#程序部署到Android
- 黑马程序员 java高新技术笔记之装箱与拆箱,枚举
- php中的Sessions详解
- hdu 2665 Kth number
- 转载:PostgreSQL内核分析 (1)
- 为什么日志只应该有三个级别
- hdu1240 Asteroids! (BFS)
- C# 汉字转拼音与汉字简繁转换
- mysql备份与恢复
- Mac Eclipse SVN 安装
- jQuery 基本语法
- hdu1372 Knight Moves (BFS)