文艺平衡树 lg3391(splay维护区间入门)
splay是支持区间操作的,先做这道题入个门
大多数操作都和普通splay一样,就不多解释了,只解释一下不大一样的操作
- #include<bits/stdc++.h>
- using namespace std;
- #define INF 0x3f3f3f3f
- inline int read(){
- int w=,f=;
- char ch=getchar();
- while(ch<''||ch>''){
- if(ch=='-') f=-;
- ch=getchar();
- }
- while(ch>=''&&ch<=''){
- w=(w<<)+(w<<)+ch-;
- ch=getchar();
- }
- return w*f;
- }
- int n,m,tot,cnt,root;
- struct node{
- int ch[],sum,cnt,val,f,rev;//比普通平衡树多一个lazy tag
- }st[];
- inline void push_up(int p){
- st[p].sum=st[st[p].ch[]].sum+st[st[p].ch[]].sum+st[p].cnt;
- }
- inline bool identify(int p){
- return st[st[p].f].ch[]==p;
- }
- inline void connect(int x,int fa,int son){
- st[x].f=fa;st[fa].ch[son]=x;return;
- }
- inline void rotate(int x){
- int y=st[x].f;int z=st[y].f;
- int yson=identify(x);int zson=identify(y);
- int b=st[x].ch[yson^];
- connect(b,y,yson);connect(y,x,(yson^));connect(x,z,zson);
- push_up(y);push_up(x);return;
- }
- inline void splay(int x,int goal){
- while(st[x].f!=goal){
- int y=st[x].f;int z=st[y].f;
- int yson=identify(x);int zson=identify(y);
- if(z!=goal){
- if(yson==zson) rotate(y);
- else rotate(x);
- }
- rotate(x);
- }
- if(!goal) root=x;
- return;
- }
- inline void insert(int x){
- int now=root;int f=;
- while(st[now].val!=x&&now){
- f=now;
- now=st[now].ch[x>st[now].val];
- }
- if(now){
- st[now].cnt++;
- }
- else{
- tot++;now=tot;
- if(f){
- st[f].ch[x>st[f].val]=now;
- }
- st[now].ch[]=st[now].ch[]=;
- st[now].cnt=st[now].sum=;
- st[now].val=x;st[now].f=f;
- }
- splay(now,);return;
- }
- inline void push_down(int p){
- int ls=st[p].ch[];int rs=st[p].ch[];
- if(st[p].rev){
- swap(st[p].ch[],st[p].ch[]);
- st[st[p].ch[]].rev^=;
- st[st[p].ch[]].rev^=;
- st[p].rev=;
- }
- }
- inline void find(int x){
- int now=root;if(!now) return;
- while(st[now].val!=x&&st[now].ch[x>st[now].val]){
- now=st[now].ch[x>st[now].val];
- }
- splay(now,);return;
- }
- inline int Next(int x,int f){
- find(x);int now=root;
- if(st[now].val<x&&!x) return now;
- if(st[now].val>x&&x) return now;
- now=st[now].ch[f];
- while(st[now].ch[f^]) now=st[now].ch[f^];
- return now;
- }
- inline int k_th(int x){
- int now=root;
- if(st[now].sum<x) return false;
- while(true){
- push_down(now);//在查找的时候记得下移标记
- int ls=st[now].ch[];
- if(x>st[ls].sum+st[now].cnt){
- x-=st[ls].sum+st[now].cnt;
- now=st[now].ch[];
- }
- else if(x<=st[ls].sum){
- now=ls;
- }
- else return now;//这个地方把返回原值改成返回位置
- }
- }inline void rev(int l,int r){
- int x=k_th(l-);int y=k_th(r+);
- splay(x,);splay(y,x);
- st[st[y].ch[]].rev^=;
- }//翻转的操作就是将l-1转到根上,r+1转到根的右儿子,然后l到r这个区间就是根右儿子的左儿子(比较绕,可以画个图想一想
- inline void output(int p){
- push_down(p);
- if(st[p].ch[]) output(st[p].ch[]);
- if(st[p].val>=&&st[p].val<=n) printf("%d ",st[p].val);
- if(st[p].ch[]) output(st[p].ch[]);
- }//输出的时候下推一下标记,输出顺序就是二叉树的顺序
- int main(){
- n=read();m=read();int i,j,k;
- insert(INF);insert(-INF);
- for(i=;i<=n;i++){
- insert(i);
- }
- while(m--){
- int x,y;x=read();y=read();
- rev(x+,y+);
- }
- output(root);
- return ;
- }
文艺平衡树 lg3391(splay维护区间入门)的更多相关文章
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 N,M<= ...
- splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)
先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 洛谷 P3391【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
随机推荐
- 剑指offer-面试题32-分行从上到下打印二叉树-二叉树遍历
/* 题目: 分行按层自上向下打印二叉树. */ /* 思路: 使用队列,将节点压入队列中,再弹出来,压入其左右子节点,循环,直到栈为空. 添加两个计数器,current记录当前行的节点数,next记 ...
- P1980 计数问题(int,string,stringstream)
题目描述 试计算在区间 1 到 n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 ...
- 一文看懂AI深度学习丨曼孚科技
深度学习(Deep Learning)是机器学习的一种,而机器学习是实现人工智能的必经途径. 目前大部分表现优异的AI应用都使用了深度学习技术,引领了第三次人工智能的浪潮. 一. 深度学习的概念 深度 ...
- wxpython 简单例子:显示文本框的窗口显示鼠标位置
简单例子来自教程: #!/bin/env python import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__ ...
- Spring mvc拦截器防御CSRF攻击
CSRF(具体参考百度百科) CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSR ...
- 深入理解Java内存模型中的虚拟机栈
深入理解Java内存模型中的虚拟机栈 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域,这些区域都会有各自的用途,以及创建和销毁的时间,有的区域会随着虚拟机进程的启 ...
- udp_demo(傻瓜来回发送)
代码讲解 import socket # 发送数据 def send_data(udp_socket, dest_ip, dest_port): send_msg = input('请输入要发送的数据 ...
- 微信小程序调起支付API
官方文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7 https://developers.weixin.q ...
- Costco这样的超级零售商,能不能干掉电商?
名创优品创始人叶国富曾说过,Costco只是没有来到中国(大陆),如果它来了,中国现在的零售业全部都会"死光".叶国富的话,似乎一语成箴. 随着Costco正式入华,其正在彻底搅动 ...
- vue动态绑定图片和背景图
1.动态绑定图片 <img class="binding-img" :src="require('../assets/images/test.png')" ...