Idea: weighted and query set
Analysis:
1. Given n conditions, we need to find the first number that does not meet the conditions.
2 each condition gives the parity of 1 in the range [l, r]. Obviously, the number of 1 in the range [l, r] can be [0, r]-[0 L-1]
3. Using the concept of querying sets, rank [x] indicates the number of [x, father [x] from x to 1 of the following node, the parity can be expressed by 1 and 0.
4. Another difficulty is discretization. The general discretization step is sorting + deduplication, And Then binary can be used when searching.
Code:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 50010;struct Node{ int x; int y; int mark; };Node node[MAXN];int arr[MAXN] , pos;int father[MAXN] , rank[MAXN] , num;void init(){ sort(arr , arr+pos); num = unique(arr , arr+pos)-arr; memset(rank , 0 , sizeof(rank)); for(int i = 0 ; i <= num ; i++) father[i] = i;}int find(int x){ if(father[x] != x){ int fa = father[x]; father[x] = find(father[x]); rank[x] = (rank[x]+rank[fa])%2; } return father[x];}int search(int x){ int left = 0; int right = num-1; while(left <= right){ int mid = (left+right)>>1; if(arr[mid] == x) return mid; else if(arr[mid] < x) left = mid+1; else right = mid-1; }}int solve(int n){ for(int i = 0 ; i < n ; i++){ int x = search(node[i].x)+1; int y = search(node[i].y)+1; int fx = find(x); int fy = find(y); int val = node[i].mark; if(fx == fy){ if((rank[y]-rank[x]+2)%2 != val) return i; } else{ father[fx] = fy; rank[fx] = (val+rank[y]-rank[x]+2)%2; } } return n;}int main(){ int len , n; char str[10]; while(scanf("%d%d" , &len , &n) != EOF){ pos = 0; for(int i = 0 ; i < n ; i++){ scanf("%d %d %s" , &node[i].x , &node[i].y , str); node[i].x--; arr[pos++] = node[i].x; arr[pos++] = node[i].y; if(str[0] == 'e') node[i].mark = 0; else node[i].mark = 1; } init(); printf("%d\n" , solve(n)); } return 0;}