【poj2104】K-th Number 主席树

时间:2022-04-25 03:47:18

题目描述

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 主席树&plus;超级详细解释

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

  3. &lbrack;POJ2104&rsqb; K – th Number &lpar;可持久化线段树 主席树&rpar;

    题目背景 这是个非常经典的主席树入门题--静态区间第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&lbrack;主席树&rsqb;【学习笔记】

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

  7. &lbrack;poj2104&rsqb; K-th Number &lpar;主席树&rpar;

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

  8. 主席树:POJ2104 K-th Number &lpar;主席树模板题&rpar;

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

  9. POJ 2104 K-th Number 主席树&lpar;区间第k大&rpar;

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

随机推荐

  1. redis&plus;cookies实现session机制(解决 手机浏览器不自动回传cookies导致session不可用问题)

    昨天在手机端测试自己的项目遇到如下情况. 1.在手机上(苹果qq浏览器),登陆时存在session中的图片验证码结果,一直获取不到,考虑是cookies的问题.但是其他网站有貌似可以正常使用cooki ...

  2. 在Excel里用vba给合并的单元格添加注释

    Excel里使用VBA对已经合并的单元格添加注释,直接使用AddComment会报: 运行时错误 '1004':应用程序定义或者对象定义错误 找了很多文章都没找到怎么解决,最后发现在AddCommen ...

  3. IO-04&period; 混合类型数据格式化输入&lpar;5&rpar;

    本题要求编写程序,顺序读入浮点数1.整数.字符.浮点数2,再按照字符.整数.浮点数1.浮点数2的顺序输出. 输入格式: 输入在一行中顺序给出浮点数1.整数.字符.浮点数2,其间以1个空格分隔. 输出格 ...

  4. 关于LED 流水灯的软件调试方法&lpar;非开发板调试&rpar;

    因为: 硬件 norflash 有寿命,所以尽量少用,而且自己也不会把 程序在 KEIL中从SDRAM 中调试,不会设置.所以采取软件虚拟的方法调试. 主要修改一下几部分: 1.  ledcircle ...

  5. HTML转义字符集合

    readme:这次可以不readme了,因为这个是我copy过来的~ ISO Latin-1字符集:  — 制表符Horizontal tab  — 换行Line feed  — 回车Carriage ...

  6. 程序集、应用程序配置及App&period;config和YourSoft&period;exe&period;config &period;

    转自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html 什么是程序集 程序集标识属性 强名称的程序集 强名称工作原理 配置文 ...

  7. CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)

    C - Network Saboteur Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  8. pandas 获取数据帧DataFrame的行、列数

    1.创建数据帧 import pandas as pd df = pd.DataFrame([[1, 'A', '3%' ], [2, 'B']], index=['row_0', 'row_1'], ...

  9. Django之ORM初始

    上一篇写了一个静态的登录验证. 实景情况网页的登录验证都是动态验证的,过程其实是从后端拿到储存的账户与密码来和前端的输入信息进行匹配校验的. 一.把项目拆分,来一个单独登录的包,放在Django项目下 ...

  10. one by one 项目 part 6

    package Controllerservlet; import java.io.IOException; import java.io.PrintWriter; import java.util. ...