题解:

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的更多相关文章

  1. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

随机推荐

  1. Salesforce视图与控制器之间的交互

    刚接触Salesforce,过程的确是比较艰难了,中文资料几乎没有,看英文资料学的效率却不高,不过看了一段时间的英文资料发现自己英语水平挺高不少啊,现在看都不用工具翻译,早知道就再次尝试报个6级,看下 ...

  2. $如何用Python装饰器实现一个代码计时器?

    有时候我们很希望看到程序中某个函数或某个代码段的耗时情况,那么该如何办呢?本文用两种方式实现了代码计时器的功能,第一种方式是采用装饰器来实现,第二种方式采用上下文管理器实现. 其实计算代码的运行时间, ...

  3. 使用selenium前学习HTML(3)——元素

    <!-- HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 注释:开始标签常被称为开放标签(opening tag),结束标签常称为闭合标签(clos ...

  4. Spark机器学习9· 实时机器学习(scala with sbt)

    1 在线学习 模型随着接收的新消息,不断更新自己:而不是像离线训练一次次重新训练. 2 Spark Streaming 离散化流(DStream) 输入源:Akka actors.消息队列.Flume ...

  5. CentOS的Qt3和Qt4问题

    在有的系统中,装有Qt3和Qt4, 在使用qmake生成Makefile后,直接make, 出错,说没有头文件, 如果调用了qt3的qmake,那么上头的INCPATH里的头文件路径也指向了Qt3, ...

  6. 20145324 《Java程序设计》第10周学习总结

    20145324 <Java程序设计>第10周学习总结 教材学习内容总结 1.网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输 2.在实际传输数据以前需要将域名转换为IP地 ...

  7. LeetCode——remove-duplicates-from-sorted-list-ii

    Question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist ...

  8. Python学习札记(十三) Function3 函数参数二

    参考:函数参数 Note A.关键字参数: 1.关键字参数:**kw 可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple.而关键字参数允许你传入0个或任意个含参数名的 ...

  9. Python学习札记(九) Basic6 dict and set

    参考:dict and set Note: A.dict Hint:注意最后三点. 1.Python内置字典dict,全称directory,在别的语言如C++中称为map,使用键值-value存储, ...

  10. RabbitMQ入门(6)——远程过程调用(RPC)

    在RabbitMQ入门(2)--工作队列中,我们学习了如何使用工作队列处理在多个工作者之间分配耗时任务.如果我们需要运行远程主机上的某个方法并等待结果怎么办呢?这种模式就是常说的远程过程调用(Remo ...