链接:

https://vjudge.net/problem/POJ-2104#author=malic

题意:

给定一个数组 a[1...n],数组元素各不相同,你的程序要对每次查询Q(i,j,k)作出回答,其中Q(i,j,k)的含义为在数组a[i...j]中第k大的数字.

例如,给出数组a=(1, 5, 2, 6, 3, 7, 4).查询语句为Q(2, 5, 3),即从(5,2,6,3)中找出第3大的元素,将之排序得到(2, 3, 5, 6),故第三大的数字是5,所以这次查询的结果应当为5.

思路:

主席树模板。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
struct Node
{
int v;
int pos;
bool operator < (const Node& that)const
{
return this->v < that.v;
}
}node[MAXN];
int Sca[MAXN], Ori[MAXN]; struct Segment
{
int l, r;
int cnt;
}Seg[MAXN*30];
int Tree_root[MAXN*30];
int cnt = 0, tree_cnt = 0;
int n, m; int Insert(int num, int last, int l, int r)
{
++tree_cnt;//树节点数加1
Seg[tree_cnt].cnt = Seg[last].cnt+1;//数加1
int nownode = tree_cnt;//记录当前节点数用于返回
int mid = (l+r)/2;
if (l == r)
{
return nownode;
}
else if (num <= mid)
{
Seg[nownode].l = Insert(num, Seg[last].l, l, mid);
Seg[nownode].r = Seg[last].r;
}
else
{
Seg[nownode].l = Seg[last].l;
Seg[nownode].r = Insert(num, Seg[last].r, mid+1, r);
}
return nownode;
} int Query(int x, int y, int l, int r, int k)
{
// cout << Seg[x].cnt << ' ' << Seg[y].cnt << ' ' << l << ' ' << r << endl;
if (l == r)
return l;
int sum = (Seg[Seg[y].l].cnt - Seg[Seg[x].l].cnt);
int mid = (l+r)/2;
if (k <= sum)
return Query(Seg[x].l, Seg[y].l, l, mid, k);
else
return Query(Seg[x].r, Seg[y].r, mid+1, r, k-sum);
} void scatter()
{
//离散化
cnt = 0;
int pos = 0;
sort(node+1, node+1+n);
Ori[++pos] = node[1].v;
Sca[node[1].pos] = ++cnt;
for (int i = 2;i <= n;i++)
{
if (node[i].v == node[i-1].v)
{
Sca[node[i].pos] = cnt;
}
else
{
Sca[node[i].pos] = ++cnt;
Ori[++pos] = node[i].v;
}
}
} int main()
{
scanf("%d %d", &n, &m);
for (int i = 1;i <= n;i++)
scanf("%d", &node[i].v), node[i].pos = i;
scatter();
Tree_root[0] = 0;
Seg[0].cnt = 0;
for (int i = 1;i <= n;i++)
{
int pos = Insert(Sca[i], Tree_root[i-1], 1, n);
Tree_root[i] = pos;
}
while (m--)
{
int x, y, k;
scanf("%d %d %d", &x, &y, &k);
int pos = Query(Tree_root[x-1], Tree_root[y], 1, n, k);
printf("%d\n", Ori[pos]);
} return 0;
}
/*
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
*/

POJ-2104-Kth Number(主席树)的更多相关文章

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

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

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

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

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

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

  4. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  5. Poj 2104 K-th Number(主席树&&整体二分)

    K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...

  6. POJ 2104 K-th Number 主席树

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

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

  8. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  9. POJ 2104 K-th Number(划分树)

    题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...

  10. hdu 2665 Kth number (poj 2104 K-th Number) 划分树

    划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...

随机推荐

  1. Java学习之==>注解

    一.概述 关于注解,首先引入官方文档的一句话:Java 注解用于为 Java 代码提供元数据.作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的.接下我将从注解的定义. ...

  2. django 通过模型类操作数据表(基础增删改查)

    进入项目shell的命令: python manage.py shell 以下为在相互shell终端中演示的例子: 首先导入模型类:(from 应用名.models import 类名) from b ...

  3. docker下载镜像加速

    1.如何获取阿里云加速地址 参考文档 https://yq.aliyun.com/articles/29941 关于加速器的地址,你只需要登录容器Hub服务的控制台,左侧的加速器帮助页面就会显示为你独 ...

  4. oracle 11g错误ora-01033:oracle initialization or shutdown in progress解决办法

    原文出自:http://blog.csdn.net/liverliu/article/details/6410287 一.首先:问题的产生原因,先前我在f:/llh/目录创建的一个bookspace表 ...

  5. linux中权限对文件和目录的影响?

    #########  rwx 权限对文件和目录的含义  ############ 代表字符          权限                  对文件的含义               对 目录 ...

  6. 平衡树(fhq无旋treap)

    fhq板子(代码正确且风格易懂) 洛谷P3369 #include<iostream> #include<cstring> #include<cstdio> #in ...

  7. win10+vs2013+pcl1.8.0(x86) 环境配置遇到的各种小问题解决

    1.PCL提供了各自的PDB调试文件(解压后放入pcl安装目录的bin下) 2.OpenNI的安装需同其余在pcl第三方库文件夹下 3.添加附加依赖项的.lib文件请按照网上对应版本添加,另外需要每行 ...

  8. Python 集合的各种操作 数学运算 关系操作 增删改查 生成式

    # 集合是无序的 是可变的 不能重复 允许数学运算 分散存储 # 创建# collegel = {'哲学','经济学','法学','教育学'}## # {'教育学', '经济学', '法学', '哲学 ...

  9. vue.js之过渡动画

    vue提供了一个封装动画的组件 <transition name="tr"></transition>,将需要执行动画的元素包裹在该组件中,在通过css修改 ...

  10. RabbitMQ入门教程(十二):消息确认Ack

    原文:RabbitMQ入门教程(十二):消息确认Ack 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csd ...