poj 2761 主席树的应用(查询区间第k小值)
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 22084 | Accepted: 7033 |
Description
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.
Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.
You can assume that n<100001 and m<50001.
Output
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
参考源代码:
#include <iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
//主席树
const int MAXN = 100005;
struct node {
int ls, rs, sum;
} ns[MAXN * 20];
int ct;
int rt[MAXN * 20];
void cpy(int& now, int old) {
now = ++ct;
ns[now] = ns[old];
}
void pushUp(int& now) {
ns[now].sum = ns[ns[now].ls].sum + ns[ns[now].rs].sum;
}
void build(int& now, int l, int r) {
now = ++ct;
ns[now].sum = 0;
if (l == r) return;
int m = (l + r) >> 1;
build(ns[now].ls, l, m);
build(ns[now].rs, m + 1, r);
}
void update(int& now, int old, int l, int r, int x) {
cpy(now, old);
if (l == r) {
ns[now].sum++;
return;
}
int m = (l + r) >> 1;
if (x <= m) update(ns[now].ls, ns[old].ls, l, m, x);
else update(ns[now].rs, ns[old].rs, m + 1, r, x);
pushUp(now);
}
int query(int s, int t, int l, int r, int k) {
if (l == r) return l;
int m = (l + r) >> 1;
int cnt = ns[ns[t].ls].sum - ns[ns[s].ls].sum;
//cout << s << " " << t << " " << cnt << endl;
if (k <= cnt) return query(ns[s].ls, ns[t].ls, l, m, k);
return query(ns[s].rs, ns[t].rs, m + 1, r, k - cnt);
}
void init(int n) {
ct = 0;
build(rt[0], 1, n);
}
int value[100005];
int b[100005];
using namespace std;
int main()
{
freopen("in.txt", "r", stdin);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &value[i]);
b[i] = value[i];
}
sort(b + 1, b + n + 1);
int sz = unique(b + 1, b + 1 + n) - b - 1;
init(sz);
for (int i = 1; i <= n; i++) {
value[i] = lower_bound(b + 1, b + 1 + sz, value[i]) - b;
update(rt[i], rt[i - 1], 1, sz, value[i]);
}
while (m--) {
int s, t, k;
scanf("%d%d%d", &s, &t, &k);
printf("%d\n", b[query(rt[s - 1], rt[t], 1, sz, k)]);
}
return 0;
}
poj 2761 主席树的应用(查询区间第k小值)的更多相关文章
- 【可持久化线段树】POJ2104 查询区间第k小值
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 61284 Accepted: 21504 Ca ...
- 主席树总结(经典区间第k小问题)(主席树,线段树)
接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...
- 主席树初步 HDU2665的区间第k小
首先看一下这个人的blog吧,讲的精炼http://blog.sina.com.cn/s/blog_4a0c4e5d0101c8fr.html 然后再推荐一下这个人的blog:http://www.c ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- 洛谷3834 hdu2665主席树模板,动态查询区间第k小
题目链接:https://www.luogu.com.cn/problem/P3834 对于区间查询第k小的问题,在区间数量达到5e5的时候是难以用朴素数据结构实现的,这时候主席树就应运而生了,主席树 ...
- 「BZOJ3065」带插入区间第K小值 替罪羊树×线段树
题目描述 从前有\(n\)只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力\(a_i\).跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间\(k\)小值.他 ...
- BZOJ 3065 带插入区间第K小值
题目描述 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间k小值. ...
- ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大
Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树 ...
- 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216
poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
随机推荐
- 从零开始,SpreadJS新人学习笔记【第3周】
表单&函数 阔别多日, SpreadJS新人学习笔记,本周起正式回归!(在断更的这一个月中,我为大家先后录制了14期SpreadJS产品入门系列学习视频,希望帮助那些正在学习和使用 Sprea ...
- 一、程序安全-SQL注入漏洞
先新建MYDB.MDF,表MyUser: 测试页面: 一.利用报错获取信息 操作:按姓名精确查询,在输入框输入:小卫' and 1=db_name()/0 and '1'='1执行语句:select ...
- 基于CentOS系统部署EPICS环境
1.虚拟机安装CentOS系统2.打开终端,以root账户登录3.进入/usr/local目录下,新建文件夹epics,并进入该文件夹4.在/usr/local/epics目录下,执行wget htt ...
- RSA 加密长度计算公式
The length of data that can be encrypted using RSA is determined primarily by the size of the key yo ...
- 03 Redis发布与订阅
以qq群的公告,单个发布者,多个收听者为例 发布/订阅 实验 发布订阅的命令 PUBLISH channel msg 将信息 message 发送到指定的频道 channel SUBSCRIBE ch ...
- MySQL之数据库优化
Mysql数据库的优化技术 对mysql优化是一个综合性的技术,主要包括 •表的设计合理化(符合3NF) •添加适当索引(index) [四种: 普通索引.主键索引.唯一索引unique.全文索引] ...
- Zookeeper包中,slf4j-log4j12和log4j冲突问题解决
程序启动时会有日志警告 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/E ...
- table 边框问题
对table设置css样式边框,分为几种情况:1.只对table设置边框2.对td设置边框3.对table和td技巧性设置表格边框4.对table和td设置背景,实现完美表格边框 以下DIVCSS5对 ...
- Python代码风格的良好养成
Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读, ...
- oracle 之创建视图异常
最近在整理的oracle 的时候发现.创建视图 例如: CREATE OR REPLACE VIEW dept_sum_vw(name,minsal,maxsal,avgsal) AS SELECT ...