题目描述

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 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).

输出

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

样例输入

7 3

1 5 2 6 3 7 4

2 5 3

4 4 1

1 7 3

样例输出

5

6

3


题目大意

给你n个数和m组询问,对于每组询问输出区间[i,j]中第k大的数是多少。

题解

主席树模板题。

先将数据离散化,然后加入到n棵动态开点线段树中。

由于每次只修改一个值,所以其余的子节点可以直接继承上一个子节点。

查询时,直接作差即可。

#include <cstdio>
#include <algorithm>
using namespace std;
struct data
{
int num , pos;
}a[100010];
int lp[4000010] , rp[4000010] , sum[4000010] , root[100010] , val[100010] , tot , cnt;
bool cmp1(data a , data b)
{
return a.num < b.num;
}
bool cmp2(data a , data b)
{
return a.pos < b.pos;
}
void pushup(int x)
{
sum[x] = sum[lp[x]] + sum[rp[x]];
}
void ins(int x , int &y , int l , int r , int p)
{
y = ++tot;
if(l == r)
{
sum[y] = sum[x] + 1;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) rp[y] = rp[x] , ins(lp[x] , lp[y] , l , mid , p);
else lp[y] = lp[x] , ins(rp[x] , rp[y] , mid + 1 , r , p);
pushup(y);
}
int query(int x , int y , int l , int r , int p)
{
if(l == r) return val[l];
int mid = (l + r) >> 1;
if(sum[lp[y]] - sum[lp[x]] >= p) return query(lp[x] , lp[y] , l , mid , p);
else return query(rp[x] , rp[y] , mid + 1 , r , p - sum[lp[y]] + sum[lp[x]]);
}
int main()
{
int n , m , i , x , y , z;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &a[i].num) , a[i].pos = i;
sort(a + 1 , a + n + 1 , cmp1);
val[0] = 0x80000000;
for(i = 1 ; i <= n ; i ++ )
{
if(a[i].num > val[cnt]) val[++cnt] = a[i].num;
a[i].num = cnt;
}
sort(a + 1 , a + n + 1 , cmp2);
for(i = 1 ; i <= n ; i ++ )
ins(root[i - 1] , root[i] , 1 , cnt , a[i].num);
for(i = 1 ; i <= m ; i ++ )
{
scanf("%d%d%d" , &x , &y , &z);
printf("%d\n" , query(root[x - 1] , root[y] , 1 , cnt , z));
}
}

【poj2104】K-th Number 主席树的更多相关文章

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

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

  2. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

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

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

  4. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  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[主席树]【学习笔记】

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

  7. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  8. 主席树:POJ2104 K-th Number (主席树模板题)

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

  9. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

随机推荐

  1. 在XAML中为ItemsControl定义分组,适合mvvm绑定

    可以先参考一下这个文章: http://www.cnblogs.com/zoexia/archive/2014/11/30/4134012.html step0: 先展示一下最简陋的界面: 上图是一个 ...

  2. 北京Uber优步司机奖励政策(2月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. MFC 中的设计模式分析

    MFC 中的设计模式分析 最近在学习设计模式,突然想到MFC里面其实也包含有设计模式的原理,于是分析了一下,做一个笔记,网上也找了一些资料,在此一并感谢. 创建型模式 单例模式(Singleton P ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. Net Core学习笔记

    Net Core 官网:https://dotnet.github.io/ Net Core Api: https://docs.microsoft.com/en-us/dotnet/api/?vie ...

  6. PS 拉伸大长腿

    1.打开一个图片工具栏--图像--画布大小 2.选择矩形选框工具--框住要拉升退的位置--然后在按Ctrl+T,进行拉伸即可

  7. Katalon 学习笔记(一)

      工具介绍: Katalon Studio是一个能提供一整套功能来实现Web,API和Mobile的全自动测试解决方案的自动化测试平台.Katalon Studio构建于开源Selenium和App ...

  8. mvc中actionresult的返回值类型

    以前一直没注意actionresult都能返回哪些类型的类型值(一直用的公司的内部工具类初始化进行返回的),今天跟大家分享一下(也是转载的别人的日志qaq). 首先我们了解一下对action的要求: ...

  9. 如何在Python 2.X中也达到类似nonlocal关键字的效果

    nonlocal关键字时Python 3.X中引入的,目的是让内层函数可以修改外层函数的变量值,而该关键字在Python 2.X中是不存在的.那么,要在Python 2.X中达到类型达到类似nonlo ...

  10. StrBlob类——智能指针作为成员

    /* 管理string的类 使用vector来管理元素 由于类对象被销毁时相应的元素成员也将销毁 所以需要将vector保存在动态内存中 */ //该程序鲁棒性不强,没有考虑到vector为空的情况 ...