zoj3765Lights(splay)
splay的增删改操作。
刚开始对于某段区间首先有了lazy标记时,把其左右孩子给交换了,导致在pushup时又交换了一次而debug了n久。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 300010
#define LL long long
#define INF 0xfffffff
#define key_value ch[ch[root][1]][0]
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
using namespace std;
int a[N][];
struct splay_tree
{
int pre[N],size[N],vv[N];
int ch[N][];
int root,n,tot,num,tot1;
int s[N][],sta[N],key[N];
void dfs(int x)
{
if(x)
{
dfs(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d s0=%d s1 = %d vv=%d\n",
x,ch[x][],ch[x][],pre[x],size[x],key[x],s[x][],s[x][],vv[x]);
dfs(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
dfs(root);
}
//以上用于debug*/
void newnode(int &x,int v,int sta,int fa)//新建一结点
{
x = ++tot;
ch[x][]=ch[x][] = ;
pre[x] = fa;
s[x][] = s[x][] = ;
s[x][sta] = v;
size[x] = ;
vv[x] = sta;
key[x] = v;
}
void pushup(int w)//由儿子更新其父亲
{
size[w] = size[ch[w][]]+size[ch[w][]]+;
int st = vv[w];
int l = ch[w][],r = ch[w][];
int cnt1 = ,cnt2 = ;
cnt1 = s[l][];
if(s[r][])
{
if(cnt1) cnt1 = __gcd(s[r][],cnt1);
else cnt1 = s[r][];
}
cnt2 = s[l][];
if(s[r][])
{
if(cnt2) cnt2 = __gcd(s[r][],cnt2);
else cnt2 = s[r][];
}
s[w][] = cnt1,s[w][] = cnt2;
s[w][st] = s[w][st]?__gcd(s[w][st],key[w]):key[w];
//cout<<s[w][0]<<" "<<s[w][1]<<endl;
}
void rotate(int r,int kind)//旋转操作,根据kind进行左旋和右旋
{
int y = pre[r];
ch[y][!kind] = ch[r][kind];
pre[ch[r][kind]] = y;
if(pre[y])
{
ch[pre[y]][ch[pre[y]][]==y] = r;
}
pre[r] = pre[y];
ch[r][kind] = y;
pre[y] = r;
pushup(y);
pushup(r);
}
void splay(int r,int goal)//将r结点旋至goal下
{
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
rotate(r,ch[pre[r]][]==r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][]==y);
if(ch[y][kind]==r)
{
rotate(r,!kind);
rotate(r,kind);
}
else
{
rotate(y,kind);
rotate(r,kind);
}
}
}
pushup(r);
if(goal==) root = r;
}
int get_k(int k)//得到第k个的结点
{
int r = root;
while(size[ch[r][]]+!=k)
{
if(size[ch[r][]]>=k)
r = ch[r][];
else
{
k-=(size[ch[r][]]+);//根据左右结点的数量来确定第k个节点在哪里
r = ch[r][];
}
}
pushup(r);
return r;
}
void add(int k,int val,int sta)//添加一个结点 位置自己修改 这里为第一个
{
splay(get_k(k),);
splay(get_k(k+),root);
int r = ch[root][];
newnode(ch[r][],val,sta,r);
pushup(r);
pushup(root);
}
void updelete(int r)//删除第r个结点
{
splay(get_k(r),);
splay(get_k(r+),root);
key_value = ;
pushup(ch[root][]);
pushup(root);
}
void update(int k,int flag,int v) //更新区间信息
{
splay(get_k(k),);
splay(get_k(k+),root);
if(flag)
{
vv[key_value]^=;
swap(s[key_value][],s[key_value][]);
}
else
{key[key_value] = v;s[key_value][vv[key_value]] = v;}
pushup(ch[root][]);
pushup(root); }
int query(int l,int r,int sta)//询问l,r区间,将第l-1个结点旋自根,第r+1个结点旋自根的有儿子,
{
//则l-r就变成了根的右儿子的左儿子
splay(get_k(l),);
splay(get_k(r+),root);
return s[key_value][sta];
}
void build(int &x,int l,int r,int fa)
{
int m = (l+r)>>;
if(l>r) return ;
newnode(x,a[m][],a[m][],fa);
build(ch[x][],l,m-,x);
build(ch[x][],m+,r,x);
pushup(x);
}
void init(int o)
{
int i;
for(i = ; i <= o; i++)
scanf("%d%d",&a[i][],&a[i][]);
size[] = ch[][] = ch[][] = s[][] = s[][] = key[] = vv[] = ;
root = tot = ;
newnode(root,,,);
newnode(ch[root][],,,root);
build(ch[ch[root][]][],,o,ch[root][]);
size[root] = ;
pushup(ch[root][]);
pushup(root);
}
}SP;
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
SP.init(n);
while(q--)
{
char sq[];
int k,x,y;
scanf("%s%d",sq,&k);
if(sq[]=='I')
{
scanf("%d%d",&x,&y);
SP.add(k+,x,y);
// SP.debug();
}
else if(sq[]=='D')
{
SP.updelete(k);
}
else if(sq[]=='R')
{
SP.update(k,,);
}
else if(sq[]=='M')
{
scanf("%d",&x);
SP.update(k,,x);
}
else if(sq[]=='Q')
{
scanf("%d%d",&x,&y);
int ans = SP.query(k,x,y);
if(ans==)
printf("-1\n");
else
printf("%d\n",ans);
// SP.debug();
}
}
}
return ;
}
zoj3765Lights(splay)的更多相关文章
- ZOJ3765---Lights (Splay伸展树)
Lights Time Limit: 8 Seconds Memory Limit: 131072 KB Now you have N lights in a line. Don't wor ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- splay最终模板
来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 【填坑】bzoj3224 splay裸题
人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...
随机推荐
- hdu-5726 GCD(rmq)
题目链接: GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- 最新版ADT(Build: v22.6.2)总是引用appcompat_v7的问题
昨天在ADT Manager里更新了一些组件,结果ADT不支持.索性直接下载了最新的ADT.但是发现无论创建什么类型的应用(无论支持的最低API是多少,或者是不是用模板),都会在创建应用的同时创建一个 ...
- HihoCoder 1504 : 骑士游历 (矩阵乘法)
描述 在8x8的国际象棋棋盘上给定一只骑士(俗称“马”)棋子的位置(R, C),小Hi想知道从(R, C)开始移动N步一共有多少种不同的走法. 输入 第一行包含三个整数,N,R和C. 对于40%的数据 ...
- SIP协议&开源SIP服务器搭建和客户端安装
1. SIP SIP 是一个应用层的控制协议,可以用来建立,修改,和终止多媒体会话,例如Internet电话 SIP在建立和维持终止多媒体会话协议上,支持五个方面: 1) 用户定位: 检查终端用户 ...
- Windows下安装zip包解压版mysql
Windows下安装zip包解压版mysql 虽然官方提供了非常好的安装文件,但是有的时候不想每次再重装系统之后都要安装一遍MySQL,需要使用zip包版本的MySQL.在安装时需如下三步: 1. 新 ...
- Asset Catalog Help (九)---Changing Image Set Names
Changing Image Set Names Use the Attributes inspector to edit a set’s name. 使用属性检查器(Attributes inspe ...
- 【eclipse插件开发实战】Eclipse插件开发2——SWT
Eclipse插件开发实战2--SWT 一.SWT简介 SWT(StandardWidget Toolkit) 标准小窗口工具箱,一开源的GUI编程框架,与AWT/Swing有相似的用处,eclips ...
- ASP.NET Core会议管理平台实战_2、基本概念的理解
id Token携带用户的信息 AccessToken:是否有权限访问资源 看数据库的表,Client相关的表,api的相关的表 Resources把用户的简介抽象出来到IdentityClaims表 ...
- PHP中正则表达式学习及应用(二)
正则表达式中的“元字符” * 匹配前一个内容的0次1次或多次 例如: <?php $mode="/go*gle/"; //前一个内容指的是 * 的前一个字符 o ,在$str ...
- HDU - 4006 The kth great number multiset应用(找第k大值)
The kth great number Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming ...