KPI

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 616    Accepted Submission(s): 261

Problem Description
你工作以后, KPI 就是你的所有了. 我开发了一个服务。取得了非常大的知名度。数十亿的请求被推到一个大管道后同一时候服务从管头拉取请求。让我们来定义每一个请求都有一个重要值。我的KPI是由当前管道内请求的重要值的中间值来计算。如今给你服务记录,有时我想知道当前管道内请求的重要值得中间值。
 
Input
有大约100组数据。

每组数据第一行有一个n(1≤n≤10000),代表服务记录数。



接下来有n行。每一行有3种形式

  "in x": 代表重要值为x(0≤x≤109)的请求被推进管道。

"out": 代表服务拉取了管道头部的请求。

  "query: 代表我想知道当前管道内请求重要值的中间值. 那就是说,假设当前管道内有m条请求, 我想知道。升序排序后第floor(m/2)+1th
条请求的重要值.



为了让题目简单,全部的x都不同。而且假设管道内没有值。就不会有"out"和"query"操作。

 
Output
对于每组数据。先输出一行



Case #i:

然后每一次"query"。输出当前管道内重要值的中间值。
 
Sample Input
6
in 874
query
out
in 24622
in 12194
query
 
Sample Output
Case #1:
874
24622
 
K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 41138   Accepted: 13447
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.

这两题都 要求子区间的第k大数,假设用快排之后,再查,复杂度太大。不能过,就要用到划分树了,划分树。事实上,就是线段树的

一个变种了,当然,这样做查询是lg(n)级别,建树是n * lg(n),再加上一次快排,也是n * lg(n),差点儿相同就能够过了!

先来看看划分树,我们在线段树的基础上,假设每一个结点都保存了。当前子段向左子树走的个数。假设,查询的k要小于,查询段向左走的个数,自然,我们向左子树就能够查到结果。假设,k>向左走的个数,当然要向右找k-midcount个数,区间的变换。我们能够

推一下向左走就是l + scount, l + ecount - 1。向右走就是mid + 1 + s - l - scount, mid + 1 + e - l - ecount,当中scount就是s之前向左走的个数。 ecount,就是e之前向左走的个数。

上核心代码

#define MID(a,b) (((a)+(b))>>1)
#define N 100050
int num[20][N], val[20][N];
//第i层,向左包含自已,向左子树的个数,当前的值
int pri[N], sorted[N];
//原始和排序后的数列
char str[20];
void build(int l, int r, int layer){
if (l >= r){
return;
}
int mid = MID(l, r);
int ql = l, qr = mid + 1, leftCount = mid, eqCount = 0;
for (int i = l; i <= r; i++)
leftCount -= val[layer][i] < sorted[mid];
for (int i = l; i <= r; i++){
if (i == l){
num[layer][i] = 0;
}
else{
num[layer][i] = num[layer][i - 1];
}
if (val[layer][i] < sorted[mid]){
num[layer][i]++;
val[layer + 1][ql++] = val[layer][i];
}
else if (val[layer][i] > sorted[mid]){
val[layer + 1][qr++] = val[layer][i];
}
else {
if (eqCount < leftCount){
eqCount++;
num[layer][i]++;
val[layer + 1][ql++] = val[layer][i];
}
else {
val[layer + 1][qr++] = val[layer][i];
}
}
}
build(l, mid, layer + 1);
build(mid + 1, r, layer + 1);
}
//在layer层l到r间,找s-e之间的第k大数
int query(int l, int r, int layer, int s, int e, int k){
if (l >= r || s >= e){
return val[layer][s];
}
int scount, ecount, mid = MID(l, r);
if (s == l){
scount = 0, ecount = num[layer][e];
}
else {
scount = num[layer][s - 1], ecount = num[layer][e];
}
int midcount = ecount - scount;
if (k <= midcount){
return query(l, mid, layer + 1, l + scount, l + ecount - 1, k);
}
else {
return query(mid + 1, r, layer + 1, mid + 1 + s - l - scount, mid + 1 + e - l - ecount, k - midcount);
}
}
int main()
{
int tcase, tcasenum = 0;
int n, k, q, s, e, begin = 1, nn, qcount, m;
while (S2(n, m) != EOF)
{
FI(n){
S(val[0][i+1]);
}
FI(n){
sorted[i + 1] = val[0][i + 1];
}
sort(sorted + 1, sorted + n + 1);
build(1, n, 0);
FI(m){
S2(s, e); S(q);
Prn(query(1, n, 0, s,e,q));
}
}
return 0;
}

