题目链接: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. http如何301到https呢?

    HTTPS协议的站点信息更加安全,同时可降低网站被劫持的风险,Firefox和chrome浏览器对访问一些非https站点会提示风险,BD等搜索引擎也明确表态了对https站点的友好.那么我们如何部署 ...

  2. 64位win10+cuda8.0+vs2013+cuDNN V5下Caffe的编译安装教程并配置matlab2014a 接口

    一.需要安装的软件 1)vs2013,我是在http://www.52pojie.cn/thread-492326-1-1.html这个网址安装的.我之前用的是vs2012,按照网上的配置教程会爆各种 ...

  3. [py]access日志入mysql-通过flask前端展示

    目录 pymysql组装sql入库日志 代码组织 将入库的日志通过flask前端展示 pymysql组装sql入库日志 pymysql模块的用法 采集这些指标(metirc)都是linux环境,会用到 ...

  4. 【Cocos2dx 3.3 Lua】定时器事件

    Cocos2dx 3.x Lua 中使用定时器有两种方式: (1)self:scheduleUpdateWithPriorityLua(update, priority) > 参数一:刷新函数 ...

  5. [LeetCode] 88. Merge Sorted Array_Easy tag: Two Pointers

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  6. LeetCode--44 通配符匹配

    题目就是说两个字符串,一个含有通配符,去匹配另一个字符串:输出两个字符串是否一致. 注意:’?’表示匹配任意一个字符,’*’表示匹配任意字符0或者多次 首先,我们想到暴力破解.如果从头到尾的破解,到第 ...

  7. python中的re模块中的向后引用和零宽断言

    1.后向引用 pattern = re.compile(r"(\w+)")#['hello', 'go', 'go', 'hello'] # pattern = re.compil ...

  8. Http请求中Content-Type和Accept讲解以及在Spring MVC中的应用

    在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在 ...

  9. 实体框架 Code First

    原文:https://msdn.microsoft.com/zh-cn/en-zn/data/jj591621

  10. svg绘图工具raphael.js的使用

    1.raphael.js svg画图的开源库,支持IE8+ 官方api: http://dmitrybaranovskiy.github.io/raphael/reference.html Githu ...