poj 2104 K-th Number(可持久线段树)

K-th Number

持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html

结构:http://www.docin.com/p-627462377.html

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = ;
int n, m, tot;
int a[N], x[N], xcnt;
int T[N*], ls[N*], rs[N*], sum[N*]; void build(int l, int r ,int &rt)
{
rt=++tot;
sum[rt]=;
if(l==r) return ;
int m=(l+r)>>;
build(l, m, ls[rt]);
build(m+,r,rs[rt]);
} void update(int last, int p, int l, int r, int &rt)
{
rt = ++tot;
ls[rt]=ls[last], rs[rt]=rs[last], sum[rt]=sum[last]+;
if(l==r) return ;
int m = (l+r)>>;
if(p<=m) update(ls[last], p, l, m, ls[rt]);
else update(rs[last], p, m+, r, rs[rt]);
} int query(int ss, int tt, int l, int r, int k)
{
if(l==r) return l;
int m = (l+r)>>;
int num = sum[ls[tt]] - sum[ls[ss]];
if(k<=num) return query(ls[ss], ls[tt], l, m, k);
else return query(rs[ss], rs[tt], m+, r, k-num);
return ;
} void solve()
{
tot = ;
for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
x[i] = a[i];
}
sort(x+, x+n+);
xcnt = unique(x+, x++n)-x-;
for(int i=; i<=n; i++) a[i] = lower_bound(x+, x+n+, a[i])-x;
build(, xcnt, T[]);
for(int i=; i<=n; i++) update(T[i-],a[i],,xcnt,T[i]);
int l, r, k;
while(m--)
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", x[query(T[l-],T[r],,xcnt,k)]);
}
} int main()
{
while(scanf("%d%d", &n, &m)> ) solve();
return ;
}
上一篇:HTML 标签


下一篇:PHP无锁内存nosql---Yac的实战