Parity

来源:互联网 发布:ubuntu 安装 kde桌面 编辑:程序博客网 时间:2024/05/17 09:17

1003. Parity

Time limit: 2.0 second
Memory limit: 64 MB
Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on.
Your task is to guess the entire sequence of numbers. You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 109. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either “even” or “odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “even” means an even number of ones and “odd” means an odd number). The input is ended with a line containing −1.

Output

Each line of output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X + 1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

Sample

inputoutput
1051 2 even3 4 odd5 6 even1 6 even7 10 odd-1
3

/**并查集**//***    由于数据比较多,所以先离散化(hash还没学好~~这是别人说的,哈希函数也是别人的)    原数据经离散后,就没什么用了(不需要再去查找原值),他们就全部变成了下标了,之后用并查集来解决。        a b even ,说明从a位到b位有偶数个1,那从第1位到a位和从第1位到b位1的数量是同奇偶的;              谁奇谁偶无所谓,我们只需要认为a和b是朋友,那将他们的祖先进行合并;    a b odd  ,说明从第1位到a位和从第1位到b位1的数量是一奇一偶;              谁奇谁偶无所谓,我们只需要认为a和b是敌人,但是,是敌人就没办法合并= =!              那我们就给a和b设置一个朋友,a的敌人是a+div,b的敌人是b+div,则b和a+div是朋友,进行合并;a和b+div是朋友,进行合并;    如果,a和b是朋友,就将a+div和b+div合并.        在每一次合并之前,判断一下关系,以找出矛盾;    当给出a b even 时,    我们判断a和b+div是不是朋友或者b和a+div是不是朋友,判断一种就行,如果是,则出现矛盾(给出的是a b even);    同样,    给出a b odd时,判断a和b是不是朋友,是的话,就产生了矛盾.***/#include <iostream>#include <cstring>using namespace std;#define mod  10001#define maxn 200007#define div  100000int HASH[maxn];int father[maxn];int n;void HashMem(){memset(HASH,-1,sizeof(HASH));}/***hash初始化***/int Hash(int x)/**离散化**/{    int p=x%mod;    while(HASH[p]!=-1&&HASH[p]!=x)        p++;    HASH[p]=x;    return p;/**返回的是下标,也就是键值**/}void FindMem(){for(int i=0;i<maxn;i++) father[i]=i;}/**并查集数组father初始化**/int Find(int x)/**查**/{    int root=x;    while(root!=father[root])        root=father[root];    father[x]=root;    return root;}void Union(int x,int y)/**并**/{    int a=Find(x);    int b=Find(y);    if(a!=b)        father[a]=b;}void init(){    HashMem();    FindMem();}void read(){    cin>>n;    int i,j,k;    int ans=-1;    string s;    for(i=0;i<n;i++)    {        cin>>j>>k>>s;        j=Hash(j-1);        k=Hash(k);        if(s=="even")        {            if(Find(j)==Find(k+div))/**判断关系**/                break;            Union(j,k);            Union(j+div,k+div);        }        else        {            if(Find(j)==Find(k))/**判断关系**/                break;            Union(j+div,k);            Union(j,k+div);        }        //cout<<"OK"<<endl;    }    ans=i;    for(i++;i<n;i++)        cin>>j>>k>>s;    cout<<ans<<endl;}int main(){    int len;    while(cin>>len)    {        if(len==-1)            break;       init();       read();    }    return 0;}