链接:

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. 网易云课堂_C++程序设计入门(下)_第11单元:工欲善其事必先利其器 - STL简介_第11单元 - 单元作业2:OJ编程 - list 与 deque

    第11单元 - 单元作业2:OJ编程 - list 与 deque 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之 ...

  2. Stream流实现斐波那契数列

    1.前言 我们都知道斐波那契数列有很多种实现方法,在jdk1.8以前没有流操作,只能通过递归或者迭代等其他方式来实现斐波那契数列, 但是jdk1.8以后,有了流操作,我们就可以使用流来实现斐波那契数列 ...

  3. (转)GIS中的4D产品(DLG、DRG、DEM、DOM)

    DLG 数字线划地图(DLG, Digital Line Graphic):是与现有线划基本一致的各地图要素的矢量 数据集,且保存各要素间的空间关系和相关的属性信息. 在世字测图中,最为常见的产品就是 ...

  4. 【神经网络与深度学习】深度学习实战——caffe windows 下训练自己的网络模型

    1.相关准备 1.1 手写数字数据集 这篇博客上有.jpg格式的图片下载,附带标签信息,有需要的自行下载,博客附带百度云盘下载地址(手写数字.jpg 格式):http://blog.csdn.net/ ...

  5. Solr 4.4.0利用dataimporthandler导入本地pdf、word等文档

    1. 创建本地目录 $ mkdir /usr/local/contentplatform/solr/solr/core1/file1 $ ls -lh total 88M -rw-r--r-- tnu ...

  6. MySQL -2- 体系结构-多实例模块

    0.在已创建mysql,port=3306的数据库前提下 需要删除/etc/.my.cnf再做以下操作 cp /etc/my.cnf /etc/my.cnf.3306 1.mkdir -p /u02/ ...

  7. React进阶篇学习

    继续上一次基础篇, 分享一些关于React的进阶技术 React 进阶部分 ** context ** ** setState vs forceUpdate ** ** Mixins ** ** HO ...

  8. [洛谷P4183][USACO18JAN]Cow at Large P

    题目链接 Bzoj崩了之后在洛谷偶然找到的点分好题! 在暴力的角度来说,如果我们$O(n)$枚举根节点,有没有办法在$O(n)$的时间内找到答案呢? 此时如果用树形$dp$的想法,发现是可做的,因为可 ...

  9. Codeforces 1156F Card Bag(概率DP)

    设dp[i][j]表示选到了第i张牌,牌号在j之前包括j的概率,cnt[i]表示有i张牌,inv[i]表示i在mod下的逆元,那我们可以考虑转移,dp[i][j]=dp[i-1][j-1]*cnt[j ...

  10. WTF!! Vue数组splice方法无法正常工作

    当函数执行到this.agents.splice()时,我设置了断点.发现传参index是0,但是页面上的列表项对应的第一行数据没有被删除, WTF!!! 这是什么鬼!然后我打开Vue Devtool ...