题目链接:https://www.luogu.org/problemnew/show/P1533

没人写$fhq\ treap$做法,那我就补一篇qwq

看到这题第一时间想主席树,然后发现我还没学主席树,于是就写了平衡树做法(当然树状数组+二分的套路也是可以的,但是两个$log$的复杂度太优秀了就不写了)

其实和treap的写法差不多,也是排序一遍然后插入,删除,求$kth$

不过好写很多,主体就40+行,注意一开始那个节点的值一定要大...

我开$0x3f$然后挂了一个上午...

(其实一开始还写了莫队来着但是发现不需要)

#include <bits/stdc++.h>

#define ll long long
#define inf 0x7fffffff
#define il inline namespace io { #define in(a) a=read()
#define out(a) write(a)
#define outn(a) out(a),putchar('\n') #define I_int int
inline I_int read() {
I_int x = , f = ; char c = getchar() ;
while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
return x * f ;
}
char F[ ] ;
inline void write( I_int x ) {
if( x == ) { putchar( '' ) ; return ; }
I_int tmp = x > ? x : -x ;
if( x < ) putchar( '-' ) ;
int cnt = ;
while( tmp > ) {
F[ cnt ++ ] = tmp % + '' ;
tmp /= ;
}
while( cnt > ) putchar( F[ -- cnt ] ) ;
}
#undef I_int }
using namespace io ; using namespace std ; #define N 300010
#define int long long int n , tot = , root = ; //fhq-treap
struct fhq_treap {
int siz , val , lc , rc , rnk ;
} t[N] ;
void pushup(int rt) {t[rt].siz = t[t[rt].lc].siz + t[t[rt].rc].siz + ;}
void split(int &a , int &b , int val , int rt) {
if(!rt) { a = b = ; return ; }
if(t[rt].val <= val) a = rt , split(t[a].rc , b , val , t[rt].rc) ;
else b = rt , split(a , t[b].lc , val , t[rt].lc) ;
pushup(rt) ;
}
void merge(int a , int b , int &rt) {
if(!a || !b) {rt = a + b ; return ;}
if(t[a].rnk < t[b].rnk) rt = a , merge(t[a].rc , b , t[rt].rc) ;
else rt = b , merge(a , t[b].lc , t[rt].lc) ;
pushup(rt) ;
}
int new_node(int val) {
t[++tot] = (fhq_treap) { , val , , , rand()} ;
return tot ;
}
inline void insert(int val) {
int x = , y = , z = new_node(val) ;
split(x , y , val , root) ;
merge(x , z , x) ; merge(x , y , root) ;
}
void Del(int val) {
int x = , y = , z = ;
split(x , y , val , root) ; split(x , z , val - , x) ;
merge(t[z].lc , t[z].rc , z) ; merge(x , z , x) ; merge(x , y , root) ;
}
inline int find_val(int rnk , int rt) {
while(t[t[rt].lc].siz + != rnk) {
if(t[t[rt].lc].siz >= rnk) rt = t[rt].lc ;
else rnk -= t[t[rt].lc].siz + , rt = t[rt].rc ;
}
return t[rt].val ;
}
//fhq-treap end struct query {
int l , r , val , id;
} q[N] ;
int ans[N] , a[N] , block ; bool cmp(query a , query b) {
return a.l == b.l ? a.r < b.r : a.l < b.l ;
} signed main() {
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
srand((unsigned)time()) ;
n = read() ; int m = read() ;
new_node(inf) ; t[].siz = ;
for(int i = ; i <= n ; i ++) a[i] = read() ;
for(int i = ; i <= m ; i ++) q[i] = (query) {read() , read() , read() , i} ;
block = sqrt(n) ; sort(q + , q + m + , cmp) ;
q[].l = ;
for(int i = ; i <= m ; i ++) {
for(int cur = q[i-].r + ; cur <= q[i].r ; cur ++) insert(a[cur]) ;
for(int cur = q[i-].l ; cur < q[i].l ; cur ++) Del(a[cur]) ;
ans[q[i].id] = find_val(q[i].val , root) ;
}
for(int i = ; i <= m ; i ++) outn(ans[i]) ;
}

