主席树就是对每个历史版本都建了一颗线段树,这样我们在统计一些问题的时候,对于一个区间[L,R]的询问,就可以利用前缀和的思想找到第L-1和第R颗历史版本的线段树来处理查找。由于这样空间需求就增大了,注意到如果每个版本之间只是多更新了一个点的话,那么这两颗树就只有一条链不相同,我们不妨在前一颗树的基础上建立第二颗树,用链表做链接,这样就达到了节省空间的目的。

Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15653    Accepted Submission(s): 4724

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases. 
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere. 
The second line contains n integers, describe the sequence. 
Each of following m lines contains three integers s, t, k. 
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source
 
  主席树的模板题,求静态的区间第k小值。
  

#include<bits/stdc++.h>
using namespace std;
#define mid ((L+R)>>1)
const int maxn=;
int root[maxn],a[maxn],tot;
struct node{int lc,rc,sum;}T[maxn*];
vector<int>v;
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+;}
void update(int &x,int y,int L,int R,int d){
T[++tot]=T[y],T[tot].sum++,x=tot;
if(L==R) return;
if(d<=mid){
update(T[x].lc,T[y].lc,L,mid,d);
}
else{
update(T[x].rc,T[y].rc,mid+,R,d);
}
}
int ask(int x,int y,int L,int R,int d){
if(L==R) return L;
int s=T[T[x].lc].sum-T[T[y].lc].sum;
if(s>=d) return ask(T[x].lc,T[y].lc,L,mid,d);
else return ask(T[x].rc,T[y].rc,mid+,R,d-s);
}
int main(){
int t,n,m,i,j,k,l,r;
cin>>t;
while(t--){
v.clear();
tot=;
cin>>n>>m;
for(i=;i<=n;++i) scanf("%d",a+i),v.push_back(a[i]);
sort(v.begin(),v.end()),v.erase(unique(v.begin(),v.end()),v.end());
for(i=;i<=n;++i) update(root[i],root[i-],,n,getid(a[i]));
for(i=;i<=m;++i){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",v[ask(root[r],root[l-],,n,k)-]);
}
}
return ;
}
 

主席树学习笔记-hdu-2665的更多相关文章

  1. 主席树学习笔记(静态区间第k大)

    题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出 ...

  2. zkw线段树学习笔记

    zkw线段树学习笔记 今天模拟赛线段树被卡常了,由于我自带常数 \(buff\),所以学了下zkw线段树. 平常的线段树无论是修改还是查询,都是从根开始递归找到区间的,而zkw线段树直接从叶子结点开始 ...

  3. 仙人掌&圆方树学习笔记

    仙人掌&圆方树学习笔记 1.仙人掌 圆方树用来干啥? --处理仙人掌的问题. 仙人掌是啥? (图片来自于\(BZOJ1023\)) --也就是任意一条边只会出现在一个环里面. 当然,如果你的图 ...

  4. 线段树学习笔记(基础&进阶)(一) | P3372 【模板】线段树 1 题解

    什么是线段树 线段树是一棵二叉树,每个结点存储需维护的信息,一般用于处理区间最值.区间和等问题. 线段树的用处 对编号连续的一些点进行修改或者统计操作,修改和统计的复杂度都是 O(log n). 基础 ...

  5. 数据结构(主席树):HDU 4729 An Easy Problem for Elfness

    An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (J ...

  6. Treap-平衡树学习笔记

    平衡树-Treap学习笔记 最近刚学了Treap 发现这种数据结构真的是--妙啊妙啊~~ 咳咳.... 所以发一发博客,也是为了加深蒟蒻自己的理解 顺便帮助一下各位小伙伴们 切入正题 Treap的结构 ...

  7. JSOI2008 Blue Mary开公司 | 李超线段树学习笔记

    题目链接:戳我 这相当于是一个李超线段树的模板qwqwq,题解就不多说了. 代码如下: #include<iostream> #include<cstdio> #include ...

  8. LuoguP2617 Dynamic Rankings (动态主席树学习理解)

    题目地址 题目链接 题解 动态主席树的板子题.动态主席树其实和静态的有很大差别,虽然同样是n个根,但是节点并不能共用,每个根节点表示bit上的一段区间. 所以其实是个树套树的东西来着,外层是bit,内 ...

  9. Splay伸展树学习笔记

    Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...

随机推荐

  1. QEvent postEvent/sendEvent

    可以自訂事件類型,最簡單的方式,是透過QEvent::Type指定事件類型的常數值,在建構QCustomEvent時作為建構引數並透過postEvent()傳送事件,例如: const QEvent: ...

  2. IP分片丢失重传

    尽管IP分片看起来是是透明的,但有一点让人不想使用它:即使只丢失一片数据也要重传整个数据报.为什么会发生这种情况呢?     因为IP层本身没有超时重传的机制——由更高层来负责超时和重传(TCP有超时 ...

  3. MySQL数据库----流程控制

    流程控制 1.条件语句 举例一 delimiter // CREATE PROCEDURE proc_if () BEGIN declare i int default 0; if i = 1 THE ...

  4. 《网络对抗》拓展:注入shellcode

    实践三 知识要求: shellcode:指溢出后执行的能开启系统shell的代码.但是在缓冲区溢出攻击时,也可以将整个触发缓冲区溢出攻击过程的代码统称为shellcode,因此可以将shellcode ...

  5. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. Excel编程的基本概念

    http://wenku.baidu.com/link?url=b3RZzH4KILFWbysnenCvXwiFFkyZqkxk8bvOMy1T7xW54MeGL1WHivGvyqxgI3yFXvY6 ...

  7. 网络压缩论文集(network compression)

    Convolutional Neural Networks ImageNet Models Architecture Design Activation Functions Visualization ...

  8. The way to Go(6): Go程序的基本结构和要素

    Reference: Github: Go Github: The way to Go Go程序的基本结构和要素 helloworld.go: package main import "fm ...

  9. HDU 6103 Kirinriki(尺取法)

    http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...

  10. Linux下通过命令行mail发送e-mail

    找到配置文件/etc/mail.rc添加如下行 # vi /etc/mail.rc set from=@qq.com set smtp=smtp.qq.com set smtp-auth-user= ...