HDU-5929 Basic Data Structure

来源:互联网 发布:托业网络课程 编辑:程序博客网 时间:2024/05/22 01:08

Basic Data Structure

原题链接

Problem Description

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,⋯,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 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.

Input

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

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.

Output

For 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.

思路:
根据运算规则,注意到只要找到距离栈底最近的0的位置,然后0前面的所有的都会被直接转换成1,求后面1的个数就好了。
模拟栈。

#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdlib>#include <vector>#include <cstdio>#include <bitset>#include <string>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <list>#include <map>#include <set>using namespace std;const int maxn=100000+110;const int mod=1e9+7;const int INF=0x7fffff;int  num[2*200000+100];int index_0[2*200000+100];int main(){    int T;    scanf ("%d",&T);    int Case=0;    memset(num,0,sizeof(num));    while(T--){        int n;        memset(num,0,sizeof(num));        int l=200000,r=200000;        int l_0=200000,r_0=200000;        int flag=0;        scanf("%d",&n);        printf("Case #%d:\n",++Case);        for(int i=0;i<n;i++){            char s[10];            scanf ("%s",s);//push,pop,reverse ,QUERY            if(s[2]=='S'){                int x;                scanf("%d",&x);                if(flag==0){                    num[++r]=x;//(l,r]                    if(x==0){                        if(r_0==l_0){                            r_0=l_0=200000;                            index_0[l_0]=r;                            index_0[r_0]=r;                            l_0--;//(,]                        }else{                            index_0[++r_0]=r;                        }                    }                }else{                    num[l--]=x;                    if(x==0){                        if(r_0==l_0){                            r_0=l_0=200000;                            index_0[l_0]=l+1;                            index_0[r_0]=l+1;                            l_0--;//(]                        }else{                            index_0[l_0--]=l+1;                        }                    }                }            }else if(s[2]=='P'){                if(flag==0){                    if(num[r]==0){                        index_0[r_0--]=0;                        r--;                    }else{                        r--;                    }                }else{                    if(num[l+1]==0){                        index_0[++l_0]=0;                        l++;                    }else{                        l++;                    }                }            }else if(s[2]=='V'){                if(flag==0){                    flag=1;                }else{                    flag=0;                }            }else{                if(r==l){                    printf("Invalid.\n");                    continue;                }                if(flag==0){                    if(r_0==l_0){                        int cnt=r-l;                        if(cnt&1){                            printf("1\n");                        }else{                            printf("0\n");                        }                    }else{                        if(index_0[l_0+1]==r){                            int cnt=r-l-1;                            if(cnt&1){                                printf("1\n");                            }else{                                printf("0\n");                            }                        }else{                            int cnt=index_0[l_0+1]-l;                            if(cnt&1){                                printf("1\n");                            }else{                                printf("0\n");                            }                        }                    }                }else{                    if(r_0==l_0){                        int cnt=r-l;                        if(cnt&1){                            printf("1\n");                        }else{                            printf("0\n");                        }                    }else{                        if(index_0[r_0]==l+1){                            int cnt=r-l-1;                            if(cnt&1){                                printf("1\n");                            }else{                                printf("0\n");                            }                        }else{                            int cnt=r-index_0[r_0]+1;                            if(cnt&1){                                printf("1\n");                            }else{                                printf("0\n");                            }                        }                    }                }            }        }    }    return 0;}