Luogu P1533 可怜的狗狗的更多相关文章

  1. luogu P1533 可怜的狗狗 |莫队+二分

    题目背景 小卡由于公务需要出差,将新家中的狗狗们托付给朋友嘉嘉,但是嘉嘉是一个很懒的人,他才没那么多时间帮小卡喂狗狗. 题目描述 小卡家有N只狗,由于品种.年龄不同,每一只狗都有一个不同的漂亮值.漂亮 ...

  2. 【题解】Luogu P1533 可怜的狗狗

    原题传送门 莫队介绍,Splay介绍 离线的题目,莫队是不错的解决方法 先把询问排一下序 剩下就套一个莫队的板子 每来一只狗就把漂亮值插入平衡树 每去掉一只狗就把漂亮值从平衡树中删掉 每次查询查平衡树 ...

  3. 洛谷P1533 可怜的狗狗 [平衡树,FHQ_Treap]

    题目传送门 可怜的狗狗 题目背景 小卡由于公务需要出差,将新家中的狗狗们托付给朋友嘉嘉,但是嘉嘉是一个很懒的人,他才没那么多时间帮小卡喂狗狗. 题目描述 小卡家有N只狗,由于品种.年龄不同,每一只狗都 ...

  4. P1533 可怜的狗狗

    http://www.luogu.org/problem/show?pid=1533 题目背景 小卡由于公务需要出差,将新家中的狗狗们托付给朋友嘉嘉,但是嘉嘉是一个很懒的人,他才没那么多时间帮小卡喂狗 ...

  5. [Luogu 1533] 可怜的狗狗

    平衡树,我用的SBT. 排一下序尽量减少操作次数. 第K大询问. 以及插入删除. #include <algorithm> #include <cstdio> #include ...

  6. P1533可怜的狗狗

    困死了,完全做不下去题 就当是对莫队最最基本的思想的一个复习叭(只有最最基本的思想,没有莫队) 传送 我们可以很容易的想到这题要用线段树. 60pts 此题要求某个区间里第K小的数,可以暴力的考虑对每 ...

  7. AC日记——可怜的狗狗 洛谷 P1533

    可怜的狗狗 思路: 主席树第k大: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300001 #defin ...

  8. 【luoguP1533】可怜的狗狗

    题目链接 发现区间按左端点排序后右端点也是单调的,所以扫一遍就行了,用权值线段树维护第\(k\)大 #include<algorithm> #include<iostream> ...

  9. Splay详解

    平衡树实际很简单的 以下讲解都以Luogu P3369 [模板]普通平衡树为例 我不会带指针的Splay,所以我就写非指针型的Splay Splay是基于二叉查找树(bst)实现的 什么是二叉查找树呢 ...

随机推荐

  1. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  2. 带分数dfs+剪枝优化

    #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using name ...

  3. android Thread

    1.Thread的三种形式 第一种: class MyThread extends Thread{ @Override public void run(){ Log.d("MainActiv ...

  4. auto类型-现代C++新特性

    auto类型 C++11中引入的auto主要用于类型推导.auto在C++98中"存储类型指示符"的语义,由于使用极少且多余,该语义从C++11开始被删除. auto类型推导用于从 ...

  5. [django]模板template原理

    django 中的render和render_to_response()和locals(): http://www.cnblogs.com/wangchaowei/p/6750512.html 什么是 ...

  6. [sh]sh最佳实战(含grep)

    sh虐我千百遍,我待sh如初恋. sh复习资料 http://www.cnblogs.com/iiiiher/p/5385108.html http://blog.csdn.net/iiiiher/a ...

  7. java的时间处理

    采用joda.time库 gradle,可以简化calendar的 compile "joda-time:joda-time:2.7" 例子:http://blog.csdn.ne ...

  8. 测试开发-web测试要点

    参数输入考虑 参数数值包含1个.多个.很多个.null.参数值前后包含空格的2种情况   数字类型:正数.负数.0.0.0.+0.0.-0.0.指数.对数.分数.小数.复数.科学计数法的测试,全角的数 ...

  9. html5 manifest 离线缓存知识点

    1.最大缓存容量为 5M. 2.manifest文件需要配置正确的MIME-type,即“text/cache-manifest”,这个是在web服务器上进行配置. ②编写.manifest文件,文件 ...

  10. QImage与QPixmap完全解析

    转载自http://www.civilnet.cn/bbs/browse.php?topicno=4691 用Qt程序在手机上显示一幅图片对编程人员来说是再基础不过的一件事情了.那么先让大家看两段代码 ...