tyvj1252/poj1733 Chubby parity/parity game and check set

Source: Internet
Author: User
Tags int size
Description

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 Hi M, whether this subsequence contains even or odd number of ones. Your friend answers Your question and you can ask the 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 isn't being correct and you want to convict him of falsehood. Thus you has decided to write a program to help you on this matter. The program would receive a series of your questions together with the answers that you had received from your friend. The aim of this program was to find the first answer which was provably wrong, i.e. that there exists a sequence satisfying Answers to all the previous questions, but no such sequence satisfies this answer. Input

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length was less or equal to 1000000000. The second line, there are one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer-question:two integers (the position of the first and last digit in The chosen subsequence) and one word which is either even ' orodd ' (the answer, i.e. the parity of the number of ones in t He chosen subsequence, where even ' means an even number of ones andodd ' means an odd number). Output

There is only one line in output containing one integer x. Number x says that there exists a sequence of zeroes and ones s Atisfying 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 is the number of a ll the questions asked. Sample Input

10
5
1 2 Even
3 4 Odd
5 6 Even
1 6 Even
7 Odd Sample Output

3

Expand the domain and check the set.

If an even number of 1 appears in [A, b], then the parity of 1 for [0,a-1] and [0,b] is the same.
If an odd number of 1 appears in [A, b], the parity between [0,a-1] and [0,b] 1 is different.

This allows you to use and check the set to maintain:

Fa[i] represents [0,i] has an even number of 1,fa[i+len] representatives [0,i] have an odd number of 1.

If [A, b] has an even number of 1, then merge Fa[a-1] and Fa[b],fa[a-1+len] and Fa[b+len].
If [A, b] has an odd number of 1, then merge Fa[a-1] and Fa[b+len],fa[a-1] and Fa[b+len].

For each operation, ask in advance whether the corresponding set of the other answer is the same, to determine whether it is contradictory.

This problem needs discretization, because the query [a, b] to use is A-1, not discretized and then minus 1, because this time minus 1 actually may represent not a unit, there may be an interval, because you are discrete. All you have to do is a--it when you read it. Because of this I wa several times ...

Code:

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace
Std
const int size=1000010;

int fa[size];

int find (int x) {return fa[x]==x?x:fa[x]=find (fa[x]);}

int lsh[size]; struct input{int l,r,d;}
L[size];

int len,n;
        int solve () {for (int i=1;i<=n;i++) {int x=find (L[I].L);
        int X1=find (L[i].l+len);
        int Y=find (L[I].R);
        int Y1=find (L[i].r+len);
            if (l[i].d==0) {if (x==y1) return i-1;
            Fa[x]=y;
        Fa[x1]=y1;
            } else {if (x==y) return i-1;
            Fa[x]=y1;
        Fa[x1]=y;
}} return n;
    } int main () {scanf ("%d%d", &len,&n);
        for (int i=1;i<=n;i++) {scanf ("%d%d", &AMP;L[I].L,&AMP;L[I].R);
        l[i].l--;
        string S;
        cin>>s;
        if (s== "even") l[i].d=0;
        if (s== "odd") l[i].d=1;
        LSH[++LSH[0]]=L[I].L;LSH[++LSH[0]]=L[I].R;
    } sort (lsh+1,lsh+1+lsh[0]);

    Len=unique (lsh+1,lsh+1+lsh[0])-lsh-1;

    for (int i=0;i<=len*3;i++) fa[i]=i;
        for (int i=1;i<=n;i++) {l[i].l=lower_bound (LSH+1,LSH+1+LEN,L[I].L)-lsh+1;
    L[i].r=lower_bound (LSH+1,LSH+1+LEN,L[I].R)-lsh+1;

    } printf ("%d", solve ());
return 0;

 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.