算法总结——主席树(poj2104)
题目:
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
Source
题解:
主席树模板题,关于主席树的知识见下:
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e5+;
struct node
{
int l,r,sum;
}tree[N*];
inline int R()
{
char c;int f=,i=;
for(c=getchar();(c<''||c>'')&&c!='-';c=getchar());
if(c=='-') i=-,c=getchar();
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f*i;
}
int num[N],a[N],n,m,root[N],tot;
inline void build(int v,int &k,int l,int r)
{
tree[++tot]=tree[k],k=tot;tree[k].sum+=;
if(l==r) return;
int mid=(l+r)/;
if(v<=mid) build(v,tree[k].l,l,mid);
else build(v,tree[k].r,mid+,r);
}
inline int query(int t1,int t2,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)/,tot=tree[tree[t2].l].sum-tree[tree[t1].l].sum;
if(k<=tot) return query(tree[t1].l,tree[t2].l,l,mid,k);
else return query(tree[t1].r,tree[t2].r,mid+,r,k-tot);
}
int main()
{
//freopen("a.in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
int p,q,k;tot=;
for(int i=;i<=n;i++)
num[i]=R(),a[i]=num[i];
sort(a+,a+n+);
for(int i=;i<=n;i++)
num[i]=lower_bound(a+,a+n+,num[i])-a;
for(int i=;i<=n;i++)
{
root[i]=root[i-];build(num[i],root[i],,n);
}
for(int i=;i<=m;i++)
{
p=R(),q=R(),k=R();
cout<<a[query(root[p-],root[q],,n,k)]<<endl;
}
}
return ;
}
算法总结——主席树(poj2104)的更多相关文章
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- 【CodeForces】960 F. Pathwalks 主席树+动态规划
[题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...
- Super Mario(主席树)
Super Mario Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded ...
- 主席树模板(poj2104)
主席树是可持久化线段树,可以记录线段树的历史版本. 代码中和线段树不同的是,l,r记录的是左右子树编号,因为普通的线段树版本中,左右子树自然就是o<<1和o<<1|1,但是主席 ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- 【POJ2104】K-th Number(主席树)
题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- BZOJ 1878 [SDOI2009]HH的项链 (主席树 或 莫队算法)
题目链接 HH的项链 这道题可以直接上主席树的模板 #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) ...
随机推荐
- VS2010中C++ 出现fatal error LNK1169: 找到一个或多个多重定义的符号
一般是函数重定义造成的 例如定义了两个 sum(x,y)函数
- 洛谷 P1449 后缀表达式
题目描述 所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级). 如:3*(5–2)+7对应 ...
- C# 重写(override)和覆盖(new)
重写 用关键字 virtual 修饰的方法,叫虚方法.可以在子类中用override 声明同名的方法,这叫“重写”.相应的没有用virtual修饰的方法,我们叫它实方法.重写会改变父类方法的功能. ...
- JS中的delete操作符
首先,delete删除成功返回true,失败返回false. js代码: function wxCount ($element) { this.init($element); } wxCount.pr ...
- OpenWrite方法打开现有文件并进行写入
实现效果: 知识运用: File类的OpenWrite方法 //实现打开现有文件以进行写入 public static FileStream OpenWrite (string path) Encod ...
- JS计算两个日期时间差,天 小时 分 秒格式
function diffTime(startDate,endDate) { startDate= new Date(startDate); endDate = new Date(endDate); ...
- 系统学习爬虫_2_urllib
什么是urllib urlopen urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cad ...
- Mac OS 终端强化美化:iterm2 + zsh + oh~my~zsh 设置教程
为了获得更好的排版效果,文章改用markdown撰写,故重发一次. 前言 mac自带的terminal终端没有文件名高亮等功能,而且界面不是很好看,故今晚学舍友折腾了终端,可以让自己使用起来更加方便, ...
- Linux-利用keepalived实现lvs的高可用性
单主模型IPVS示例 配置keepalive 高可用的ipvs集群示例:修改keepalived配置文件 修改主机:192.168.234.27的keepalived配置文件 [root@234c27 ...
- css实现盒尺寸重置、均匀分布的子元素、截断文本
盒尺寸重置 重置盒子模型,以便width s和height s并没有受到border 还是padding他们的影响 . CSS文字折断 css实现盒尺寸重置.均匀分布的子元素.截断文本 如何对多行文本 ...