poj2104 Kth-Number
Description
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 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
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
题意:给你一个大小为n的序列和m个询问,每次询问求[L,R]区间第k大的值。
正解:主席树或整体二分
以前一直觉得主席树是一个很高深的东西,自己写了以后才知道原来主席树代码极短。。
建n颗值域线段树,第i颗线段树保存1-i对应区间的结点个数,如根结点为1-maxa的个数,左右儿子分别二分得到。
那么,这颗主席树就能满足前缀和的性质,在查询时,只需将第r颗树上的贡献减去第l-1颗树上的贡献就好。查询贡献时,判断当前两结点之差与k的关系。如果当前差<=k,那么就查询左子树,否则查询右子树,查询到叶子结点时就得出答案。。
还有一个问题,如果真的直接开n颗线段树肯定会MLE,那么我们可以利用可持久化技术,每次插入一棵线段树时只加入与之前的树不同的一条链,其他的结点不变,那么我们每次插入的空间复杂度为O(logN),总复杂度为O(NlogN),可以接受。
不多说了,看代码吧。。
//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; int root[],a[],num[],hash[],s[],ls[],rs[],n,m,sz,tot; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar(); if (ch=='-') q=,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-,ch=getchar(); return q ? -x : x;
} il void insert(RG int l,RG int r,RG int x,RG int &y,RG int k){
y=++sz,s[y]=s[x]+; if (l==r) return; ls[y]=ls[x],rs[y]=rs[x]; RG int mid=(l+r)>>;
if (k<=mid) insert(l,mid,ls[x],ls[y],k); else insert(mid+,r,rs[x],rs[y],k); return;
} il int ask(RG int l,RG int r,RG int x,RG int y,RG int k){
while (l<r){
RG int mid=(l+r)>>;
if (s[ls[y]]-s[ls[x]]>=k) r=mid,x=ls[x],y=ls[y];
else l=mid+,k-=s[ls[y]]-s[ls[x]],x=rs[x],y=rs[y];
}
return l;
} il void work(){
n=gi(),m=gi(); for (RG int i=;i<=n;++i) num[i]=a[i]=gi(); sort(num+,num+n+);
hash[++tot]=num[]; for (RG int i=;i<=n;++i) if (num[i]!=num[i-]) hash[++tot]=num[i];
for (RG int i=;i<=n;++i) insert(,tot,root[i-],root[i],lower_bound(hash+,hash+tot+,a[i])-hash);
for (RG int i=;i<=m;++i){ RG int l=gi(),r=gi(),k=gi(); printf("%d\n",hash[ask(,tot,root[l-],root[r],k)]); }
return;
} int main(){
File("kth");
work();
return ;
}
poj2104 Kth-Number的更多相关文章
- POJ2104 K-th Number —— 区间第k小 整体二分
题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS Memory Limit: 65536K Tota ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- POJ2104 K-th Number [整体二分]
题目传送门 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 69053 Accepted: 24 ...
- POJ2104 K-th Number(主席树)
题目 Source http://poj.org/problem?id=2104 Description You are working for Macrohard company in data s ...
- POJ2104 K-th Number[主席树]【学习笔记】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- [POJ2104]K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 34048 Accepted: 10810 Ca ...
- [poj2104] K-th Number (主席树)
主席树 Description You are working for Macrohard company in data structures department. After failing y ...
- 主席树:POJ2104 K-th Number (主席树模板题)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44952 Accepted: 14951 Ca ...
- POJ2104 K-th Number(线段树)
题目链接 K-th Number #include <cstdio> #include <cstring> #include <iostream> #include ...
- POJ2104 K-th Number (子区间内第k大的数字)【划分树算法模板应用】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 40920 Accepted: 13367 Ca ...
随机推荐
- docker工作流程
Docker提供一种方法在容器中运行安全隔离的应用程序,应用程序与所有依赖项和库一起打包在容器中.因为你的应用程序总是可以使用它在构建镜像中期望的环境运行,测试和部署比以往任何时候都更简单,因为你的构 ...
- 一个想法照进现实-《IT连》创业项目:关于团队组建
前言: 从上一篇<三天的风投对接活动内幕分享>归来后,从中领悟了不少内涵. 之后暂停了找钱的想法,这些天也拒绝了不少想要参与众筹的同学. 目前主要精力放在以下三件事: 1:重新规划顶层设计 ...
- 对于自定义标签类中JspBody类的invoke方法的理解
下面是javaeeAPI中对于invoke()方法的介绍: 其中的参数out是一个Writer类的对象,如果写null,就是将标签体内容写到了与此jsp相关联的JspWriter对象,也就是下面的w: ...
- CSS3实现DIV垂直居中+水平居中的四种方法
<div class="div1"> <div class="div2"></div> </div> html结 ...
- 使用Struts2校验器
今天遇到了这样的问题:我的jsp页面.web.xml.struts.xml.UserAction-validation.xml等内容写的都正确,就是无法使用校验器!在网上找了半天就是不出来,迫不得已我 ...
- 英文单词断行问题:CSS中word-break、word-wrap以及hyphens的兼容性和区别
CSS中一提到单词断行,最先映入脑海的肯定是word-break和word-wrap这两条属性.但对于这两条属性到底有什么区别,兼容性如何,我一直都概念模糊.今天抽空把它们以及CSS3中新加入的断行属 ...
- Git 入门和常用命令详解
git 使用使用教程 git 使用简易指南 常用 Git 命令清单 下载 https://git-scm.com/downloads 工作流 本地仓库由三部分组成. 工作区:保存实际的文件( ...
- js原生的轮播图
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- Visual Studio Code 使用Chrome Debug 代码
一.添加插件 Debugger for Chrome,点击安装,安装完成之后,启动 二.配置启动参数 1.按 F5,出现界面如图,选择 Chrome 2.然后会打开配置文件 launch.json 3 ...
- 老李推荐:第6章3节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-命令翻译类
老李推荐:第6章3节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-命令翻译类 每个来自网络的字串命令都需要进行解析执行,只是有些是在解析的过程中直接执行 ...