Address https://www.acwing.com/problem/content/241/
Small A and B are playing a little game.
First, a small S A write a sequence composed of 0 and 1, the length is N.
Then, the small B presents a problem to small M A.
In each question, the small number of two designated B l and r, answer A small S [l ~ r] has an odd number or an even number.
B found a small little wit A likely lying.
For example, small A had answered the S [1 ~ 3] has an odd number 1, S [4 ~ 6] has an even number of 1, and now answered S [1 ~ 6] has an even number of 1, clearly this is self contradictory.
Please check this help small B M answer, noting that small can be determined after lying A certain number of at least answer.
I.e., obtain a minimum k, 01 such that the first sequence S 1 ~ k satisfies one answered, but does not satisfy 1 ~ k + 1 th answer.
Input Format
The first row contains an integer N, 01 represents the sequence length.
The second row contains an integer M, it represents the number of questions.
Next M rows, each row contains a set of questions and answers: two integers l and r, and an answer "even" or "odd", to describe the S [l ~ r] has an odd number or an even number.
Output Format
Output a integer k, 01 represents a sequence of 1 ~ k satisfies a first answer, but does not satisfy 1 ~ k + 1 th answer, if the answer satisfies all 01 sequences, the total output number of problems.
Range Data N ≤ 10 ^. 9 , M ≤ 10000 Input Sample: 10 . 5 . 1 2 the even 3 . 4 ODD . 5 . 6 the even . 1 . 6 the even . 7 10 ODD Output Sample: 3
Solution 1:
Weighted disjoint-set
There are two difficulties in this topic
1 with a large range of data 10 ^ 9
2 ask how the content is transformed into disjoint-set
Problem Solution 1 is to use a discrete mapping different data to 1234. . . Since only 10,000 ask, ask each provide two data so long as the data range of 2 to 10000 *
2 is a solution to the problem within the scope of the prefix and if r is 1 and l providing an odd number or an even number and the prefix is calculated sum [r] - sum [l-1]
In addition, because an even number Save Save the even odd odd even number are only two different types of the last odd and even an effect that the difference in appearance is odd
So in fact, is disjoint-set parity prefix and each element
Increasing array of all weighted elements are prefix and incorporated into an ancestor, d is whether [x] x weighted array element to be recorded is the same as the parity of the root
When the time to get a new inquiry if two elements have been merged under a common ancestor, then you can get based on their similarities and differences and the similarities and differences of their ancestors, their similarities and differences and come to judge whether the input is the same if not conflict, to return
code show as below
1 #include <iostream> 2 #include <string> 3 #include <unordered_map> 4 5 using namespace std; 6 7 const int MAX_M = 20010; 8 9 int N, M; 10 int n, m; 11 12 int fa[MAX_M]; 13 int d[MAX_M]; 14 15 int idx = 0; 16 17 unordered_map<int, int> S; 18 19 20 //离散化 21 int get(int x) { 22 if (S.count(x) == 0) S[x] = ++idx; 23 return S[x]; 24 } 25 26 void init() 27 { 28 for (int i = 0; i < MAX_M; i++) { 29 fa[i] = i; 30 } 31 } 32 33 int find(int x) { 34 if (fa[x] != x) { 35 int root = find(fa[x]); 36 d[x] += d[fa[x]]%2; 37 fa[x] = root; 38 } 39 40 return fa[x]; 41 } 42 43 44 int main() 45 { 46 47 cin >> n >> m; 48 int res = m; 49 init(); 50 for (int i = 1; i <= m; i++) { 51 is int A, B; String S; 52 is CIN >> A >> B >> S; 53 is A = GET (A - . 1 ); B = GET (B); 54 is int PA = Find (A), Pb = Find (B); 55 56 is iF (PA == Pb) { 57 is // Check whether the previous two information 58 iF (S == " the even " ) 59 { 60 // both have the same parity 61 is iF ((D [A] D + [B])% 2 ! = 0 ) { 62 // contradictory 63 is RES = I - . 1 ; 64 BREAK ; 65 } 66 } 67 the else IF (S == " ODD " ) { 68 // two different parity 69 IF ((D [A] + D [ B])% 2 ! = . 1 ) { 70 // contradictory 71 is RES = I - . 1 ; 72 BREAK ; 73 is } 74 } 75 else { 76 cout << s << endl; 77 cout << "error" << endl; 78 break; 79 } 80 } 81 else { 82 //pa != pb 83 //合并 84 fa[pa] = pb; 85 int add = 0; 86 if (s == "odd") add = 1; 87 d[pa] = (d[a] + d[b] + add)%2; 88 } 89 90 91 } 92 cout << res << endl; 93 94 95 96 return 0; 97 }