题目链接:https://vjudge.net/problem/POJ-2104

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 64110   Accepted: 22556
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

题解:

查询区间第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 = 2e5+; struct node
{
int x, y, k, type, id;
};
node q[MAXN]; int n, m, c[MAXN];
int lowbit(int x) {return x&(-x);}
void add(int x, int val) {for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;}
int sum(int x) {int ret=; for(int i=x;i>;i-=lowbit(i))ret+=c[i]; return ret;} int ans[MAXN];
node q1[MAXN], q2[MAXN]; //两个桶
void solve(int l, int r, int ql, int qr) //二分答案
{
if(ql>qr) return; //!!
if(l==r) //当l==r时,即答案已明确
{
for(int i = ql; i<=qr; i++)
if(q[i].type==) ans[q[i].id] = l;
return;
} int mid = (l+r)>>; //写成 (l+r)/2会runtime error,不知为何
int t1 = , t2 = ;
for(int i = ql; i<=qr; i++) //枚举操作
{
if(q[i].type==)
{
if(q[i].y<=mid)
{
add(q[i].x, );
q1[++t1] = q[i];
}else q2[++t2] = q[i];
}
else
{
int pre = sum(q[i].y)-sum(q[i].x-);
if(pre>=q[i].k) q1[++t1] = q[i];
else
{
q[i].k -= pre;
q2[++t2] = q[i];
}
}
}
for(int i = ; i<=t1; i++) //撤回对线段树的操作
if(q1[i].type==) add(q1[i].x, -); for(int i = ; i<=t1; i++) q[ql+i-] = q1[i];
for(int i = ; i<=t2; i++) q[ql+t1+i-] = q2[i];
solve(l, mid, ql, ql+t1-);
solve(mid+, r, ql+t1, qr);
} int main()
{
while(scanf("%d%d",&n,&m)==)
{
int tot = ;
for(int i = ; i<=n; i++)
{
++tot;
scanf("%d", &q[tot].y);
q[tot].x = i; q[tot].type = ;
}
for(int i = ; i<=m; i++)
{
++tot;
scanf("%d%d%d", &q[tot].x,&q[tot].y,&q[tot].k);
q[tot].id = i; q[tot].type = ;
} memset(c, , sizeof(c));
solve(-MOD,MOD, ,tot);
for(int i = ; i<=m; i++)
printf("%d\n", ans[i]);
}
}

最新文章

  1. css基础
  2. git commit之后未submit,rebase之后找不到自己代码的处理方法
  3. Node.js入门:Hello World
  4. Router的创建者——RouteBuilder
  5. 2.1.12 Next Permutation 下一个字典序数组
  6. sqlserver日常维护脚本
  7. hdu 3661 Assignments(水题的解法)
  8. POJ 1860 Currency Exchange
  9. uva 12097 - Pie
  10. CSS 与 HTML5 响应式图片
  11. 代码检查工具jshint和csslint
  12. JavaScript中的call()、apply()与bind():
  13. TF30042: The database is full. Contact your Team Foundation Server administrator.
  14. Axis2 webservice入门--Webservice的发布与调用
  15. Webpack系列-第三篇流程杂记
  16. What&#39;s the meaning of unqualified-id?
  17. 吴恩达deeplearning之CNN—卷积神经网络
  18. Angular2 File Upload
  19. [每天解决一问题系列 - 0009] File System Redirector
  20. testng多线程

热门文章

  1. 2016.8.19 在dialog上增加一个button出现错误:failed to execute setAttribute on Element...
  2. C# Graphics
  3. Spring IOC源代码具体解释之整体结构
  4. 安装centos出错
  5. Node.js 把抓取到的电影节目列表单发或者群发到QQ邮箱
  6. springMVC --@RequestParam注解(后台控制器获取參数)
  7. grunt前端打包——css篇
  8. HDFS源码分析数据块复制监控线程ReplicationMonitor(一)
  9. Spark源码分析之一:Job提交运行总流程概述
  10. JS——特效秀