POJ 2104 K-th Number 静态主席树(裸
题目链接:点击打开链接
题意:
给定n长的序列。q个询问
以下n个数字给出序列
每一个询问[l, r] k ,输出该区间中第k大的数
先建一个n个节点的空树。然后每次从后往前新建一棵树,依附原来的空树建。询问就是在树上二分。
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std; const int N = 100010;
const int M = N * 30;
int n, q, tot;
int a[N];
int T[M], lson[M], rson[M], c[M];
vector<int>G; void input(){
G.clear();
for (int i = 1; i <= n; i++)scanf("%d", &a[i]), G.push_back(a[i]);
sort(G.begin(), G.end());
G.erase(unique(G.begin(), G.end()), G.end());
for (int i = 1; i <= n; i++)a[i] = lower_bound(G.begin(), G.end(), a[i]) - G.begin() + 1;
}
int build(int l, int r){
int root = tot++;
c[root] = 0;
if (l != r){
int mid = (l + r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid + 1, r);
}
return root;
}
int updata(int root, int pos, int val){
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = G.size();
while (l <= r){
int mid = (l + r) >> 1;
if (pos <= mid){
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid - 1;
}
else {
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int L, int R, int k){
int l = 1, r = G.size(), ans = l;
while (l <= r){
int mid = (l + r) >> 1;
if (c[lson[L]] - c[lson[R]] >= k){
ans = mid;
r = mid - 1;
L = lson[L];
R = lson[R];
}
else {
l = mid + 1;
k -= c[lson[L]] - c[lson[R]];
L = rson[L];
R = rson[R];
}
}
return ans;
}
int main(){
while (~scanf("%d%d", &n, &q)){
input();
tot = 0;
T[n + 1] = build(1, G.size());
for (int i = n; i; i--)
T[i] = updata(T[i + 1], a[i], 1);
while (q--){
int l, r, k; scanf("%d %d %d", &l, &r, &k);
printf("%d\n", G[query(T[l], T[r+1], k)- 1]);
}
}
return 0;
}
POJ 2104 K-th Number 静态主席树(裸的更多相关文章
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- COGS 930. [河南省队2012] 找第k小的数 主席树
主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- POJ 2104 K-th Number(主席树模板题)
http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...
- POJ 2104 K-th Number(主席树——附讲解)
Description You are working for Macrohard company in data structures department. After failing your ...
- poj 2104 K-th Number(主席树)
Description You are working for Macrohard company in data structures department. After failing your ...
- poj 2104 静态主席树
我的第一道主席树(静态). 先记下自己对主席树的理解: 主席树的作用是用于查询区间第k大的元素(初始化nlog(n),查询log(n)) 主席树=可持续线段树+前缀和思想 主席树实际上是n棵线段树(由 ...
- poj 2104 K-th Number(主席树
Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 59058 Accepted: 20529 Case Time Limi ...
随机推荐
- django admin显示多对多字段
参考文档https://jingyan.baidu.com/article/4e5b3e190f55c591901e24b3.html admin.py from .models import *cl ...
- Swift学习笔记(3):基本运算符
目录: 运算符 元组比较 空和运算符 区间运算符 运算符 +, -, *, /, %, =, +=, -=, *=, /= 算术运算符 >, <, ==, >=, <=, != ...
- 相机拍照友盟检测crash是为什么?
友盟报错如下* setObjectForKey: object cannot be nil (key: UIImagePickerControllerOriginalImage)(null)(( 0 ...
- 关于md解析器
不得不说,博客园的 md 解析器真的不够好.和 csdn 以及 sf 社区的比起来,差太多了.以后,博客园就老老实实写随笔了,文章类的还是用 csdn 吧.
- PostgreSQL Replication之第九章 与pgpool一起工作(5)
9.5 检查复制 如果所有的节点都处于开机并运行的状态.我们就可以在集群上运行我们的第一个操作了.在我们的例子中,我们将简单地连接到pgpool并创建一个新的数据库.createdb 是一个命令行工具 ...
- Open With Atom添加到右键菜单/从右键菜单移除
1.进入Settings 快捷键ctrl+shift+p,输入settings后回车 2.切换到System选项卡 3.通过勾选/取消勾选以下选项实现添加/移除右键菜单 √ Show in file ...
- MySql系列之单表查询
单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 关键字的执行 ...
- [USACO16FEB]围栏Fenced In Platinum
题目:洛谷P3141. 题目大意:有一个方形区域,被分成若干区域.现在要去掉若干条围栏,使得所有区域连通,求最少去掉多少长度的围栏. 解题思路:贪心.建议画图思考. 先对围栏位置进行排序,然后相邻两条 ...
- [ZJOI2012]旅游(树的直径)
[ZJOI2012]旅游 题目描述 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示 ...
- MySql中允许远程连接
要达到这个目的需要实现两点 开通用户权限 解除本地绑定 开通用户权限 首先登陆服务器端的mysql //不使用空格可以直接登陆 mysql -u用户名 -p密码 mysql> use mysql ...