题目链接:
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 41
∙ PUSH x: put x on the top of the stack, x must be 0 or 1.
∙ POP: throw the element which is on the top of the stack.
Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:
∙REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
∙QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do NAND operation one by one from left to right, i.e. If atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop−1 nand ... nand a1. Note that the Stack will notchange after QUERY operation. Specially, if the Stack is empty now,you need to print ”Invalid.”(without quotes).
By the way, NAND is a basic binary operation:
∙ 0 nand 0 = 1
∙ 0 nand 1 = 1
∙ 1 nand 0 = 1
∙ 1 nand 1 = 0
Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.
For each test case, the first line contains only one integers N (2≤N≤200000), indicating the number of operations.
In the following N lines, the i-th line contains one of these operations below:
∙ PUSH x (x must be 0 or 1)
∙ POP
∙ REVERSE
∙ QUERY
It is guaranteed that the current stack will not be empty while doing POP operation.
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+4;
const int maxn=2e5+20;
const double eps=1e-12; int n,m,a[2*maxn];
char s[20];
deque<int>qu;
int flag,l,r;
void POP()
{
if(flag)
{
if(a[r]==0)qu.pop_back();
r--;
}
else
{
if(a[l]==0)qu.pop_front();
l++;
}
}
void PUSH(int x)
{
if(flag)
{
a[++r]=x;
if(x==0)qu.push_back(r);
}
else
{
a[--l]=x;
if(x==0)qu.push_front(l);
}
}
void Rev(){flag^=1;}
void query()
{
if(qu.empty())
{
if(r<l){printf("Invalid.\n");return ;}
int num=r-l+1;
if(num&1)printf("1\n");
else printf("0\n");
}
else
{
if(flag)
{
int fr=qu.front();
int num=fr-l;
if(num&1)
{
if(fr==r)printf("1\n");
else printf("0\n");
}
else
{
if(fr==r)printf("0\n");
else printf("1\n");
}
}
else
{
int fr=qu.back();
int num=r-fr;
if(num&1)
{
if(fr==l)printf("1\n");
else printf("0\n");
}
else
{
if(fr==l)printf("0\n");
else printf("1\n");
}
}
}
return ;
}
inline void Init()
{
flag=1;l=N;r=N-1;
while(!qu.empty())qu.pop_back();
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
Init();
printf("Case #%d:\n",++Case);
read(n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]=='P')
{
if(s[1]=='U')
{
int x;
scanf("%d",&x);
PUSH(x);
}
else POP();
}
else if(s[0]=='R')Rev();
else query();
}
}
return 0;
}