题目链接:http://poj.org/problem?id=1733
Time Limit: 1000MS Memory Limit: 65536K

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

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output

3

Problem solving report:

Description:给定一个由0和1组成长度为n的序列,现有m组判断,第x个到第y个数字组成的子序列中1的数量是奇数还是偶数。问m组判断中,从哪一组开始是错误的,若第x+1组是错误的则输出x,如果都正确则输出m。
Problem solving:使用并查集来维护。序列中1的数量的奇偶可以用异或来解决(0^1=1、0^0=0、1^1=0),偶数用0表示,奇数用1表示。还有就是要注意数据范围,数据太大,无法用数组保存,需要进行离散化处理。

Accepted Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
int f[MAXN << 1], dis[MAXN << 1], spt[MAXN << 1];
struct edge {
    int avt, bvt, tmp;
}p[MAXN];
int getf(int v) {
    if (f[v] != v) {
        int tmp = f[v];
        f[v] = getf(f[v]);
        dis[v] ^= dis[tmp];
    }
    return f[v];
}
bool merge(int u, int v, int w) {
    int t1 = getf(u);
    int t2 = getf(v);
    if (t1 != t2) {
        f[t2] = t1;
        dis[t2] = dis[u] ^ w ^ dis[v];
        return false;
    }
    return dis[u] ^ dis[v] != w;
}
int main() {
    char str[5];
    int n, m, cnt, ans;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%s", &p[i].avt, &p[i].bvt, str);
        spt[i << 1] = p[i].avt - 1;
        spt[i << 1 | 1] = p[i].bvt;
        if (str[0] != 'e')
            p[i].tmp = 1;
        else p[i].tmp = 0;
    }
    ans = m;
    sort(spt, spt + (m << 1));
    cnt = unique(spt, spt + (m << 1)) - spt;
    for (int i = 0; i < cnt; i++) {
        f[i] = i;
        dis[i] = 0;
    }
    for (int i = 0; i < m && ans >= m; i++) {
        p[i].avt = lower_bound(spt, spt + cnt, p[i].avt - 1) - spt;
        p[i].bvt = lower_bound(spt, spt + cnt, p[i].bvt) - spt;
        if (merge(p[i].avt, p[i].bvt, p[i].tmp))
            ans = i;
    }
    printf("%d\n", ans);
    return 0;
}