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 ...
随机推荐
- Rate Limiter
Whenever you expose a web service / api endpoint, you need to implement a rate limiter to prevent ab ...
- 并发编程 深入分析Volatile的实现原理
在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的“可见性”.可见性的意思是当一个线 ...
- Codeforces 817+818(A~C)
(点击题目即可查看原题) 817A Treasure Hunt 题意:给出起点和终点,每次移动只能从 (a,b)移动至(a+x,b+y) , (a+x,b-y) , (a-x,b+y) , (a-x, ...
- Java switch case 语句
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch(expression){ case value : //语句 break; //可选 ca ...
- 小白学习django第五站-简易案例
首先在setting.py文件中编写数据库配置内容 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': ' ...
- 在Linux中,没有文件创建时间的概念。只有文件的访问时间、修改时间、状态改变时间
在Linux中,没有文件创建时间的概念.只有文件的访问时间.修改时间.状态改变时间.也就是说不能知道文件的创建时间.但如果文件创建后就没有修改过,修改时间=创建时间:如果文件创建后,状态就没有改变过, ...
- VMware Ubuntu18.04 安装及配置笔记
安装Ubuntu 下载虚拟机VMware 下载镜像Ubuntu 过程略, 参考 https://zhuanlan.zhihu.com/p/38797088 Ubuntu配置 特别提示: 在Ubuntu ...
- Thinkphp5.0快速入门笔记(3)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 快速入门第三节 获取当前的请求 ...
- Binding的简单使用
Binding可以看作是数据的桥梁,两端分别为Source和Target,一般情况,Source是逻辑层的对象,Target是UI层的控件对象,可以将数据从逻辑层送往UI层展现 简单的例子: clas ...
- 牛客挑战赛32E 树上逆序对
nowcoder 口胡一时爽 先从这个逆序对的性质入手,手玩可以发现对于一对具有祖先关系节点的点,只有权值绝对值大的才能对这一对点是否为逆序对造成影响.具体来讲,如果祖先点权值大,并且取正号,那么其后 ...