看到数组有插入有删除差不多就决定是splay了,这题按照splay想的话是比较裸的,在每个点分别维护这个区间内开灯的GCD和关灯的GCD,如果在一个没有开灯的区间内,那么关灯的GCD设为-1,然后再GCD函数内做一下特判就行了,剩下的都是splay的基本操作,个人感觉唯一的缺点就是代码量有些大,现场如果有其他方法的话还是别考虑splay了。
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 305005
#define mid ((l+r)>>1)
using namespace std;
int nm[N],tst[N],val[N],st[N];
int ch[N][2],sz[N],pre[N],gc[N][2];
int idx,root,n;
int gcd(int a,int b)
{
if(a == -1)return b;
if(b == -1)return a;
return b == 0?a:gcd(b,a%b);
}
void NewNode(int &rt,int fa,int v,int sst)
{
rt = ++idx;
pre[rt] = fa;
val[rt] = v;
st[rt] = sst;
sz[rt] = 1;
ch[rt][0] = ch[rt][1] = 0;
gc[rt][sst] = v;
gc[rt][sst^1] = -1;
}
void PushUp(int rt)
{
sz[rt] = sz[ch[rt][0]]+sz[ch[rt][1]]+1;
gc[rt][0] = gcd(gc[ch[rt][0]][0],gc[ch[rt][1]][0]);
gc[rt][1] = gcd(gc[ch[rt][0]][1],gc[ch[rt][1]][1]);
gc[rt][st[rt]] = gcd(gc[rt][st[rt]],val[rt]);
}
void Rotate(int x,int kind)
{
int y=pre[x];
ch[y][kind^1]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
PushUp(y);
}
void Splay(int rt,int goal)
{
while(pre[rt]!=goal)
{
int y=pre[rt];
if(pre[y]==goal)
Rotate(rt,ch[y][0]==rt);
else
{
int kind=ch[pre[y]][0]==y;
if(ch[y][kind]==rt)
{
Rotate(rt,kind^1);
Rotate(rt,kind);
}
else
{
Rotate(y,kind);
Rotate(rt,kind);
}
}
}
PushUp(rt);
if(goal==0) root=rt;
}
void Select(int k,int goal)
{
int rt=root;
while(sz[ch[rt][0]]!=k)
{
if(sz[ch[rt][0]]>k)
rt=ch[rt][0];
else
{
k-=(sz[ch[rt][0]]+1);
rt=ch[rt][1];
}
}
Splay(rt,goal);
}
void build(int l,int r,int &rt,int fa)
{
if(l>r)return;
NewNode(rt,fa,nm[mid],tst[mid]);
build(l,mid-1,ch[rt][0],rt);
build(mid+1,r,ch[rt][1],rt);
PushUp(rt);
}
void Insert(int pos,int x,int sst)
{
Select(pos,0);
if(ch[root][0] == 0)
{
NewNode(ch[root][0],root,x,sst);
PushUp(root);
return ;
}
int ls = ch[root][0];
while(ch[ls][1])ls = ch[ls][1];
Splay(ls,root);
NewNode(ch[ls][1],ls,x,sst);
PushUp(ls);
PushUp(root);
}
void Delete(int pos)
{
Select(pos,0);
if(ch[root][0]==0)
{
root=ch[root][1];
pre[root]=0;
return ;
}
int ls=ch[root][0];
while(ch[ls][1]) ls = ch[ls][1];
Splay(ls,root);
int rt=ch[root][1];
ch[ls][1]=rt;
pre[rt]=ls;
root=ls;
pre[ls]=0;
PushUp(root);
}
void Modify(int pos,int x)
{
Select(pos,0);
val[root] = x;
PushUp(root);
}
void Change(int pos)
{
Select(pos,0);
st[root]^=1;
PushUp(root);
}
int Query(int l,int r,int sst)
{
Select(l-1,0);
Select(r+1,root);
int ret = gc[ch[ch[root][1]][0]][sst];
return ret?ret:-1;
}
void ini()
{
idx = root = 0;
NewNode(root,0,-1,0);
NewNode(ch[root][1],root,-1,0);
build(1,n,ch[ch[root][1]][0],ch[root][1]);
PushUp(ch[root][1]);
PushUp(root);
}
int main()
{
int q,i,j;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(i = 1;i<=n;i++)scanf("%d%d",&nm[i],&tst[i]);
ini();
for(i = 1;i<=q;i++)
{
char op[2];
int a,b,c;
scanf("%s",op);
if(op[0] == 'Q')
{
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",Query(a,b,c));
}
else if(op[0] == 'I')
{
scanf("%d%d%d",&a,&b,&c);
Insert(a+1,b,c);
}
else if(op[0] == 'D')
{
scanf("%d",&a);
Delete(a);
}
else if(op[0] == 'M')
{
scanf("%d%d",&a,&b);
Modify(a,b);
}
else
{
scanf("%d",&a);
Change(a);
}
}
}
return 0;
}