【可持久化线段树】POJ2104 查询区间第k小值
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 61284 | Accepted: 21504 | |
| Case Time Limit: 2000MS | ||
Description
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
题解
可持久化线段树,就是动态的去开点
如果一段区间并没有被修改,那么我们就可以直接让指针指向这一段区间
如果被修改,那么我们再新建点去存它修改过后的区间
这样一来显然每次最多新建树的深度个点,也就是logn个点
然后说说基本操作
单点修改:
显然我们要修改某个点的值而不影响历史版本的线段树
那么我们就在从根节点向下找目标节点时,把路径上的点都复制一份,再在回溯时修改
最多增加logn个点
区间修改:
显然我们要修改某个区间的值而不影响历史版本的线段树
那么我们就往下走,记得pushdown和新开点
找到区间后就打标记
区间查询:
从root[now]出发,向下走,记得pushdown(pushdown的时候也要新开节点,不要忘了)
代码
//by 减维
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<map>
#include<bitset>
#include<algorithm>
#define ll long long
#define ls l,mid
#define rs mid+1,r
using namespace std; struct number{
int val,pos;
}a[]; struct tree{
int l,r,sum;
}t[]; int n,m,cnt,ys[],root[]; bool cmp(const number&x,const number&y){return x.val<y.val;} void build(int num,int &x,int l,int r)
{
t[++cnt]=t[x];x=cnt;
++t[x].sum;
if(l==r)return ;
int mid=(l+r)>>;
if(num<=mid)build(num,t[x].l,ls);
else build(num,t[x].r,rs);
} int ask(int x,int y,int k,int l,int r)
{
if(l==r)return l;
int tt=t[t[y].l].sum-t[t[x].l].sum;
int mid=(l+r)>>;
if(k<=tt)return ask(t[x].l,t[y].l,k,ls);
else return ask(t[x].r,t[y].r,k-tt,rs);//记得一定要减去tt
} int main()
{
scanf("%d%d",&n,&m);
t[]=(tree){,,};
for(int i=;i<=n;++i)
{
scanf("%d",&a[i].val);
a[i].pos=i;
}
sort(a+,a+n+,cmp);
for(int i=;i<=n;++i)ys[a[i].pos]=i;
for(int i=;i<=n;++i)
{
root[i]=root[i-];
build(ys[i],root[i],,n);
}
for(int i=,x,y,z;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&z);
printf("%d\n",a[ask(root[x-],root[y],z,,n)].val);
}
return ;
}
【可持久化线段树】POJ2104 查询区间第k小值的更多相关文章
- HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 主席树(可持久化线段树)静态区间第K小
传送门主席树 #include <bits/stdc++.h> #define int long long using namespace std; const int maxn=2e5+ ...
- hdu2665可持久化线段树,求区间第K大
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj 2761 主席树的应用(查询区间第k小值)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 22084 Accepted: 7033 De ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- CF452F等差子序列 & 线段树+hash查询区间是否为回文串
记录一下一个新学的线段树基础trick(真就小学生trick呗) 给你一个1到n的排列,你需要判断该排列内部是否存在一个3个元素的子序列(可以不连续),使得这个子序列是等差序列.\(n\) <= ...
- 洛谷3834 hdu2665主席树模板,动态查询区间第k小
题目链接:https://www.luogu.com.cn/problem/P3834 对于区间查询第k小的问题,在区间数量达到5e5的时候是难以用朴素数据结构实现的,这时候主席树就应运而生了,主席树 ...
- 「BZOJ3065」带插入区间第K小值 替罪羊树×线段树
题目描述 从前有\(n\)只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力\(a_i\).跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间\(k\)小值.他 ...
随机推荐
- oracle实现主键自增
首先,创建一张表: CREATE TABLE example( ID Number(4) NOT NULL PRIMARY KEY, NAME VARCHAR(25)); 然后,自定义一个序列(seq ...
- ASP.NET Core 使用 Hangfire 定时任务
定时任务组件,除了 Hangfire 外,还有一个 Quarz.NET,不过 Hangfire .NET Core 支持的会更好些. ASP.NET Core 使用 Hangfire 很简单,首先,N ...
- 自学Zabbix3.5.7-监控项item-Applications
Applications应用程序用于对逻辑组中的项目进行分组.例如我们要监控MySQL,我们可以将所有和MySQL相关的item放到这个应用程序中.例如MySQL的availability of My ...
- Netty学习笔记(一):接收nodejs模拟表单上传的文件
好久不写博客了,也好久不写代码了,这两天临时遇上一个事情,觉得不难,加上觉得手有些生,就动手做了一下,结果遇上了不少坑,有新坑,有老坑,痛苦无比,现在总算差不多了,赶紧记录下来,希望以后不再重复这种痛 ...
- SQL Server-聚焦ROW_NUMBER VS TOP N性能
前言 抱歉各位,从八月份开始一直在着手写EntityFramework 6.x和EntityFramework Core 2.0的书籍写作,所以最近一直遗漏了对博客的管理,后面会着手于写SQL Ser ...
- iOS 科学计数法保留N位有效数字
iOS开发 项目中用到了将一个很大的数值转换成科学计数法的需求,转换成科学计数法的方式在iOS中其实是很好做的,使用NSNumber 的 kCFNumberFormatterScientificSty ...
- 记一次生产环境Nginx日志骤增的问题排查过程
摘要:众所周知,Nginx是目前最流行的Web Server之一,也广泛应用于负载均衡.反向代理等服务,但使用过程中可能因为对Nginx工作原理.变量含义理解错误,或是参数配置不当导致Nginx工作异 ...
- 完善chrome翻译插件ChaZD,支持有道智云api
首先放上该项目的github地址:https://github.com/codethereforam/ChaZD 之前想找一个chrome支持划词翻译的插件,最终在知乎上看到了这个回答,推荐的是Cha ...
- HTML知识点记录
1.input的type设置为file时,设置multiple属性可以同时选择多个文件.
- ES6 函数的扩展2
8.2 rest参数 ES6引入rest参数(形式为"-变量名"),用于获取函数的多余参数,这样就不需要使用arguments对象了. arguments对象并没有数组的方法,re ...