zoj3765
题解:
splay维护
注意是gcd
代码:
#include<bits/stdc++.h>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int N=;
char op[];
int pre[N],ch[N][],key[N],size[N],root,tot1,L,R,pos,_st,val;
int sum0[N],sum1[N],st[N],s[N],tot2,a[N],status[N],n,q;
int gcd(int a,int b)
{
if (!b)return a;
else return gcd(b,a%b);
}
void NewNode(int &r,int father,int k,int _st)
{
if (tot2)r=s[tot2--];
else r=++tot1;
pre[r]=father;
ch[r][]=ch[r][]=;
key[r]=k;
st[r]=_st;
if (_st==){sum0[r]=k;sum1[r]=;}
else {sum1[r]=k;sum0[r]=;}
size[r]=;
}
void push_up(int r)
{
int lson=ch[r][],rson=ch[r][];
size[r]=size[lson]+size[rson]+;
sum0[r]=sum1[r]=;
sum0[r]=gcd(sum0[lson],sum0[rson]);
sum1[r]=gcd(sum1[lson],sum1[rson]);
if (st[r])sum1[r]=gcd(sum1[r],key[r]);
else sum0[r]=gcd(sum0[r],key[r]);
}
void Build(int &x,int l,int r,int father)
{
if (l>r)return;
int mid=(l+r)/;
NewNode(x,father,a[mid],status[mid]);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
void Rotate(int x,int kind)
{
int y=pre[x];
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if (pre[y])ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
push_up(y);
}
void splay(int r,int goal)
{
while (pre[r]!=goal)
{
if (pre[pre[r]]==goal)Rotate(r,ch[pre[r]][]==r);
else
{
int y=pre[r],kind=ch[pre[y]][]==y;
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if (!goal)root=r;
}
int Get_kth(int r,int k)
{
int t=size[ch[r][]]+;
if (t==k)return r;
if (t>k)return Get_kth(ch[r][],k);
else return Get_kth(ch[r][],k-t);
}
void Insert(int pos,int _val,int _st)
{
splay(Get_kth(root,pos+),);
splay(Get_kth(root,pos+),root);
NewNode(Key_value,ch[root][],_val,_st);
push_up(ch[root][]);
push_up(root);
}
void erase(int r)
{
if(!r)return;
s[++tot2] = r;
erase(ch[r][]);
erase(ch[r][]);
}
void Delete(int pos)
{
splay(Get_kth(root,pos),);
splay(Get_kth(root,pos+),root);
erase(Key_value);
pre[Key_value] = ;
Key_value = ;
push_up(ch[root][]);
push_up(root);
}
void Change(int pos)
{
splay(Get_kth(root,pos),);
splay(Get_kth(root,pos+),root);
st[Key_value]^=;
push_up(Key_value);
push_up(ch[root][]);
push_up(root);
}
void Modify(int pos,int _val)
{
splay(Get_kth(root,pos),);
splay(Get_kth(root,pos+),root);
key[Key_value]=_val;
push_up(Key_value);
push_up(ch[root][]);
push_up(root);
}
int Query(int L,int R,int _st)
{
int ans;
splay(Get_kth(root,L),);
splay(Get_kth(root,R+),root);
if (!_st)ans=sum0[Key_value];
else ans=sum1[Key_value];
if (!ans)ans=-;
return ans;
}
int main()
{
while (~scanf("%d%d",&n,&q))
{
root=tot1=tot2=;
ch[root][]=ch[root][]=size[root]=pre[root]=sum0[root]=sum1[root]=;
NewNode(root,,,);
NewNode(ch[root][],root,,);
for (int i=;i<n;i++)scanf("%d%d",&a[i],&status[i]);
Build(Key_value,,n-,ch[root][]);
push_up(ch[root][]);push_up(root);
while(q--)
{
scanf("%s",op);
if (op[]=='Q')
{
scanf("%d%d%d",&L,&R,&_st);
printf("%d\n",Query(L,R,_st));
}
if (op[]=='I')
{
scanf("%d%d%d",&pos,&val,&_st);
Insert(pos,val,_st);
}
if (op[]=='D')
{
scanf("%d",&pos);
Delete(pos);
}
if (op[]=='R')
{
scanf("%d",&pos);
Change(pos);
}
if (op[]=='M')
{
scanf("%d%d",&pos,&val);
Modify(pos,val);
}
}
}
return ;
}
zoj3765的更多相关文章
- ZOJ3765 Lights Splay树
非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...
随机推荐
- Android4.0 Surface机制分析
1. java层面的Surface 对于Surface我们的认识主要是android的类Surface, android的文档描述Surface是"Handle onto a raw ...
- JavaWeb项目中各种路径的获取
以工程名为/DemoWeb为例: 访问的jsp为: http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工 ...
- iOS “弱账号” 暗转 “强账号”
一.背景 由于某些历史原因,我们产品中50%以上活跃用户是弱账户.即 客户端按照某种规则生成的一个伪id 存在keychain 里,作为这个用户的唯一标识,实现快速登录.正常情况下是不会有问题. 最近 ...
- mongodb研究(mongodb 内存数据库)
本日志大部分都不是原创的转载复制的会带链接保持版权 工作中使用mongodb已经好久了,讽刺的是到了最后快离职的时候才有时间好好研究下源码. 印象:mongodb是一个内存数据库,数据都是放到内存 ...
- Java银行家算法
实验存档,代码特别烂.. 测试.java package operating.test; import operating.entity.bank.Bank; import operating.ent ...
- 在python3下对数据分块(8x8大小)使用OpenCV的离散余弦变换DCT
在MATLAB中有blkproc (blockproc)对数据处理, 在python下没找到对应的Function, 这里利用numpy 的split(hsplit和vsplit) 对数据分块处理成8 ...
- Django学习笔记之CBV和FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- 有道云笔记配合MPic+七牛云 自制MarkDown文档图床(适用Typora)
注:从有道云笔记v6.5开始,有道云笔记会员可以使用MarkDown有道自带的图床.(但是非会员可以采用下面的七牛云图床+MarkDown方法) 0x00 前言 一直用有道云笔记,粘贴图片,做笔记没问 ...
- 如何用纯 CSS 创作单元素点阵 loader
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/YvBvBr 可交互视频 此 ...
- hashmap总结2
1. 关于HashMap的一些说法: a) HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体.HashMap的底层结构是一个数组,数组中的每一项是一条链表. b) Hash ...