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 <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = 0x7fffffff;
const int SIZE = ;
const int maxnode = + ;
using namespace std;
typedef long long ll;
using namespace std;
struct DATA{
int val, x;//x代表位置
bool operator < (const DATA &b)const{
return val < b.val;
}
}data[MAXN];
struct QUESTION{
int l, r;
int k;
}q[MAXM];
int c[MAXN], id[MAXN], Ans[MAXN];
int tmp[MAXN];
bool mark[MAXN];
int Max = -INF, Min = INF, pos, n, m;
//树状数组
inline int lowbit(int x){return x&-x;}
int sum(int x){
int tmp = ;
while (x > ){
tmp += c[x];
x -= lowbit(x);
}
return tmp;
}
void add(int x, int val){
while (x <= n){
c[x] += val;
x += lowbit(x);
}
return;
} void init(){
memset(mark, , sizeof(mark));
memset(c, , sizeof(c));
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &data[i].val);
data[i].x = i;
Max = max(Max, data[i].val);
Min = min(Min, data[i].val);
}
//排序
sort(data + , data + + n);
}
void solve(int l, int r, int L, int R){
if (l > r || L == R) return;
int mid = (L + R) >> ;
while (data[pos + ].val <= mid && pos < n){
add(data[pos + ].x, );
pos++;
}
while (data[pos].val > mid){
add(data[pos].x, -);
pos--;
}
int cnt = ;//记录找到答案的回答个数
for (int i = l; i <= r; i++){
if (sum(q[id[i]].r) - sum(q[id[i]].l - ) > q[id[i]].k - ){
Ans[id[i]] = mid;
mark[i] = ;
cnt++;
}else mark[i] = ;
}
int l1 = l, l2 = l + cnt;
for (int i = l; i <= r; i++)
if (mark[i]) tmp[l1++] = id[i];
else tmp[l2++] = id[i]; for (int i = l; i <= r; i++) id[i] = tmp[i];
solve(l, l1 - , L, mid);
solve(l1, l2 - , mid + , R);
}
void work(){
pos = ;//初始化即data中的下标
for (int i = ; i <= m; i++){
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
}
for (int i = ; i <= m; i++) id[i] = i;
solve(, m, Min, Max + );
for (int i = ; i <= m; i++) printf("%d\n", Ans[i]);
} int main(){ init();
work();
return ;
}

【POJ2104】【整体二分+树状数组】区间第k大的更多相关文章

  1. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  2. 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  3. 【转载】【树状数组区间第K大/小】

    原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=s ...

  4. Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)

    Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...

  5. 【BZOJ-2527】Meteors 整体二分 + 树状数组

    2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Stat ...

  6. BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组

    BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...

  7. 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

    题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...

  8. 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组

    题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...

  9. HDU 5249 离线树状数组求第k大+离散化

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

随机推荐

  1. php 中 global 与 $GLOBAL 由引用产生的区别

    很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GLOBALS['var'] 是外部的全局变量$var本身. global $var 是外部$var的 ...

  2. ANT的下载和安装

    下载 下载页面:http://ant.apache.org/bindownload.cgi 安装 撰写本文的时候,最新的ANT版本是1.9.7,它是绿色软件,解压缩得到apache-ant-1.9.7 ...

  3. JavaScript之闭包与高阶函数(一)

    JavaScript虽是一门面向对象的编程语言,但同时也有许多函数式编程的特性,如Lambda表达式,闭包,高阶函数等. 函数式编程是种编程范式,它将电脑运算视为函数的计算.函数编程语言最重要的基础是 ...

  4. Gwt 整合FusionCharts及封装搜狗地图时出现的问题

    smartGwt 整合FusionCharts 把需要的.swf文件和FusionCharts.js放在war下面(路径就自己定了) 可以工程的html文件中引FusionCharts.js文件 也可 ...

  5. IIS7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。=

    本文转载:http://www.cnblogs.com/ctcx/archive/2012/07/19/2599741.html 在 Microsoft Visual Studio 2010 打开看了 ...

  6. leetcode-WordLadder

    Word Ladder Total Accepted: 10243 Total Submissions: 58160My Submissions Given two words (start and  ...

  7. [RxJS] Marble diagrams in ASCII form

    There are many operators available, and in order to understand them we need to have a simple way of ...

  8. Readprocessmemory使用方法

    函数功能:该函数从指定的进程中读入内存信息,被读取的区域必须具有訪问权限. 函数原型:BOOL ReadProcessMemory(HANDLE hProcess,LPCVOID lpBaseAddr ...

  9. 常见的浏览器Hack技巧总结(转)

    如果你经常需要做前端页面,那么你一定多多少少需要解决页面的浏览器兼容问题.而浏览器兼容问题大部分也集中在对IE系列的兼容.这里就总结一下对IE系列的CSS Hack,记录一下,方便以后查阅. IE H ...

  10. apache配置php

    第一部分:安装apache 1 .安装apache软件,custom 选全部,安装目录为: F:\Apache2.2\ 2.默认为80端口(如冲突,要学会修改端口) 输入:http://localho ...