COGS 1534 [NEERC 2004]K小数 主席树题解
来源:互联网 发布:python buffer类型使用 编辑:程序博客网 时间:2024/05/19 17:48
时间限制:2 s 内存限制:512 MB
【中文题意】
给出一个长度为n的序列a1~an,有m次询问(x,y,k),每次询问a[x]~a[y]内的第k小数。
输入第一行为n,m,第二行为a1~an,接下来m行是m个(x,y,k)。
由于数据较大,请使用C风格的输入输出。
1<=n<=100000,1<=m<=5000
【题目描述】
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1…n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: “What would be the k-th number in a[i…j] segment, if this segment was sorted?”
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2…5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
【输入格式】
The first line of the input file contains n — the size of the array, and m — the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values — the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
【输出格式】
For each question output the answer to it — the k-th number in sorted a[i…j] segment.
【样例输入】
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
【样例输出】
5
6
3
【提示】
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
【来源】
Northeastern Europe 2004, Northern Subregion
【题目来源】
北京大学 POJ 2104
显然是主席树模板题
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<queue>#include<set>const int MAXN=100000*20;using namespace std;int sz,root[MAXN],from,to,kk,n,m,a[MAXN];vector<int>v;struct Tree{ int lson,rson,sum;}tree[MAXN];int id(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}void modify(int x,int &y,int l,int r,int loc){ tree[++sz]=tree[x];y=sz;tree[y].sum++; if(l==r) return; int mid=(l+r)>>1; if(loc<=mid) modify(tree[x].lson,tree[y].lson,l,mid,loc); else modify(tree[x].rson,tree[y].rson,mid+1,r,loc); }int query(int x,int y,int k,int l,int r){ if(l==r) return l; int mid=(l+r)>>1; int t=tree[tree[y].lson].sum-tree[tree[x].lson].sum; if(t>=k) query(tree[x].lson,tree[y].lson,k,l,mid); else query(tree[x].rson,tree[y].rson,k-t,mid+1,r);}int main(){ freopen("kthnumber.in","r",stdin); freopen("kthnumber.out","w",stdout); scanf("%d%d",&n,&m); for(register int i=1;i<=n;i++)scanf("%d",&a[i]),v.push_back(a[i]); sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end()); for(register int i=1;i<=n;i++)modify(root[i-1],root[i],1,n,id(a[i])); while(m--){ scanf("%d%d%d",&from,&to,&kk); printf("%d\n",v[query(root[from-1],root[to],kk,1,n)-1]); } return 0;}
- COGS 1534 [NEERC 2004]K小数 主席树题解
- K小数查询主席树
- zoj-2112(主席树动态求区间第k小数)
- 【COGS】257 动态排名系统 【动态第K小】树状数组+主席树
- COGS-930-找第k小的数-HNOI2012-主席树
- 【POJ】【P2104&P2761】【题解】【区间K大】【主席树】
- [POJ2104] K-th Number(区间K小数,主席树模板)
- COGS-257-动态排名系统-树状数组+主席树
- k-th number(主席树 )
- NEERC 2014 题解(待续)
- cogs 机器人搬运 题解
- COGS 首遇lancer 题解
- HDU 4866 Shooting 题解:主席树
- BZOJ 3524: [Poi2014]Couriers 主席树题解
- 【主席树】BZOJ3524(Poi2014)[Couriers]题解
- 线段树--第k小数
- POJ2104--K-th Number(主席树)
- 【差分主席树】zjoi2013 k大数
- LARC DL笔记(二) 训练自己的img
- maven简介
- 实现Logistic回归
- Oracle查询语句
- 未使用宏的代码
- COGS 1534 [NEERC 2004]K小数 主席树题解
- CC2640R2F BLE5.0 CC2640R2F UART驱动
- laraval 关于Packets out of order
- 原码、反码、补码和移码其实很简单
- 备用
- LeetCode 14. Longest Common Prefix
- Java网络编程
- POJ 1308 Is It A Tree? 基础并查集★
- 运行wampserver遇到的vcruntime.dll丢失问题(初搞php的朋朋们来这看啊)