Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack:

∙∙ 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,⋯,a1atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop−1atop−1 nand ... nand a1a1. Note that the Stack will not change 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. 

InputThe first line contains only one integer T (T≤20T≤20), which indicates the number of test cases.

For each test case, the first line contains only one integers N (2≤N≤2000002≤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.
OutputFor each test case, first output one line "Case #x:w, where x is the case number (starting from 1). Then several lines follow,  i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print " Invalid."(without quotes). (Please see the sample for more details.) 
Sample Input

2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY

Sample Output

Case #1:
1
1
Invalid.
Case #2:
0

Hint

In the first sample: during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l
(from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid. 给一个栈.有push,pop,query ,reverse这些操作,
对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;
∙∙ 0 nand 0 = 1 
∙∙ 0 nand 1 = 1 
∙∙ 1 nand 0 = 1 
∙∙ 1 nand 1 = 0  组队训练赛的时候这题我直接扔给队友写的
队友写的自闭了 不过最后还是出来了
自己赛后补题 补到自闭
我不适合写模拟 我写了很久 尽量的简化了 模拟过程
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 4e5 + ;
int t, n, a[maxn], L, R, cas = , x;
char s[];
multiset<int>st;
multiset<int>::iterator it;
int main() {
// FIN;
sf(t);
while(t--) {
st.clear();
L = 2e5, R = 2e5;
int flag = ;
sf(n);
printf("Case #%d:\n", cas++);
for (int i = ; i < n ; i++) {
scanf("%s", s);
if (s[] == 'Q') {
if (L == R) printf("Invalid.\n");
else if (st.empty()) printf("%d\n",(R-L)%);
else {
if (flag) {
//fuck((*st.begin()) - L);
printf("%d\n", (((*st.begin()) - L) + (*st.begin() != R - )) % );
} else {
it=st.end();
it--;
// fuck(L);
printf("%d\n", ((R - (*it))- + (*it != L)) % );
}
}
}
if (s[] == 'P' && s[] == 'U') {
sf(x);
if (flag) a[R++] = x;
else a[--L] = x;
if (!x) {
if (flag) st.insert(R - );
else st.insert(L);
}
}
if (s[] == 'P' && s[] == 'O') {
if (L == R) continue;
if (flag) {
if(!a[R - ]) st.erase(R - );
R--;
} else {
if (!a[L]) st.erase(L);
L++;
}
}
if (s[] == 'R') flag ^= ;
}
}
return ;
}

最新文章

  1. 浅谈 举家搬迁静态文件到CDN
  2. CSS笔记(十四)CSS3之动画
  3. BitmapData类介绍
  4. 直播开始:&#39;云榨汁机&#39;诞生记--聊聊JavaScript中的&#39;业务建模&#39;
  5. 随便写了一个DAO
  6. CentOs7&amp;zookeeper
  7. selenium 处理浏览器多窗口
  8. opencv保存选择图像中的区域(二)
  9. 【C#】获取本地Cookie的问题
  10. Java如何实现对Mysql数据库的行锁
  11. opengl学习笔记(二)
  12. 融云(找到“每个App都有沟通的需求”的细分市场)
  13. ASP.NET MVC 异步Excel数据选择导出
  14. 浅析PageRank算法
  15. CSS——z-index
  16. Java基础---泛型、集合框架工具类:collections和Arrays
  17. spring mvc 接收 put参数
  18. ElasticStack系列之十四 &amp; ElasticSearch5.x bulk update 中重复 id 性能骤降
  19. (二)SpringMVC控制器
  20. hbase 预分区

热门文章

  1. JS变量定义时连续赋值的坑!
  2. [Clr via C#读书笔记]Cp14字符字符串和文本处理
  3. java poi技术读取到数据库
  4. POJ 2287 田忌赛马 贪心算法
  5. Java Class Object
  6. DAY6敏捷冲刺
  7. 模拟Excel同一列相同值的单元格合并
  8. lintcode-138-子数组之和
  9. 【week2】四人小组项目(WBS、NABCD)
  10. 什么是RESTFUL协议?