zoj 3765 Lights(伸展树)

利用伸展树实现对区间内灯的状态查询(gcd)及动态修改操作,包括插入、删除、切换状态和修改灯的值。在伸展树中特别处理状态为0的灯,使用数组记录不同状态灯的gcd,对于未存在的状态赋予特殊值-1。
摘要由CSDN通过智能技术生成

题意:最开始有N个灯,有Q个操作。操作有一下几种类型:①查询区间[L,R]中灯的状态为status的所有灯的gcd②在第i位后插入一个值为val,状态为status的灯③删除第i个灯④第i个灯是开的那么关掉,否则打开⑤修改第i个灯的值。

思路:用伸展树暴力点写就行了。用数组记录当前区间状态为0和1的gcd的值。对于0结点,这些值赋值为-1就行了,在gcd中修改一下,对-1进行处理就行了。剩下的都是些基本操作。。。


代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=300000+10;
int ch[maxn][2],pre[maxn],size[maxn],val[maxn],st[maxn];
int gcdst[maxn][2],root,tot;
int num[200010],fs[200010];
int gcd(int a,int b)
{
    if(a==-1) return b;
    if(b==-1) return a;
    int c;
    while(b)
    {
        c=a%b;
        a=b;
        b=c;
    }
    return a;
}
void NewNode(int &rt,int fa,int v,int state)
{
    rt=++tot;
    ch[rt][0]=ch[rt][1]=0;
    size[rt]=1;val[rt]=v;
    pre[rt]=fa;
    gcdst[rt][state]=v;gcdst[rt][state^1]=-1;
    st[rt]=state;
}
void PushUp(int rt)
{
    size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+1;
    gcdst[rt][0]=gcd(gcdst[ch[rt][0]][0],gcdst[ch[rt][1]][0]);
    gcdst[rt][1]=gcd(gcdst[ch[rt][0]][1],gcdst[ch[rt][1]][1]);
    gcdst[rt][st[rt]]=gcd(gcdst[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 RotateTo(int k,int goal)
{
    int rt=root;
    while(size[ch[rt][0]]!=k)
    {
        if(size[ch[rt][0]]>k)
            rt=ch[rt][0];
        else
        {
            k-=(size[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 ;
    int m=(l+r)>>1;
    NewNode(rt,fa,num[m],fs[m]);
    build(l,m-1,ch[rt][0],rt);
    build(m+1,r,ch[rt][1],rt);
    PushUp(rt);
}
int Query(int L,int R,int state)
{
    RotateTo(L-1,0);
    RotateTo(R+1,root);
    return gcdst[ch[ch[root][1]][0]][state];
}
void Insert(int pos,int v,int state)
{
    RotateTo(pos,0);
    if(ch[root][0]==0)
    {
        NewNode(ch[root][0],root,v,state);
        PushUp(root);
        return ;
    }
    int rc=ch[root][0];
    while(ch[rc][1])
        rc=ch[rc][1];
    Splay(rc,root);
    NewNode(ch[rc][1],rc,v,state);
    PushUp(rc);
    PushUp(root);
}
void Delete(int pos)
{
    RotateTo(pos,0);
    if(ch[root][0]==0)
    {
        root=ch[root][1];
        pre[root]=0;
        return ;
    }
    int rc=ch[root][0];
    while(ch[rc][1]) rc=ch[rc][1];
    Splay(rc,root);
    int rt=ch[root][1];
    ch[rc][1]=rt;
    pre[rt]=rc;
    root=rc;
    pre[rc]=0;
    PushUp(root);
}
void Changes(int pos)
{
    RotateTo(pos,0);
    st[root]^=1;
    PushUp(root);
}
void Modify(int pos,int v)
{
    RotateTo(pos,0);
    val[root]=v;
    PushUp(root);
}
void Init(int n)
{
    for(int i=0;i<n;++i)
        scanf("%d%d",&num[i],&fs[i]);
    ch[0][0]=ch[0][1]=0;
    pre[0]=size[0]=0;val[0]=-1;
    gcdst[0][0]=gcdst[0][1]=-1;
    root=tot=0;
    NewNode(root,0,-1,0);
    NewNode(ch[root][1],root,-1,0);
    build(0,n-1,ch[ch[root][1]][0],ch[root][1]);
    PushUp(ch[root][1]);
    PushUp(root);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,q;
    char str[5];
    while(~scanf("%d%d",&n,&q))
    {
        Init(n);
        while(q--)
        {
            scanf("%s",str);
            int L,R,x,s;
            if(str[0]=='Q')
            {
                scanf("%d%d%d",&L,&R,&s);
                printf("%d\n",Query(L,R,s));
            }
            else if(str[0]=='I')
            {
                scanf("%d%d%d",&L,&x,&s);
                Insert(L+1,x,s);
            }
            else if(str[0]=='D')
            {
                scanf("%d",&L);
                Delete(L);
            }
            else if(str[0]=='R')
            {
                scanf("%d",&L);
                Changes(L);
            }
            else
            {
                scanf("%d%d",&L,&x);
                Modify(L,x);
            }
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值