K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 61284   Accepted: 21504
Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
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 first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
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

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

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

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

题解

可持久化线段树,就是动态的去开点

如果一段区间并没有被修改,那么我们就可以直接让指针指向这一段区间

如果被修改,那么我们再新建点去存它修改过后的区间

这样一来显然每次最多新建树的深度个点,也就是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小值的更多相关文章

  1. 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 ...

  2. 主席树(可持久化线段树)静态区间第K小

    传送门主席树 #include <bits/stdc++.h> #define int long long using namespace std; const int maxn=2e5+ ...

  3. hdu2665可持久化线段树,求区间第K大

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. poj 2761 主席树的应用(查询区间第k小值)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 22084   Accepted: 7033 De ...

  5. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  6. HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. CF452F等差子序列 & 线段树+hash查询区间是否为回文串

    记录一下一个新学的线段树基础trick(真就小学生trick呗) 给你一个1到n的排列,你需要判断该排列内部是否存在一个3个元素的子序列(可以不连续),使得这个子序列是等差序列.\(n\) <= ...

  8. 洛谷3834 hdu2665主席树模板,动态查询区间第k小

    题目链接:https://www.luogu.com.cn/problem/P3834 对于区间查询第k小的问题,在区间数量达到5e5的时候是难以用朴素数据结构实现的,这时候主席树就应运而生了,主席树 ...

  9. 「BZOJ3065」带插入区间第K小值 替罪羊树×线段树

    题目描述 从前有\(n\)只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力\(a_i\).跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间\(k\)小值.他 ...

随机推荐

  1. 【最短路】 ZOJ 1544 Currency Exchange 推断负圈

    给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱  汇率为W1 B->A 所需的手 ...

  2. JavaScript必知的特性(继承)

    多数人在学习JavaScript的时候.都是做Web的时候.须要表单验证.或者是一些简单的DOM操作,如同我上篇所讲.处在一个"辅助"的地位. 处在"辅助"地位 ...

  3. 导出Excel1 - 项目分解篇

    我们在全部的MIS系统(信息管理系统)中都能见到他.所以我们把这个通用功能提出来. 项目名称:车辆信息管理系统(中石化石炼) 项目负责人:xiaobin 项目时间:2006.12 - 2007.2 E ...

  4. telematics product and company in China

    持续更新中. 总的来看后装OBD市场日渐繁荣,可是应用深度不够:前装infotainment受限于产量和商业模式,举步维艰. 车联网作为汽车信息化的起点,会有泡沫,也会在大数据和物联网的浪潮中逐步积淀 ...

  5. 快速序列化组件MessagePack介绍

    简介 MessagePack for C#(MessagePack-CSharp)是用于C#的极速MessagePack序列化程序,比MsgPack-Cli快10倍,与其他所有C#序列化程序相比,具有 ...

  6. Python3.x和Python2.x的区别【转】

    转载自:https://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html 1.性能 Py3.0运行 pystone benchm ...

  7. Struts2 2.5.12的问题

    使用maven搭建的Struts项目,使用Struts 2.5.12会启动报错,应该不是包冲突的问题,感觉是lang3.jar的问题,换回2.5.10.1就没事了. 这点挺奇怪的,不应该是jar包损坏 ...

  8. J2EE: JCA (Java Connector Architecture) [转]

    JCA (J2EE 连接器架构,Java Connector Architecture)是对J2EE标准集的重要补充.因为它注重的是将Java程序连接到非Java程序和软件包中间件的开发.连接器特指基 ...

  9. 理解spread运算符与rest参数

    理解spread运算符与rest参数 spread运算符与rest参数 是ES6的新语法.它们的作用是什么?能做什么事情? 1. rest运算符用于获取函数调用时传入的参数. function tes ...

  10. www.netcraft.com查看站点服务器使用的是什么操作系统

    查看站点服务器使用的是什么操作系统