题目链接:http://poj.org/problem?id=2104

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 64277   Accepted: 22615
Case Time Limit: 2000MS

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 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.

Input

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).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion

划分树:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e5+; int tree[][MAXN];
int sorted[MAXN];
int toleft[][MAXN]; void build(int l, int r, int dep)
{
if(l==r) return;
int mid = (l+r)>>;
int same = mid-l+;
for(int i = l; i<=r; i++)
if(tree[dep][i]<sorted[mid])
same--; int lpos = l, rpos = mid+;
for(int i = l; i<=r; i++)
{
if(tree[dep][i]<sorted[mid])
tree[dep+][lpos++] = tree[dep][i];
else if(tree[dep][i]==sorted[mid] && same>)
{
tree[dep+][lpos++] = tree[dep][i];
same--;
}
else
tree[dep+][rpos++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l-] + lpos - l;
} build(l, mid, dep+);
build(mid+, r, dep+);
} int query(int L, int R, int l, int r, int dep, int k)
{
if(l==r) return tree[dep][l];
int mid = (L+R)>>;
int cnt = toleft[dep][r] - toleft[dep][l-]; if(cnt>=k)
{
int newl = L + toleft[dep][l-] - toleft[dep][L-];
int newr = newl + cnt - ;
return query(L, mid, newl, newr, dep+, k);
}
else
{
int newr = r + toleft[dep][R] - toleft[dep][r];
int newl = newr - (r-l-cnt);
return query(mid+, R, newl, newr, dep+, k-cnt);
}
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(tree, , sizeof(tree));
for(int i = ; i<=n; i++)
{
scanf("%d",&tree[][i]);
sorted[i] = tree[][i];
}
sort(sorted+, sorted++n);
build(, n, );
int s, t, k;
while(m--)
{
scanf("%d%d%d",&s,&t,&k);
printf("%d\n", query(,n,s,t,,k));
}
}
return ;
}

