题意:给你两个端点, 告诉你区间中,1的个数,是奇数还数偶数,求是否矛盾,输出第几句话最先发生矛盾,若无则输出m + 1.
解题思路:这个题RE了好多次,最后发现初始化函数写的不对,应该初始化MAXN,而不是输入进来的n,因为要离散化……。使用map进行离散化,然后设d = 0为偶数,d = 1为奇数。然后就是用dist数组储存此点到树根的区间的奇偶。然后向量法偏移。注意取余的方法。操作就像HDU3038
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.
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
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define MAX_N 5005
const int INF = 0x3f3f3f3f;
int parent[MAX_N];//父节点
int dist[MAX_N];
int top = 0;
map<int,int> mt;
inline void init(int n)
{
for(int i = 0 ; i < n ; i++)
{
parent[i] = i;
dist[i] = 0;
}
top = 0;
}
int find(int a)
{
if(parent[a] != a)
{
int t = parent[a];
parent[a] = find(parent[a]);
dist[a] = (dist[a] + dist[t]) % 2;
}
return parent[a];
}
void unite(int a, int b, int d)
{
int fa = find(a);
int fb = find(b);
if(fa == fb) return;
parent[fb] = fa;
dist[fb] = (dist[a] - dist[b] + d + 2) % 2;
}
bool same(int a, int b)
{
return find(a) == find(b);
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
{
init(MAX_N);
int a, b;
char op[20];
int d;
int flag = 0;
int ans = 0;
for(int i = 0 ; i < m ; i++)
{
scanf("%d%d%s", &a, &b, &op);
a--;
if(op[0] == 'e') d = 0;
else d = 1;
if(mt.find(a) == mt.end())
{
mt[a] = top++;
}
if(mt.find(b) == mt.end())
{
mt[b] = top++;
}
if(flag) continue;
if(same(mt[a], mt[b]))
{
if(dist[mt[b]] != (dist[mt[a]] + d + 2)% 2)
{
ans = i;
flag = 1;
}
}
else
{
unite(mt[a], mt[b], d);
}
if(!flag) ans = m;
}
cout<<ans<<endl;
mt.clear();
}
return 0;
}