题目链接

思路

裸的主席树。查询的时候,通过相减求出区间内左子树中数的个数a。然后判断要查找的k是否比这个z要大。如果比这个值大,那么就去右子树中查找第k - z大,否则去左子树中查找第k大。

代码

/*
* @Author: wxyww
* @Date: 2018-12-11 16:27:19
* @Last Modified time: 2018-12-11 16:46:07
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<map>
#include<bitset>
using namespace std;
typedef long long ll;
const int N = 200000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int a[N],root[N];
map<int,int>ma;
int tree[N * 30],ls[N * 30],rs[N * 30];
int tot,dy[N];
void update(int &rt,int lst,int l,int r,int pos) {
rt = ++tot;
ls[rt] = ls[lst];rs[rt] = rs[lst];
tree[rt] = tree[lst] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(pos <= mid) update(ls[rt],ls[lst],l,mid,pos);
else update(rs[rt],rs[lst],mid + 1,r,pos);
}
int query(int L,int R,int l,int r,int k) {
int z = tree[ls[R]] - tree[ls[L]];
if(l == r) return l;
int mid = (l + r) >> 1;
if(k <= z) return query(ls[L],ls[R],l,mid,k);
else return query(rs[L],rs[R],mid + 1,r,k - z);
}
int main() {
int n = read(),m = read();
for(int i = 1;i <= n;++i) ls[i] = a[i] = read(); sort(ls + 1,ls + n + 1);
int js = 0;
ma[ls[1]] = ++js;
dy[js] = ls[1];
for(int i = 2;i <= n;++i) if(ls[i] != ls[i - 1]) ma[ls[i]] = ++js,dy[js] = ls[i];
for(int i = 1;i <= n;++i) a[i] = ma[a[i]]; for(int i = 1;i <= n;++i) update(root[i],root[i - 1],1,js,a[i]); while(m--) {
int l = read(),r = read(),k = read();
printf("%d\n",dy[query(root[l - 1],root[r],1,js,k)]);
}
return 0;
}

[luogu3834][可持久化线段树 1(主席树)]的更多相关文章

  1. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  2. P3919 【模板】可持久化数组 -初步探究主席树

    本篇blog主要是给自己(大家)看的. 感谢longlongzhu123奆佬(此人初二LCT)的指点,使本蒟蒻可以快速开始主席树入门. what is 主席树? $        $主席树这个名字只不 ...

  3. POJ 2104 K-th Number(分桶,线段树,主席树)

    一道比较经典的数据结构题.可以用多种方式来做. 一,分桶法(平方分解). 根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k, 且kth是满足条 ...

  4. 【题解】BZOJ3489 A Hard RMQ problem(主席树套主席树)

    [题解]A simple RMQ problem 占坑,免得咕咕咕了,争取在2h内写出代码 upd:由于博主太菜而且硬是要用指针写两个主席树,所以延后2hQAQ upd:由于博主太菜而且太懒所以他决定 ...

  5. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  6. 【BZOJ4771】七彩树(主席树)

    [BZOJ4771]七彩树(主席树) 题面 BZOJ 题解 如果没有深度限制,每次只询问子树内的颜色个数,除了树套树\(dfs\)序加前驱或者后继强行二维数点之外,还有这样一种做法: 把所有相同颜色的 ...

  7. 洛谷P3248 树 [HNOI2016] 主席树+倍增+分治

    正解:主席树+倍增+分治 解题报告: 传送门! 首先看到这题会想到之前考过的这题 但是那题其实简单一些,,,因为那题只要用个分治+预处理就好,只是有点儿思维难度而已 这题就不一样,因为它说了是按照原树 ...

  8. BZOJ_2588_Spoj 10628. Count on a tree_树剖+主席树

    BZOJ_2588_Spoj 10628. Count on a tree_树剖+主席树 题意: 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastan ...

  9. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

随机推荐

  1. RDD

    scala> val rdd1=sc.parallelize(Array("coffe","coffe","hellp"," ...

  2. elasticsearch介绍,安装,安装错误解决及相应插件安装

    一.elasticsearch介绍 1.简介(使用的是nosql,更新比mongodb慢): ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎, ...

  3. String、StringBuffer、StringBuilder三种类型的一点比较

    简要记一点 以运行速度来说: StringBuilder>StringBuffer>String 以线程安全来说: StringBuilder线程不安全,而StringBuffer线程安全 ...

  4. 三、K8S成功

    kubeadm join 172.17.149.114:6443 --token yjogpa.kk85u2i4n5rt1omq --discovery-token-ca-cert-hash sha2 ...

  5. vuex2.0 基本使用(4) --- modules

    vue 使用的是单一状态树对整个应用的状态进行管理,也就是说,应用中的所有状态都放到store中,如果是一个大型应用,状态非常多, store 就会非常庞大,不太好管理.这时vuex 提供了另外一种方 ...

  6. HDU 4256 翻译罗马数字

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6414173.html The Famous Clock Time Limit: 2000/1000 ...

  7. CF280C Game on Tree

    题目链接 : CF280C Game on Tree 题意 : 给定一棵n个节点的树T 根为一(我咕的翻译漏掉了...) 每次随机选择一个未被删除的点 并将它的子树删除 求删整棵树的期望步数 n ∈ ...

  8. zabbix 自定义 nginx 监控模板

    打开zabbix首页→配置→模板→创建模板模板名称:Template App NGINXagent 需添加自定义监控项:UserParameter=nginx.status[*],/bin/bash ...

  9. Codeforces Round #463 F. Escape Through Leaf (李超线段树合并)

    听说正解是啥 set启发式合并+维护凸包+二分 根本不会啊 , 只会 李超线段树合并 啦 ... 题意 给你一颗有 \(n\) 个点的树 , 每个节点有两个权值 \(a_i, b_i\) . 从 \( ...

  10. 栈长这里是生成了一个 Maven 示例项目。

    Spring Cloud 的注册中心可以由 Eureka.Consul.Zookeeper.ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netfl ...