tjut 5929

<pre name="code" class="html">#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <queue>  
using namespace std;  
int a[450000];  
deque<int> q;  
int main()  
{  
    int T;  
    scanf("%d",&T);  
    int ca=1;  
    while(T--)  
    {  
        int h=200001;  
        int t=h-1;  
        int type=1;  
        int n;  
        scanf("%d",&n);  
        char op[10];  
        q.clear();  
        int cnt=0;  
        printf("Case #%d:\n",ca++);  
        for(int i=0; i<n; i++)  
        {  
            scanf("%s",op);  
            int x;  
            if(op[0]=='P'&&op[1]=='U')  
            {  
                scanf("%d",&x);  
                if(type)  
                {  
                    a[h]=x;  
                    if(!x)  
                    q.push_back(h);  
                    h++;  
                }  
                else  
                {  
                    a[t]=x;  
                    if(!x)  
                    q.push_front(t);  
                    t--;  
                }  
                cnt++;  
            }  
            else if(op[0]=='P'&&op[1]=='O')  
            {  
                if(cnt==0)  
                    continue;  
                if(type)  
                {  
                    if(a[h-1]==0)  
                        q.pop_back();  
                    h--;  
                }  
                else  
                {  
                    if(a[t+1]==0)  
                        q.pop_front();  
                    t++;  
                }  
                cnt--;  
            }  
            else if(op[0]=='Q')  
            {  
                int num=0;  
                if(cnt==0)  
                {  
                    cout<<"Invalid."<<endl;  
                }  
                else if(cnt==1)  
                {  
                    cout<<a[h-1]<<endl;  
                }  
                else  
                {  
                    if(type)  
                    {  
                        if(q.empty())  
                            num=cnt;  
                        else  
                        {  
                            num=q.front()==h-1?cnt-1:q.front()-t;  
                        }  
                    }  
                    else  
                    {  
                        if(q.empty())  
                            num=cnt;  
                        else  
                        {  
                            num=q.back()==t+1?cnt-1:h-q.back();  
                        }  
  
                    }  
                    if(num%2==0)  
                        cout<<"0"<<endl;  
                    else  
                        cout<<"1"<<endl;  
                }  
            }  
            else  
            {  
                type^=1;  
            }  
        }  
    }  
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值