链接

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 ;
}

最新文章

  1. 【先定一个小目标】Windows下Redis的安装使用
  2. GD库处理图像
  3. 微信调试、API、AJAX的调试 SocketLog
  4. IIS 架构解析
  5. ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
  6. 持续集成基础-Jenkins(二)-搭建Jenkins环境和配置第一个Job
  7. Java Hour 50 日期类型
  8. hook_schema 小总结
  9. UVa 1453 - Squares 旋转卡壳求凸包直径
  10. wamp下开启SSL,解决APACHE启动问题
  11. poj-3895-Cycles of Lanes 简单DFS
  12. 17.4.3 使用MulticastSocket实现多点广播(2)
  13. Java与C/C++的区别
  14. WKWebView使用
  15. HTML学习笔记4:文档申明和编码标签
  16. C# 将datatable导出成Excel
  17. Android 开发 使用javax.mail发送邮件。
  18. map里面的set方法
  19. vue项目中的tab页实现
  20. WPF中ComboBox使用

热门文章

  1. springmvc配置一:ajax请求防止返回中文乱码配置说明
  2. PHP/Javascript 数组定义 及JSON中的使用 ---OK
  3. 错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”
  4. Kmeans算法--python实现
  5. [poj2186]Popular Cows(targin缩点)
  6. Flutter实战视频-移动电商-18.首页_火爆专区后台接口调试
  7. PXE与cobbler实现系统自动安装
  8. visual editor ve1.5下载
  9. 利用ant 和 Junit 生成测试报告
  10. java web 学习-网络资源