hdu4441 Queue Sequence

hdu4441
题意就是给你一列数,有三种操作,第一个就是在第i个位置(从零开始)插入这个数列中还没出现的第一个正整数,然后在第n+1个负数前插入这个正整数的负数,第二个就是删除+i和-i,第三个就是查询+i和-i之间的数的和。

找还没出现的第一个正整数就用线段树维护好啦
插入负数的时候就..记录每个下正数的个数和负数的个数…虽然正数的个数能算出来吧..但是…感觉那样麻烦【捂脸】
又是ll…..

#include <cstdio>
#define N 110000
#define M 220000
struct node1{int num,l,r;}tree[4*N];
int T=0,root=0,n,tot,ch[M][2],size[M],fa[M],num1[M],num2[M],v[M],a[N],b[N];
long long sum[M];

void print(int p){
    if(ch[p][0]) print(ch[p][0]);
    printf("%d ",v[p]);
    if(ch[p][1]) print(ch[p][1]);
}
void print(){
    for(int i=1;i<=tot;i++) printf("%d v:%d l:%d r:%d f:%d num1:%d num2:%d sz:%d sum:%d\n",i,v[i],ch[i][0],ch[i][1],fa[i],num1[i],num2[1],size[i],sum[i]);
    print(root); printf("\n");
    for(int j=1;j<=n;j++) printf("%d ",a[j]);printf("\n");
    for(int j=1;j<=n;j++) printf("%d ",b[j]);printf("\n");
}

void build(int v,int l,int r){
    tree[v].l=l;tree[v].r=r;tree[v].num=0;
    if(l==r) return;
    int mid=l+r>>1;
    if(l<=mid) build(v<<1,l,mid);
    if(mid<r) build(v<<1|1,mid+1,r);
}
int query1(int v){      //查找第一个还没有用的数是几 
    tree[v].num++;
    if(tree[v].l==tree[v].r) return tree[v].l;
    if(tree[v<<1].num!=tree[v<<1].r-tree[v<<1].l+1) return query1(v<<1);
    return query1(v<<1|1);
}
void remove(int v,int x){
    tree[v].num--; 
    if(tree[v].l==tree[v].r) return;
    int mid=tree[v].l+tree[v].r>>1;
    if(x<=mid) remove(v<<1,x);
    if(mid<x) remove(v<<1|1,x);
}
void newnode(int x){
    v[++tot]=sum[tot]=x;ch[tot][0]=ch[tot][1]=num1[tot]=num2[tot]=fa[tot]=0;size[tot]=1;
    if(x>0) num1[tot]++,a[x]=tot;
    if(x<0) num2[tot]++,b[-x]=tot;
}
void update(int x){
    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    num1[x]=num1[ch[x][0]]+num1[ch[x][1]];
    num2[x]=num2[ch[x][0]]+num2[ch[x][1]];
    sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+v[x];
    if(v[x]>0) num1[x]++;
    if(v[x]<0) num2[x]++;
}
void rotate(int &x){
    int y=fa[x],z=fa[y],t=ch[y][0]==x;
    if(z) ch[z][ch[z][1]==y]=x;
    else root=x;
    fa[x]=z;fa[y]=x;fa[ch[x][t]]=y;
    ch[y][t^1]=ch[x][t];ch[x][t]=y;
    update(y);update(x);
} 
void splay(int x,int &rt){
    while(x!=rt){
        int y=fa[x];
        if(y==rt){rotate(x);return;}
        if(ch[y][0]==x^ch[fa[y]][0]==y) rotate(x);
        else rotate(y); rotate(x);
    }
}
void insert1(int p,int x,int y){        //找到第x个数,在其后面加上数值为y的数 
    if(size[ch[p][0]]+1==x){
        newnode(y);int p1=ch[p][1];
        if(p1){
            while(ch[p1][0]) p1=ch[p1][0];
            ch[p1][0]=tot;fa[tot]=p1;
        }else ch[p][1]=tot,fa[tot]=p;
        splay(tot,root);return;
    }if(x<=size[ch[p][0]]) insert1(ch[p][0],x,y);
    else insert1(ch[p][1],x-size[ch[p][0]]-1,y);
}
void insert2(int p,int x,int y){        //找到第x个负数,在其前面加上数值为y的数 
    int z=0;if(v[p]<=0) z=1;
    if(x==num2[ch[p][0]]+1 && z){
        newnode(y);int p1=ch[p][0];
        if(p1){
            while(ch[p1][1]) p1=ch[p1][1];
            ch[p1][1]=tot;fa[tot]=p1;
        }else ch[p][0]=tot,fa[tot]=p;
        splay(tot,root);return;
    }if(x<=num2[ch[p][0]]) insert2(ch[p][0],x,y);
    else insert2(ch[p][1],x-num2[ch[p][0]]-z,y);
}
void del(int x){
    splay(x,root);
    int pre=ch[x][0],succ=ch[x][1];
    while(ch[pre][1]) pre=ch[pre][1];
    while(ch[succ][0]) succ=ch[succ][0];
    splay(pre,root);splay(succ,ch[pre][1]);
    ch[succ][0]=0;update(succ);update(pre);
}
long long query(int x){ 
    splay(a[x],root);
    splay(b[x],ch[a[x]][1]); 
    return sum[ch[b[x]][0]];
}
int main(){
    freopen("hdu4441.in","r",stdin);
    while(scanf("%d",&n)>0){
        printf("Case #%d:\n",++T);
        build(1,1,n);tot=0;root=1;
        newnode(0);newnode(0);
        fa[2]=1;ch[1][1]=2;size[1]=2;
        for(int i=1;i<=n;i++){
            char str[10];int x;scanf("%s%d",str,&x);
            if(str[0]=='i'){
                int y=query1(1);
                insert1(root,x+1,y);
                insert2(root,num1[ch[root][0]]+1,-y);
            }else if(str[0]=='r'){
                del(a[x]);del(b[x]);
                remove(1,x);
            }else printf("%lld\n",query(x));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值