题解:

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. Java中的JDBC是什么?

    JDBC(Java Data Base Connectivity, Java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它有一组用Java语言编写的类和接 ...

  2. linux下多线程之pthread_detach(pthread_self())

    写个碰到的问题,记录下自己的技术之路点滴pthread_detach(pthread_self())linux线程执行和windows不同,pthread有两种状态joinable状态和unjoina ...

  3. 资产证券化(ABS)+ 特殊目的信托(SPV)

    资产证券化是指以基础资产未来所产生的现金流为偿付支持,通过结构化设计进行信用增级,在此基础上发行资产支持证券(Asset-backed Securities, ABS)的过程,通过将有形或者无形资产作 ...

  4. 浅析SQL注入

    body, td { font-family: calibri; font-size: 10pt; } 演示 记得以前瞎鼓捣的时候,学过一个传说中的SQL注入万能字符串,是这个样子的' or '1'= ...

  5. JQuery的click、bind、delegate、off、unbind

    .click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...

  6. 关于Linq的对List<实体>去掉重复ID的一个小例子!

    注意 下面的代码只要ID相同(即使其他的不相同)都会过滤掉,简单来讲就是过滤掉ID相同的实体,如果ID相同,其他属性取第一个的值 List<Abc> list = new List< ...

  7. VCF和GVCF格式说明

    注意:本文的内容主要来自于GATK官网的讲解,所以vcf也是GATK产生的,用其他caller,比如varscan2产生的vcf文件的内容注释可能不一致. 参考:https://gatkforums. ...

  8. 20145301 《Java程序设计》第九周学习总结

    20145301 <Java程序设计>第八周学习总结 教材学习内容总结 整合数据库 JDBC JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作 ...

  9. 《JAVA程序设计》第五次实验报告

    20145333 实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21:3 ...

  10. Finder Quick Menu FAQ

    How to use Finder Quick Menur: 1. Start Finder Quick Menu.2. Open "System Preferences -> Ext ...