hdu 5818 Joint Stacks(线段树)

Joint Stacks
------------
Problem Description
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:

- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B

After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.


Input
There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.


Output
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.


Sample Input

4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge B A
pop B
pop B
pop B 
0



Sample Output

Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0

题意:
有A,B两个栈,三种操作
1.push一个元素进入栈中
2.pop某个栈的栈顶元素
3.按输入顺序合并两个栈

分析:
乍一看题目马上想到用栈或者链表去模拟,但是发现这样做不是很好实现,细节多,不好处理。
重新看一下题目,我们主要需要解决的是合并的问题,观察到合并操作其实就是将目前存在的所有元素归属到A或者B,何不用线段树去处理这个区间问题,再考虑其他两个操作,push就是将数组的后端加入一个元素,pop就是找到属于A或B的最后一个元素,线段树支持这样的操作,问题得到解决,复杂度 O(nlogn) .

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 200005
int n;
int a[4*maxn],b[4*maxn],tag[4*maxn];
int val[maxn];

void init(int l,int r,int k)
{
    a[k]=b[k]=0;
    tag[k]=0;
    if(l==r) return ;
    int mid=l+r>>1;
    init(l,mid,k<<1);
    init(mid+1,r,k<<1|1);
}

void pushdown(int k)
{
    if(!tag[k]) return ;
    if(tag[k]==1)
    {
        a[k<<1]=a[k<<1]+b[k<<1];
        b[k<<1]=0;
        tag[k<<1]=tag[k];
        a[k<<1|1]=a[k<<1|1]+b[k<<1|1];
        b[k<<1|1]=0;
        tag[k<<1|1]=tag[k];
    }
    else
    {
        b[k<<1]=a[k<<1]+b[k<<1];
        a[k<<1]=0;
        tag[k<<1]=tag[k];
        b[k<<1|1]=a[k<<1|1]+b[k<<1|1];
        a[k<<1|1]=0;
        tag[k<<1|1]=tag[k];
    }
    tag[k]=0;
}

void update(int add,int kind,int v,int d,int l,int r,int k)
{
    if(l==d&&r==d)
    {
        if(kind==1) a[k]+=add;
        else b[k]+=add;
        if(add==1) val[l]=v;
        return ;
    }
    pushdown(k);
    int mid=l+r>>1;
    if(d<=mid) update(add,kind,v,d,l,mid,k<<1);
    else update(add,kind,v,d,mid+1,r,k<<1|1);
    a[k]=a[k<<1]+a[k<<1|1];
    b[k]=b[k<<1]+b[k<<1|1];
}

int Find(int kind,int l,int r,int k)
{
    if(l==r) return l;
    pushdown(k);
    int mid=l+r>>1;
    if(kind==1)
    {
        if(a[k<<1|1]>=1) return Find(kind,mid+1,r,k<<1|1);
        else return Find(kind,l,mid,k<<1);
    }
    else
    {
        if(b[k<<1|1]>=1) return Find(kind,mid+1,r,k<<1|1);
        else return Find(kind,l,mid,k<<1);
    }
}

int main()
{
    int ca=1;
    while(scanf("%d",&n)!=EOF&&n)
    {
        init(1,n,1);
        int tot=1;
        printf("Case #%d:\n",ca++);
        for(int i=1; i<=n; i++)
        {
            char s[10];
            scanf("%s",s);
            if(!strcmp(s,"push"))
            {
                int x;
                scanf("%s%d",s,&x);
                val[tot]=x;
                if(s[0]=='A') update(1,1,x,tot,1,n,1);
                else update(1,2,x,tot,1,n,1);
                tot++;
            }
            else if(!strcmp(s,"pop"))
            {
                int t;
                scanf("%s",&s);
                if(s[0]=='A') t=1;
                else t=2;
                int pos=Find(t,1,n,1);
                printf("%d\n",val[pos]);
                update(-1,t,-1,pos,1,n,1);
            }
            else
            {
                int t;
                char s1[10];
                scanf("%s%s",s,s1);
                if(s[0]=='A') t=1;
                else t=2;
                tag[1]=t;
                if(t==1) a[1]=a[1]+b[1],b[1]=0;
                else b[1]=a[1]+b[1],a[1]=0;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值