链接:

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. Redis 入门 3.1 热身

    3.1 热身 1. 获得符合规则的键名列表 KEYS pattern pattern 支持 glob 风格通配符格式 语言 字符组 ? 匹配一个字符 * 匹配任意个(包括0个)字符 [] 匹配括号间的 ...

  2. 容易忽略的javascript知识点的总结

    /** 对代码行进行折行 **/您可以在文本字符串中使用反斜杠对代码行进行换行.下面的例子会正确地显示:document.write("Hello \World!"); 不过,您不 ...

  3. Day03:运算符和表达式 / 分支结构

    Java 运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 字符串运算符 ...

  4. Cocos2d-X多线程(1) 在cocos2d-x中使用多线程

    教科书上说:进程是资源分配的最小单位,线程是CPU调度的最小单位. 进程是程序在计算机上的一次执行活动.直观的讲就是会产生一个pid. int main() {     //业务逻辑代码     re ...

  5. 基于高斯分布的异常检测(Anomaly Detection)算法

    记得在做电商运营初期,每每为我们频道的促销活动锁取得的“超高”销售额感动,但后来随着工作的深入,我越来越觉得这里面水很深.商家运营.品类运营不断的通过刷单来获取其所需,或是商品搜索排名,或是某种kpi ...

  6. 【Qt开发】QString与数字类型的转换(不同进制)

    把QString转换为 double类型 方法1.QString str="123.45"; double val=str.toDouble(); //val=123.45 方法2 ...

  7. Python示例-TCP Port Scan

    import socket import threading # target host address host = "127.0.0.1" # thread list thre ...

  8. python 并发编程 多线程 Thread对象的其他属性或方法

    介绍 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. threading模块提供的一些方法: ...

  9. Pikachu漏洞练习平台实验——XSS(二)

    概述 简介 XSS是一种发生在Web前端的漏洞,所以其危害的对象也主要是前端用户 XSS漏洞可以用来进行钓鱼攻击.前端js挖矿.盗取用户cookie,甚至对主机进行远程控制 攻击流程 假设存在漏洞的是 ...

  10. Dubbo基础、高级讲解

    基础 https://blog.csdn.net/hardworking0323/article/category/6148466 高级 https://blog.csdn.net/hardworki ...