题目描述

English Vietnamese 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.

输入输出格式

输入格式:

The first line of the input contains n — the size of the array, and m — the number of questions to answer (1 ≤ n ≤ 100000, 1 ≤ m ≤ 5000).

The second line contains n different integer numbers not exceeding 10^9 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).


SAMPLE INPUT
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

输出格式:


For each question output the answer to it — the k-th number in sorted
a[i ... j] segment. SAMPLE OUTPUT
5
6
3

Note : naive solution will not work!!!

打个版,留给以后复制粘贴2333

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define maxn 100005
using namespace std;
struct node{
node *lc,*rc;
int s;
}nil[maxn*30],*rot[maxn],*cnt;
int a[maxn],num[maxn],n,ky;
int m,le,ri,k;
char ch; node *update(node *u,int l,int r){
node *ret=++cnt;
*ret=*u;
ret->s++; if(l==r) return ret; int mid=l+r>>1;
if(le<=mid) ret->lc=update(ret->lc,l,mid);
else ret->rc=update(ret->rc,mid+1,r); return ret;
} int query(node *u,node *v,int l,int r){
if(l==r) return num[l]; int mid=l+r>>1,c=v->lc->s-u->lc->s;
if(k<=c) return query(u->lc,v->lc,l,mid);
else{
k-=c;
return query(u->rc,v->rc,mid+1,r);
}
} inline void prework(){
cnt=rot[0]=nil->lc=nil->rc=nil;
nil->s=0; for(int i=1;i<=n;i++){
le=a[i];
rot[i]=update(rot[i-1],1,ky);
}
} inline void solve(){
while(m--){
scanf("%d%d%d",&le,&ri,&k);
printf("%d\n",query(rot[le-1],rot[ri],1,ky));
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i),num[i]=a[i];
sort(num+1,num+n+1);
ky=unique(num+1,num+n+1)-num-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(num+1,num+ky+1,a[i])-num; prework();
solve(); return 0;
}

  

Spoj MKTHNUM - K-th Number的更多相关文章

  1. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  2. SPOJ - PHRASES K - Relevant Phrases of Annihilation

    K - Relevant Phrases of Annihilation 题目大意:给你 n 个串,问你最长的在每个字符串中出现两次且不重叠的子串的长度. 思路:二分长度,然后将height分块,看是 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

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

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

  5. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  6. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  7. 大数据热点问题TOP K

    1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...

  8. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

  9. C++经典编程题#1:含k个3的数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...

随机推荐

  1. cakephp中使用 find('count')方法

    对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...

  2. easyui-numberbox限定仅输入数字

    许多必填项都涉及到数字,比如电话号码,身份证号这些要求用户在输入时只能输入数字.Easyui提供了数字框控件,允许用户只输入数字, <td> <input id="ssd& ...

  3. 移动弱网测试方案Network Emulator for Windows Toolkit

    移动app在测试时,有时需要考虑弱网的情形下,app的表现,那么怎么营造这样子的环境呢? 一.首先需要控制网络,有两种方式其一使用网络损伤仪进行,其二采用软件方式.硬件采购费用太贵,因此使用win平台 ...

  4. [oldboy-django][2深入django]django目录说明 + 路由系统

    django project目录说明 project - app01 -- admin.py #django自带后台管理 -- apps.py #app01配置文件 -- models.py #编写类 ...

  5. Python面向对象之私有方法(4)

    类里面有很多成员修饰符,用来修饰各种属性 (1)私有属性,只有内部的方法可以访问 class Foo: xo = 'xo'#表明是公共的,内部外部都可以访问 __ox = '私有属性'#私有属性,只有 ...

  6. nginx在基于域名访问的时候是下载的界面

    刚才在做nginx实验时候出现访问域名的时候是下载页面一直下载了好多文件,使用IP访问就正常,在配置文件中找到一个sendfile的参数,把参数值改为off或者直接注释掉这个参数就可以访问了.

  7. ubuntu 下openoffice安装

    openoffice官网建议的安装步骤:http://www.openoffice.org/download/index.html 前提条件 如果你希望Java集成,你要确保你有安装最新的JRE.它的 ...

  8. Start state is missing. Add at least one state to the flow

    springmvc配置过程中,会配置数据库文件,比如说如下文件:这个时候可能会出现“Start state is missing. Add at least one state to the flow ...

  9. spring+xml集成测试(准备数据和验证项的外部文件化)

    Spring的集成测试 单位测试和集成测试,我想大家都做过,一般情况下,一般逻辑且不需要操作数据库的情况比较适合于单位测试了.而对于一个数据库应用来说,集成测试可能比单元测试更重要,你可以想象,一个互 ...

  10. [NOI2010][bzoj2005] 能量采集 [欧拉函数+分块前缀和优化]

    题面: 传送门 思路: 稍微转化一下,可以发现,每个植物到原点连线上植物的数量,等于gcd(x,y)-1,其中xy是植物的横纵坐标 那么我们实际上就是要求2*sigma(gcd(x,y))-n*m了 ...