算法总结——主席树(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) ...
随机推荐
- PHP 根据两点的经纬度计算距离
/** * @name 根据经纬度确定两点的距离[地理位置] * @author tbj * @param float $lat 纬度值 * @param float $lng 经度值 * @date ...
- 异步 BeginInvoke
委托的异步调用异步多线程的三大特点:1.同步方法卡界面,原因是主线程被占用:异步方法不卡界面,原因是计算交给了别的线程,主线程空闲2.同步方法慢,原因是只有一个线程计算:异步方法快,原因是多个线程同事 ...
- vs和github同步开发步骤
首先,这是在visual studio中使用.需要了解关于vs同步github必不可少.下载安装破解什么的先完成vs. 1. 然后安装一个vs中使用github的插件.vs自带的下载.这个是下载地址. ...
- synchronized 和ReentrantLock的区别
历史知识:JDK5之前,只有synchronized 可以用,之后就有了ReetrantLock可以用了 ReetrantLock (再入锁) 1.位于java.util.concurrnt.lock ...
- struts2基于注解的action
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- Promise中的next 另一个用法
const chainAsync = fns => { let curr = 0 ; const next = (...args) => fns[curr++](next,...args) ...
- BZOJ3301 P2524 UVA11525 算法解释康托展开
这三个题的代码分别对应第二个第一个第三个 在刘汝佳蓝书上我遇到了这个康托展开题. 当时去了解了一下,发现很有意思 百度上的康托展开定义 原理介绍 编辑 康托展开运算 其中, 为整数,并且 . 的意义为 ...
- PAT 乙级 1011
题目 题目地址:PAT 乙级 1011 思路 这道题的比较坑的地方在于给定数据的范围 int 类型的数据大小是[-2^31 , 2^31 -1] 即 [-2147483648,2147483647] ...
- 【Git版本控制】Git使用教程
1.Git的综述 SVN是集中式版本控制系统,版本库集中放在中央服务器上,而干活时用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器.集 ...