K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 44952   Accepted: 14951
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大,没有修改操作,于是作主席树模板打了~~~
  引用主席树介绍:
 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node{
int a,b,rs,ls,sum;
}tr[];
int a[],b[];
int rt[],pos,cnt;
void Build(int &node,int a,int b)
{
node=++cnt;
tr[node].a=a;
tr[node].b=b;
if(a==b)return;
int mid=(a+b)>>;
Build(tr[node].ls,a,mid);
Build(tr[node].rs,mid+,b);
} void Insert(int pre,int &node)
{
node=++cnt;
tr[node].ls=tr[pre].ls;
tr[node].rs=tr[pre].rs;
tr[node].a=tr[pre].a;
tr[node].b=tr[pre].b;
tr[node].sum=tr[pre].sum+;
if(tr[node].a==tr[node].b)return;
int mid=(tr[node].a+tr[node].b)>>;
if(mid>=pos)Insert(tr[pre].ls,tr[node].ls);
else Insert(tr[pre].rs,tr[node].rs);
}
int Query(int pre,int node,int k)
{
if(tr[node].ls==tr[node].rs)return b[tr[node].a];
int cmp=tr[tr[node].ls].sum-tr[tr[pre].ls].sum;
if(cmp>=k)return Query(tr[pre].ls,tr[node].ls,k);
else return Query(tr[pre].rs,tr[node].rs,k-cmp);
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;b[i]=a[i],i++)
scanf("%d",&a[i]);
sort(b+,b+n+);
Build(rt[],,n);
for(int i=;i<=n;i++)
{
pos=lower_bound(b+,b+n+,a[i])-b;
Insert(rt[i-],rt[i]);
}
int l,r,k;
for(int i=;i<=q;i++)
{
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",Query(rt[l-],rt[r],k));
}
return ;
}

主席树:POJ2104 K-th Number (主席树模板题)的更多相关文章

  1. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...

  2. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  3. CODEFORCES 340 XOR and Favorite Number 莫队模板题

    原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...

  4. [POJ2104] K – th Number (可持久化线段树 主席树)

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

  5. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  6. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  7. POJ2104 K-th Number[主席树]【学习笔记】

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

  8. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  9. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  10. Online Judge 2014 K-th Number -主席树

    You are working for Macrohard company in data structures department. After failing your previous tas ...

随机推荐

  1. java读取redis的timeout异常

    http://blog.csdn.net/shuaiokshuai/article/details/23266091 FIFO Fist-in Fisrt-out 先进先出

  2. [转] EF Configuring a DbContext

    本文转自:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext Note This document ...

  3. HTML5 WebAudioAPI简介(一)

    一.常用对象 1.AudioContext对象 AudioContext是一个专门用于音频处理的接口,并且原理是讲AudioContext创建出来的各种节点(AudioNode)相互连接,音频数据流经 ...

  4. LaTeX 中插入数学公式

    一.常用的数学符号 1.小写希腊字母 \alpha \nu \beta \xi \gamma o \delta \pi \epsilon \rho \zeta \sigma \eta \tau \th ...

  5. HTTP could not register URL http://+:8000/.... Your process does not have access rights to this namespace

    windows 7, Visual Studio 2013 在托管 Windows 服务中承载 WCF 服务时报错: HTTP could not register URL http://+:8000 ...

  6. oracle-同义词,又学到东西了

    select  col1  from tab1@db_link1; create or replace synonym test123 as se,ect col1 from tab1@db_link ...

  7. java 按天创建文件夹

    按天创建文件夹,也就是每天创建一个,适合上传文件服务使用,文件数量较多时可以按文件夹区分. public static final String FMT = "yyyy-MM-dd" ...

  8. php之购物车类思路及代码

    <?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...

  9. Wdcp缺少mod_rewite模块

    1.下载apache源码包,解压并查找到mod_rewrite.c文件 tar -zxvf httpd-2.2.27.tar.gz cd httpd-2.2.27 [root@localhost ht ...

  10. Json串到json对象的转换

    JSON(JavaScript Object Notation) JS对象符号 是一种轻量级的数据交换格式 JavaScript eval()函数实现 (一) 标准格式 function JsonFo ...