POJ1733-离散化+带权并查集

来源:互联网 发布:哈利波特知乎 编辑:程序博客网 时间:2024/04/30 03:34

Parity game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8342 Accepted: 3248

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

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is 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 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).

Output

There is only one line in 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 Input

1051 2 even3 4 odd5 6 even1 6 even7 10 odd

Sample Output

3


题目大意: 给你一串长度为n的01串,给你m个操作,每次操作为判断子串[x,y]内1的个数为奇数还是偶数,求x表示前x个操作没有矛                    

                   盾,第x+1个操作与前面的相矛盾(除了全部都没有矛盾)

题目思路:一开始看到这个题没看懂题目意思,还是英语太渣了,后面看了其他大佬们的博客才真正知道题意,然后想了会并没有                     

                    想到用并查集怎么做,然后又去看别人的博客 写到一个模型才恍然大悟,[x,y]区间的1的个数其实可以表示为                    

                    sum(y)-sum(x-1),是不是很像树状数组啊?,其实是并查集,这样就可以联系并查集了,

                   区间x,y 就可以换成 x-1,y 了 ,比如 1 2 e  3 4 o 5 6 e 1 6 e 去过不转换的话 1 6 与 3 4 就没有联系了,转换后的话就是                     

                   0 2 e 2 4 o 4 6 e 0 6 e这样就可以用带权并查集做了,至于数据范围离散化下就ok了

AC代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<map>using namespace std;const int maxn = 10050;int pre[maxn],a[maxn],rk[maxn];struct st{    int l,r;    char q[10];}s[maxn];int n,m;int get(int x){    if(x!=pre[x]){        int tmp = pre[x];        pre[x] = get(pre[x]);        rk[x]=(rk[x]+rk[tmp])%2;    }    return pre[x];}int main(){       while(~scanf("%d%d",&n,&m)){          int cnt = 0;          for(int i=0;i<m;i++){             scanf("%d%d%s",&s[i].l,&s[i].r,s[i].q);             s[i].l--;             a[cnt++]=s[i].l,a[cnt++]=s[i].r;          }          // 离散化          sort(a,a+cnt);          map<int,int>mp;          cnt = unique(a,a+cnt)-a;          for(int i=0;i<cnt;i++){                pre[i]=i,rk[i]=0;                mp[a[i]]=i;          }          int ans = 0;          for(int i=0;i<m;i++){             int x = mp[s[i].l],y = mp[s[i].r];             int xx = get(x);             int yy = get(y);             if(xx!=yy){                pre[xx]=yy;                if(s[i].q[0]=='e')          //偶数边的权值为0                rk[xx]=(rk[x]+rk[y])%2;                else rk[xx]=(rk[x]+rk[y]+1)%2;    //奇数边的权值为1                ans++;             }             else {                if(rk[x]==rk[y]&&s[i].q[0]=='o')break;                    if(rk[x]!=rk[y]&&s[i].q[0]=='e')break;                ans++;             }          }          printf("%d\n",ans);       }    return 0;}






0 0