ZOJ 3765 Lights(SplayTree)

ZOJ 3765 Lights(SplayTree)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765

分析:

       SplayTree的操作应用.与之前做的SplayTree题目基本一致,还是那些操作.

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=300000+10;
#define Key_Value ch[ch[root][1]][0]
int pre[maxn],ch[maxn][2],size[maxn],key[maxn],root,tot1;
int st[maxn];//该节点的状态
int sum0[maxn],sum1[maxn];//该子树对应状态的gcd
int s[maxn],tot2;
int a[maxn],status[maxn];
int n,q;

long long gcd(long long a,long long b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
void New_Node(int &r,int fa,int k,int _st)
{
    if(tot2) r=s[tot2--];
    else r=++tot1;
    key[r]=k;
    pre[r]=fa;
    st[r]=_st;
    ch[r][0]=ch[r][1]=0;
    sum0[r]=sum1[r]=0;
    if(st[r]) sum1[r]=key[r];
    else sum0[r]=key[r];
    size[r]=1;
}
void Push_Up(int r)
{
    int lson=ch[r][0],rson=ch[r][1];
    size[r]=1+size[lson]+size[rson];
    sum0[r]=sum1[r]=0;
    sum0[r]=gcd(sum0[lson],sum0[rson]);
    sum1[r]=gcd(sum1[lson],sum1[rson]);
    if(st[r]) sum1[r]=gcd(key[r],sum1[r]);
    else sum0[r]=gcd(key[r],sum0[r]);
}
void Build(int &x,int l,int r,int fa)
{
    if(l>r)return;
    int mid=(l+r)>>1;
    New_Node(x,fa,a[mid],status[mid]);
    Build(ch[x][0],l,mid-1,x);
    Build(ch[x][1],mid+1,r,x);
    Push_Up(x);
}
void Init()
{
    root=tot1=tot2=0;
    ch[root][0]=ch[root][1]=pre[root]=size[root]=sum0[root]=sum1[root]=0;
    New_Node(root,0,0,0);
    New_Node(ch[root][1],root,0,0);
    for(int i=0;i<n;i++) scanf("%d%d",&a[i],&status[i]);
    Build(Key_Value,0,n-1,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
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]][1]==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]][0]==r);
        else
        {
            int y=pre[r];
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    Push_Up(r);
    if(goal==0) root=r;
}
int Get_Kth(int r,int k)
{
    int t=size[ch[r][0]]+1;
    if(k==t) return r;
    else if(k<t) return Get_Kth(ch[r][0],k);
    else return Get_Kth(ch[r][1],k-t);
}
void Insert(int pos,int val,int _st)
{
    Splay(Get_Kth(root,pos+1),0);
    Splay(Get_Kth(root,pos+2),root);
    New_Node(Key_Value,ch[root][1],val,_st);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void erase(int r)
{
    if(!r)return;
    s[++tot2]=r;
    erase(ch[r][0]);
    erase(ch[r][1]);
}
void Delete(int pos)
{
    Splay(Get_Kth(root,pos),0);
    Splay(Get_Kth(root,pos+2),root);
    erase(Key_Value);
    Key_Value=0;
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void Change(int pos)
{
    Splay(Get_Kth(root,pos),0);
    Splay(Get_Kth(root,pos+2),root);
    st[Key_Value]^=1;
    Push_Up(Key_Value);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void Modify(int pos,int val)
{
    Splay(Get_Kth(root,pos),0);
    Splay(Get_Kth(root,pos+2),root);
    key[Key_Value]=val;
    Push_Up(Key_Value);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
int Query(int l,int r,int _st)
{
    Splay(Get_Kth(root,l),0);
    Splay(Get_Kth(root,r+2),root);
    int ans= _st==0? sum0[Key_Value]:sum1[Key_Value];
    if(ans==0) return -1;
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&q)==2)
    {
        Init();
        char op[20];
        int x,y,z;
        while(q--)
        {
            scanf("%s",op);
            if(op[0]=='Q')
            {
                scanf("%d%d%d",&x,&y,&z);
                printf("%d\n",Query(x,y,z));
            }
            else if(op[0]=='I')
            {
                scanf("%d%d%d",&x,&y,&z);
                Insert(x,y,z);
            }
            else if(op[0]=='D')
            {
                scanf("%d",&x);
                Delete(x);
            }
            else if(op[0]=='R')
            {
                scanf("%d",&x);
                Change(x);
            }
            else
            {
                scanf("%d%d",&x,&y);
                Modify(x,y);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值