POJ 2104 K-th Number

主席树模板~~~~

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#define maxn 100005
using namespace std;
int root[maxn],ls[maxn*20],rs[maxn*20],sum[maxn*20];
int tot=0,n,m,a,b,c,num[maxn],hash[maxn];
void build(int left,int right,int fath,int ftype)
{
int rt=++tot;
if (ftype==1) ls[fath]=rt;
else rs[fath]=rt;
sum[rt]=0;
if (left==right) return;
int mid=(left+right)>>1;
build(left,mid,rt,1);
build(mid+1,right,rt,2);
}
void update(int left,int right,int p,int last,int rt,int fath,int ftype)
{
rt=++tot;
if (ftype==1) ls[fath]=rt;
else rs[fath]=rt;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+1;
if (left==right) return;
int mid=(left+right)>>1;
if (p<=mid) update(left,mid,p,ls[last],ls[rt],rt,1);
else update(mid+1,right,p,rs[last],rs[rt],rt,2);
}
int query(int left,int right,int c,int last,int rt)
{
if (left==right) return left;
int cnt=sum[ls[rt]]-sum[ls[last]];
int mid=(left+right)>>1;
if (c<=cnt) return query(left,mid,c,ls[last],ls[rt]);
else return query(mid+1,right,c-cnt,rs[last],rs[rt]);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
hash[i]=num[i];
}
sort(hash+1,hash+n+1);
int cnt=unique(hash+1,hash+n+1)-hash-1;
root[0]=1;
build(1,cnt,0,0);
for (int i=1;i<=n;i++)
num[i]=lower_bound(hash+1,hash+cnt+1,num[i])-hash;
for (int i=1;i<=n;i++)
{
root[i]=tot+1;
update(1,cnt,num[i],root[i-1],root[i],0,0);
}
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
int ans=query(1,cnt,c,root[a-1],root[b]);
printf("%d\n",hash[ans]);
}
return 0;
}

时间: 2024-11-05 22:07:30

POJ 2104 K-th Number的相关文章

POJ 2104:K-th Number(主席树静态区间k大)

题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var t:array[0..100000*50]of point; a,b,id,root:array[0..100000]of longint; n,i,m,x,y,k,v,len:longint; procedure qsort(l,h:longint); var i,j,t,m:longint; b

POJ 2104:K-th Number(整体二分)

http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了. 在这题二分可行的答案,根据这个答案,把询问操作丢在左右两个队列里面分别递归继续按这样处理.注释里写的很详细. 1 #include <iostream> 2 #include <cstdlib> 3 #

【POJ 2104】K-th Number

Description 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

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

Poj 2104区间第k大(归并树)

题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key ins

POJ 2104&amp;HDU 2665 Kth number(主席树入门+离散化)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP

K-th Number POJ - 2104 划分树

K-th Number 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

K-th Number POJ - 2104 (整体二分)

K-th Number POJ - 2104 之前学主席树写了一遍 最近再看CDQ分治和整体二分,一直不是很理解,看着别人代码稍微理解了一些 1 //比主席树慢了挺多 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 const int maxn = 1e5 + 10; 9 const int maxq = 5010; 10 const

poj 2104主席树求区间第k小

POJ - 2104 题意:求区间第k小 思路:无修改主席树 AC代码: #include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&