划分树 poj2104 hdu5249的更多相关文章

  1. poj2104(划分树模板)

    poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...

  2. 划分树(poj2104)

    poj2104 题意:给出n个数,有m次查询,每次查询要你找出 l 到 r 中第 k 大的数: 思路:划分树模板题 上述图片展现了查询时如何往下递推的过程 其中ly表示 [sl,l) 中有多少个数进入 ...

  3. poj2104(划分树模板)

    poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...

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

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

  5. 初学划分树,小见解之!POJ-2104/HDU-2665

    划分树 本来是学主席树的,可怜我等巨弱观群巨博客难解fotle主席的思想精髓.于是学了一下划分树,嗯,花了一下午时间理解build(其实自己模拟一遍就通了),我很难理解为什么划分树会看不懂而能学会主席 ...

  6. hdu2665 && poj2104划分树

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

  7. poj2104 线段树 划分树

    学习:http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html 划分树的build: 划分树是分层构建的,在构建的t层时,我们可以 ...

  8. POJ2104 k-th number 划分树

    又是不带修改的区间第k大,这次用的是一个不同的方法,划分树,划分树感觉上是模拟了快速排序的过程,依照pivot不断地往下划分,然后每一层多存一个toleft[i]数组,就可以知道在这一层里从0到i里有 ...

  9. poj2104 划分树 区间K大 在线 无修改

    博主sbit....对于高级数据结构深感无力,然后这些东西在OI竟然烂大街了,不搞就整个人都不好了呢. 于是我勇猛的跳进了这个大坑 ——sbit 区间K大的裸题,在线,无修改. 可以用归并树(\(O( ...

随机推荐

  1. CentOS6.6(单用户模式)重设root密码

    1.开机时手要快按任意键,因为默认时间5s 2.grub菜单,只有一个内核,没什么好上下选的,按e键.不过如果你升级了系统或安装了Xen虚拟化后,就会有多个显示了. 3.接下来显示如下,选择第二项,按 ...

  2. Solr 安装与集成IK中文分词器

    创建wangchuanfu core 1.  在example目录下创建wangchuanfu-solr文件夹: 2.  将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...

  3. TravelCMS旅游网站系统诞生记

    本人就是一纯粹码农,没什么学历,更没什么技术,但是自认为学习能力还不错,近期有一个旅游网站系统项目正在进行中,在此以贴图记录这个项目的诞生过程,本是一个定制系统,但是不想把系统做死,以通用产品的标准来 ...

  4. ado.net数据库操作(1)

    这些都是网上搜索到的,我把他放在自己的博客里,作为笔记 1.1创建数据库连接(sqlserver) <%@ Import Namespace="System.Data" %& ...

  5. .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法—获取数据库表结构方法和跟参数赋值方法

    1./// <summary> /// 获取当前目标表结构 /// </summary> /// <param name="tableName"> ...

  6. PowerDesigner使用常见问题

    1.在数据库生成表的时候,要求PowerDesigner中设计的表的Name的值要放到数据库中表的描述中,而不是PowerDesigner 中字段的Comment: 具体方法如下:首先将PowerDe ...

  7. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  8. Hadoop文件的基本操作

    Hadoop提供了大量的API对文件系统中的文件进行操作,主要包括: (1)读取文件 (2)写文件 (3)读取文件属性 (4)列出文件 (5)删除文件 1、读取文件 以下示例中,将hdfs中的一个文件 ...

  9. js中几个容易混淆的概念

    1. var name = "The Window";var object = {name : "My Object",getName: function(){ ...

  10. shell中的for、while、until

    for var in list do commands done 在每个迭代中,变量var会包含列表中的当前值,第一个迭代会使用列表中的第一个值,第二个迭代使用第二个值. 在do和done中,$var ...