POJ --2104
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 34935 | Accepted: 11134 | |
Case Time Limit: 2000MS |
Description
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 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
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
划分树
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 100005
using namespace std;
class TreeNode
{
public:
int left;
int right;
int mid;
};
int ToLeft[][MAX];
int val[][MAX];
TreeNode node[*MAX];
int sorted[MAX];
void BuildTree(int k, int d, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].mid = (l + r) >> ;
int mid = (l + r) >> ;
if(l == r)
return ;
int lsame = mid - l + ;
for(int i = l;i <= r; i ++)
{
if(val[d][i] < sorted[mid])
lsame --;
}
int lpos = l;
int rpos = mid + ;
for(int i = l;i <= r;i ++)
{
if(i == l)
ToLeft[d][i] == ;
else
ToLeft[d][i] = ToLeft[d][i-];
if(val[d][i] < sorted[mid])
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
}
else if(val[d][i] > sorted[mid])
val[d+][rpos++] = val[d][i];
else
{
if(lsame)
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
lsame --;
}
else
val[d+][rpos++] = val[d][i];
}
}
BuildTree(k << , d + , l, mid);
BuildTree(k << |, d+, mid + , r);
} int Query(int l, int r, int k, int d, int idx)
{
if(l == r)
return val[d][l];
int s;
int ss;
if(node[idx].left == l)
{
s = ToLeft[d][r];
ss = ;
}
else
{
s = ToLeft[d][r] - ToLeft[d][l-];
ss = ToLeft[d][l-];
}
if(s >= k)
{
int newl = node[idx].left + ss;
int newr = node[idx].left + ss + s - ;
return Query(newl, newr, k, d + , idx << );
}
else
{
int bb = l - node[idx].left - ss;
int b = r- l - s + ;
int newl = node[idx].mid + bb + ;
int newr = node[idx].mid + bb + b;
return Query(newl, newr, k - s, d + , idx << |);
}
} int main(int argc, char const *argv[])
{
int n, m;
int l, r, k;
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
for(int i = ;i <= n;i ++)
{
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted+, sorted+n+);
BuildTree(, , , n);
for(int i = ;i < m;i ++)
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", Query(l, r, k, , ));
}
}
return ;
}
POJ --2104的更多相关文章
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- K-th Number POJ - 2104
K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...
- POJ 2104 K-th Number【整体二分 + 树状数组】
本来只是想学一下CDQ,还是先把整体二分搞懂一点. 这题窝几个月前分别用划分树,树套树,主席树和挑战上介绍的分桶法实现了一发(然而现在都忘得差不多了) 最快的是划分树,其次是主席树,然后是树套树,还有 ...
- K-th Number POJ - 2104 划分树
K-th Number You are working for Macrohard company in data structures department. After failing your ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- K-th Number Poj - 2104 主席树
K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...
- 主席树 【权值线段树】 && 例题K-th Number POJ - 2104
一.主席树与权值线段树区别 主席树是由许多权值线段树构成,单独的权值线段树只能解决寻找整个区间第k大/小值问题(什么叫整个区间,比如你对区间[1,8]建立一颗对应权值线段树,那么你不能询问区间[2,5 ...
- HDU 2665 && POJ 2104(主席树)
http://poj.org/problem?id=2104 对权值进行建树(这个时候树的叶子是数组b的有序数列),然后二分查找原数列中每个数在有序数列中的位置(即第几小),对每一个前缀[1,i]建一 ...
- POJ 2104:K-th Number(整体二分)
http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...
随机推荐
- PHP 数组的值插入
曾今写过一个坑货的数组方法 function array_insert($myarray,$value,$position=0) { $fore=($position==0)?array():a ...
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
在使用Fragment的过程中,常常会遇到在Activity的onSaveInstanceState方法调用之后,操作commit或者popBackStack而导致的crash. 因为在onSaveI ...
- javascript - 图片的幻灯片效果
javascript 代码: <script type="text/javascript"> function select_play() { var select_p ...
- GET请求和POST请求
A:有哪些get请求呢? a.在浏览器地址栏直接输入一个请求地址 b.点击超链接 c.表单默认的提交方式method="GET/get" B:get请求方式的特点 a.会将请求参数 ...
- OC - 13.数据解析(JSON与XML)
##数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需 ...
- 理解java Web项目中的路径问题
本文以项目部署在tomcat服务器为例,其他相信也是一样的. 先说明请求页面的写法,在web中,页面路径主要写的有以下几种 1.请求重定向 2.浏览器的请求被服务器请求到新页面(我称为“转发”) 3. ...
- eclipse和cygwin搭建C++环境的修正版本
最近嫌弃切换系统麻烦.用了cygwin作为netbeans下C++的环境继续学习.我学的很渣,就不卖弄了. 网络上有很多这样的文章.经过对比和实验发现一个深坑.教程部分还是跟其他的一样,重点部分红字做 ...
- VS 2012中消失了的Create UnitTest
前言:最近正在研究一个新项目的开发工作,这个项目的要求是必须写UnitTest,对于我个人来讲是很不喜欢写UnitTest的感觉这个东西会很大程度的延误开发进度,所以之前项目的UnitTest是能不写 ...
- JavaScript学习心得(六)
函数 对函数参数没有任何类型检查(弱类型),在必要时在函数内加上类型检查(typeof): JavaScript的函数参数无法设置默认值(可以通过检查参数,当为undefined时设置一个值 func ...
- 【python之旅】python简介和入门
python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...