主席树(循环):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+;
const int M = MAXN*; int n, q, m, tot;
int a[MAXN], t[MAXN];
int T[MAXN], lson[M], rson[M], c[M]; void Init_hash()
{
for(int i = ; i<=n; i++)
t[i] = a[i];
sort(t+,t++n);
m = unique(t+,t++n)-(t+);
} int hash(int x)
{
return lower_bound(t+,t++m, x)-t;
} int build(int l, int r)
{
int root = tot++;
c[root] = ;
if(l!=r)
{
int mid = (l+r)>>;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
} int update(int root, int pos, int val)
{
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = , r = m;
while(l<r)
{
int mid = (l+r)>>;
if(pos<=mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid + ;
}
c[newroot] = c[root] + val;
}
return tmp;
} int query(int left_root, int right_root, int k)
{
int l = , r = m;
while(l<r)
{
int mid = (l+r)>>;
if(c[lson[left_root]]-c[lson[right_root]]>=k)
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + ;
k -= c[lson[left_root]]-c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
} int main()
{
while(scanf("%d%d",&n,&q)==)
{
tot = ;
for(int i = ; i<=n; i++)
scanf("%d",&a[i]);
Init_hash();
T[n+] = build(,m);
for(int i = n; i; i--)
{
int pos = hash(a[i]);
T[i] = update(T[i+],pos,);
}
while(q--)
{
int l, r, k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n", t[query(T[l],T[r+],k)]);
}
}
}

主席树(递归):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+;
const int M = MAXN*; int n, q, m, tot;
int a[MAXN], t[MAXN];
int T[MAXN], lson[M], rson[M], c[M]; void Init_hash()
{
for(int i = ; i<=n; i++)
t[i] = a[i];
sort(t+,t++n);
m = unique(t+,t++n)-(t+);
} int hash(int x)
{
return lower_bound(t+,t++m, x)-t;
} int build(int l, int r)
{
int root = tot++;
c[root] = ;
if(l!=r)
{
int mid = (l+r)>>;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
} int update(int root, int l, int r, int pos, int val)
{
int newroot = tot++;
if(l==r)
{
c[newroot] = c[root] + val;
return newroot;
}
int mid = (l+r)>>;
if(pos<=mid)
{
lson[newroot] = update(lson[root],l,mid,pos,val);
rson[newroot] = rson[root];
}
else
{
rson[newroot] = update(rson[root],mid+,r,pos,val);
lson[newroot] = lson[root];
}
c[newroot] = c[lson[newroot]] + c[rson[newroot]];
return newroot;
} int query(int left_root, int right_root, int l, int r, int k)
{
if(l==r) return l; int mid = (l+r)>>;
if(c[lson[left_root]]-c[lson[right_root]]>=k)
return query(lson[left_root],lson[right_root],l,mid,k);
else
return query(rson[left_root],rson[right_root],mid+,r,k-(c[lson[left_root]]-c[lson[right_root]]));
} int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
tot = ;
for(int i = ; i<=n; i++)
scanf("%d",&a[i]);
Init_hash();
T[n+] = build(,m);
for(int i = n; i; i--)
{
int pos = hash(a[i]);
T[i] = update(T[i+],,m,pos,);
}
while(q--)
{
int l, r, k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n", t[query(T[l],T[r+],,m,k)]);
}
}
}

POJ2104 K-th Number —— 静态区间第k小的更多相关文章

  1. POJ2104 K-th Number 静态区间第k最值 平方分割

    干掉这道题的那一刻,我只想说:我终于**的AC了!!! 最终内存1344K,耗时10282ms,比起归并树.划分树以及其他各种黑科技,这个成绩并不算光彩⊙﹏⊙ 但至少,从最初的无数次TLE到最终的AC ...

  2. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  3. HDU3473--Minimum Sum(静态区间第k大)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. 数据结构2 静态区间第K大/第K小

    给定数组$A[1...N]$, 区间$[L,R]$中第$K$大/小的数的指将$A[L...R]$中的数从大到小/从小到大排序后的第$K$个. "静态"指的是不带修改. 这个问题有多 ...

  5. Dynamic Rankings || 动态/静态区间第k小(主席树)

    JYF大佬说,一星期要写很多篇博客才会有人看 但是我做题没有那么快啊QwQ Part1 写在前面 区间第K小问题一直是主席树经典题=w=今天的重点是动态区间第K小问题.静态问题要求查询一个区间内的第k ...

  6. 主席树初步学习笔记(可持久化数组?静态区间第k大?)

    我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...

  7. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  8. 主席树学习笔记(静态区间第k大)

    题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出 ...

  9. 主席树(静态区间第k大)

    前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...

随机推荐

  1. 【经验】使用Profiler工具分析内存占用情况

    Unity3D为我们提供了一个强大的性能分析工具Profiler.今天我们就使用Profiler来具体分析一下官方样例AngryBots的内存使用信息数据. 首先打开Profiler选择Memory选 ...

  2. 协程(Coroutine)并不是真正的多线程(转)

    自:http://www.zhihu.com/question/23895384 说到Coroutine,我们必须提到两个更远的东西.在操作系统(os)级别,有进程(process)和线程(threa ...

  3. Objective-C学习笔记(十九)——对象方法和类方法的相互调用

    事实上在OC的对象方法(减号方法)和类方法(加号方法)并非相互独立的,它们也能够发生千丝万缕的关系,今天我们来研究下它们两者相互调用的问题.该样例还是以People类为基础. (一)对象方法调用类方法 ...

  4. iOS 从UITableViewController中分离数据源

    之前看objc.io #1 Light View Controllers看到一个非常不错的技巧:从UITableViewController中分离数据源,这样能够减小UITableViewContro ...

  5. 2个YUV视频拼接技术

    http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...

  6. java List复制:浅拷贝与深拷贝

    Java的拷贝可以分为三种:浅拷贝(Shallow Copy).深拷贝(Deep Copy).延迟拷贝(Lazy Copy). 在java中除了基本数据类型之外(int,long,short等),还存 ...

  7. mac os x升级MOUNTAIN LION后svn command not found的解决

    因为svn是个开发工具 所以苹果把他移到 Xcode developer package 里 去了,所以你没装xcode之类的,先去AppStore把xcode装了   装好之后sudo vi /et ...

  8. PostgreSQL 封装操作数据库方法

    /// <summary> /// 模块名:操作postgres数据库公共类 /// 作用:根据业务需求对数据库进行操作. /// 注:系统中的公共方法,根据需要,逐一引入 /// 作者: ...

  9. 【BZOJ4264】小C找朋友 随机化

    [BZOJ4264]小C找朋友 Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不 ...

  10. ubuntu16.04+cuda8.0+cudnn5.0+caffe

    ubuntu安装过程(硬盘安装)http://www.cnblogs.com/zhbzz2007/p/5493395.html“但是千万不要用麒麟版!!!比原版体验要差很多!!!”开关机的时候电脑最上 ...