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 ...
随机推荐
- Idea中的插件-列出Java Bean的所有set方法
插件的git 地址: https://github.com/yoke233/genSets 将插件jar导入idea中,使用方式是对象后加.allset,然后回车.
- hpuoj 1193: Interval
Interval [STL.双指针.二分] 题目链接 http://acm.hpu.edu.cn/problem.php?id=1193 或者 题目链接 http://acm.nyist.net/Ju ...
- Opencv— — mix channels
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- JNI之JAVA调用C++接口
1.JNI定义(来自百度百科) JNI是Java Native Interface的缩写,中文为JAVA本地调用.从Java1.1开始,Java Native Interface(JNI)标准成为ja ...
- OpenWrt路由器通过iPhone有线共享网络上网
2018年4月更新: 我自己的手机在openwrt上网速很慢,在电脑上又很快.应该不是被限速了,但是没找到原因. 三大运营商在学校争客户,手机卡开出了校内无限流量的条件.很开心,之前准备到东北大学的时 ...
- 关于git被误删除的分支还原问题
在开发过程中, 有可能会将正在开发的本地分支误删, 本地分支被删除时, 如果已经将本地分支的变更推送到了远端, 还没有问题, 如果被删除的本地分支只提交了没有推送到远端, 就悲剧了, 相当于在你上一次 ...
- (转)data Table的用法大全
jqyery dataTable 基本用法 一:官方网站:[http://www.datatables.net/] 二:基本使用:[http://www.guoxk.com/node/jquery-d ...
- FlashkUI v1.33 发布(提供移动设备支持)
v1.33 Beta更新内容:增加对移动设备的支持,新增自定义双渲染器双层树组件.List增加按数据子项排序功能. <ignore_js_op> 介绍: Flex已经不作为Adobe官方支 ...
- CodeForces 41A+43A【课上无聊刷水题系列】
41Acode 好像只要前一个字符串存在下一个字符串的头单词就YES: #include <bits/stdc++.h> using namespace std; typedef __in ...
- POJ3414(BFS+[手写队列])
贴一发自己写的手写队列-.. #include <stdio.h> #include <iostream> #include <string.h> #include ...