题目:

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.

Source

Northeastern Europe 2004, Northern Subregion

题解:

主席树模板题,关于主席树的知识见下:

代码:

#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)的更多相关文章

  1. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  2. 【CodeForces】960 F. Pathwalks 主席树+动态规划

    [题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...

  3. Super Mario(主席树)

    Super Mario  Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded ...

  4. 主席树模板(poj2104)

    主席树是可持久化线段树,可以记录线段树的历史版本. 代码中和线段树不同的是,l,r记录的是左右子树编号,因为普通的线段树版本中,左右子树自然就是o<<1和o<<1|1,但是主席 ...

  5. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

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

  7. 【POJ2104】K-th Number(主席树)

    题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...

  8. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  9. BZOJ 1878 [SDOI2009]HH的项链 (主席树 或 莫队算法)

    题目链接  HH的项链 这道题可以直接上主席树的模板 #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) ...

随机推荐

  1. iOS:让UIView覆盖导航栏

    当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...

  2. IOS之UIAlert​Controller

    你知道 UIAlertView.UIActionSheet (以及它们各自的 delegate protocols) 在 iOS 8 中已经被废弃了吗? 这是真的.在你的代码中按住 ⌘ 点击 UIAl ...

  3. shell框架

    #!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...

  4. easyui 刷新页面

    window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象 ...

  5. Alpha-beta pruning

    function alphabeta(node, depth, α, β, maximizingPlayer) or node is a terminal node return the heuris ...

  6. 【iOS】UITableview cell 顶部空白的n种设置方法

    我知道没人会主动设置这个东西,但是大家一定都遇到过这个问题,下面总结下可能是哪些情况: 1, self.automaticallyAdjustsScrollViewInsets = NO;  这个应该 ...

  7. 安装linux虚拟机(Ubuntu & KALI)

    VMware workstation 15.0.0 ubuntu-18.10-desktop 首先安装VMware 参考资料很多,不再赘述. 之后参考 https://www.cnblogs.com/ ...

  8. ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

    部署docker下的mysql时出现以下报错 [root@docker ~]# mysql -h192.168.30.22 -uroot -p Enter password: 出现报错: ERROR ...

  9. snprintf()返回值的陷阱

    int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...

  10. bootstrap历练实例:标签式的导航菜单

    本章将讲解bootstrap提供的用于定义导航元素的一些选项,它使用相同的标签和基类.nav.Bootsrtap也提供了一个用于共享标记和状态的帮助器类.改变修饰的class,可以在不同的样式间进行切 ...