链接:

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:LeakFilling(IO流)

    1.IO流中缓冲区过小,会造成读入不全(打印出来的东西会不全)读入的会是最后一个读入的,不会造成写出不全(即写出来的东西).2.缓冲区在读入时的作用就是将某个文件内容的读入到缓冲区,然后通过缓冲区来进 ...

  2. Python unittest 之 BeautifulReport可视化报告

    众所周知的报告是HTMLTestRunner,虽然经过众多的大神修改后,功能挺强大的,但这颜值,我就不多说了,大家自己感受下吧 HTMLTestRunner就不多说了,近来发现了一款款式新颖,还不漏油 ...

  3. python学习之模块-模块(一)

    第五章 5.1 自定义模块 模块概念: ​ 把一些常用的函数放在一个py文件中,这个文件就称之为模块. 模块的意义: ​ 1.方便管理.让程序的解构更加清晰,实现功能的重复使用: ​ 2.提升开发效率 ...

  4. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  5. pip提示ModuleNotFoundError: No module named 'pkg_resources'

    卸载setuptools后,pip下载python包一直提示ModuleNotFoundError: No module named 'pkg_resources',如下图: 在网上找了很多贴了都无法 ...

  6. tensorflow 2.0 技巧 | 自定义tf.keras.Model的坑

    自定义tf.keras.Model需要注意的点 model.save() subclass Model 是不能直接save的,save成.h5,但是能够save_weights,或者save_form ...

  7. Python密码登录程序的思考--学与习

    # 初学者的起步,对于开始的流程图结构还不太熟悉 #   思考: 1,write()与writelines()的区别,前者确定为字符串,后者为序列(列表,字典.元组等),自动为你迭代输入#      ...

  8. MyEclipse 2013 破解

    本文链接:https://blog.csdn.net/Jayliue/article/details/97414181 1.运行cracker.jar 用 cd 命令找到 cracker.jar所在目 ...

  9. 存储过程实例基于postgersql

    数据库用的是postgersql 数据库管理工具是DBeaver  mybatis操作数据库基于jdbcTemplate 1.写的存储方法放在数据库下面的Procedures目录下 function ...

  10. [codeforces#592Div2]C-G题

    题目链接 感觉这场难度迷茫,个人觉得难度排序为$A<B<D=E=G<C<F$ C题: 比赛结束1500+pp,结果出分900+fst,我就是fst的睿智Orz. 题意为给出$n ...