even

The first line gives you a n (<= 10 ^ 10) indicates that a character string of length n 0,
the second line gives you a m (<= 5000) represented by m rows next input,
the next m input lines
x, y, represents the even intermediate x to the y-th character count is an even number,
x, y, x to intermediate oDD represents the y-th character count is an odd number,
if m the first sentence k + 1 is the first conflict with the previous case, the output K; otherwise, the output m

The Input the Sample
10 // string of length 10.
5 // 5 asked
1 2 even // even number of sections 1-2. 1
. 3. 4 ODD interval 3-4 // odd number. 1
5. 6 the even
. 1 the even. 6
. 7 the ODD 10
the Sample the Output
3 // 4th ask the previous three inquiries contradiction

sol: This problem with disjoint-set method to open a virtual point solution, similar to criminal gangs.

The extracted information is read: a number of parity section 1, section by section and FIG. With SUM [i] to i denotes the number of the front and, since every number is 0, SUM [i] is the number indicating how many bits before i 1.

If the interval [i, j] 1 is an even number, i.e., sum [j] -sum [i-1] of the value is even, then the sum [j] is consistent with the sum [i-1] of the parity, because odd - even-odd too, the even - even-even number too, will then be i, j in the same set. And i, j i of opposite parity to be set 'and j' in the same.

If the interval [i, j] is an odd number 1, i.e., sum [j] -sum [i-1] is an odd value, then the sum [j] Contrary to sum [i-1] of the parity, because odd - even-odd too, the even - odd odd too, will then be i, j in different sets. With opposite parity i i 'and j to be set in the same, opposite parity j and j' i should be set in the same.

In addition, the value of n given this question a lot, is 10 ^ 10, to record the father node of each node disjoint-set when we do, father need to open an array of 2 * 10 ^ 10 is so big, super memory space, when we find little value of m, within just 5000, even if the left and right endpoints of each interval are not the same, and only 10,000 points, opened the virtual point re-doubled, and this question we do not even disjoint-set edge, as long as know about the relationship between endpoints, whether in the same set, with the two end points is irrelevant what the specific value, so we can do a discrete operation. The left end of each interval is mapped to a smaller number.

Code is implemented as follows:

 1 #include <cstdio>  
 2 #include <cstdlib>  
 3 #include <cstring>  
 4 #include <algorithm>  
 5 #include <cmath>  
 6 #include <stack>  
 7 #include <queue>  
 8 #include <vector>  
 9 #include <string>  
10 #include <climits>  
11 #include <iostream>  
12 #define INF 0x3f3f3f3f  
13 #define ll long long 
14 #define dd double 
15 using namespace std;
16 int dad[20100],m,n,a[20010],t;
17 char st[20100];
18 struct node
19 {
20     int l,r,ty;
21 }que[10010];
22 int getf(int x)
23 {
24     return x==dad[x]?x:dad[x]=getf(dad[x]);
25 }
26 int main()
27 {
28     scanf("%d",&m);
29     Scanf ( " % D " , & n-);
 30      for ( int I = . 1 ; I <= n-; I ++ )
 31 is      {
 32          Scanf ( " % D% D% S " , & que [I] .L, & que [ I] .r, ST);
 33 is          que [I] .ty = (ST [ 0 ] == ' O ' ? . 1 : 0 ); // if it is odd TRUE, the even on FALSE 
34 is          A [++ T] que = [I] .L . 1 , // note the left margin 
35          A [++ T] = que [I] .r; // note the right border
36      }
 37 [      Sort (a + . 1 , a + T + . 1 ); // this is done discretized with 
38 is      m = UNIQUE (a + . 1 , a + T + . 1 ) and -A- . 1 ; // calculating a array of M species different numbers 
39      for ( int I = . 1 ; I <= 2 * m; I ++) // father point to open twice   
40           DAD [I] = I;
 41 is      for ( int I = . 1 ; I <= n-; I ++ ) // n-number is asked 
42 is      {
 43 is          int X = lower_bound (A + . 1, A + m + . 1 , que [I] .L . 1 ) - A;
 44 is          int Y = lower_bound (A + . 1 , A + m + . 1 , que [I] .r) - A;
 45          int XX = X + m; // find respective opposite point 
46 is          int YY = Y + m; // find respective opposite point 
47          IF (que [I] == .ty . 1 )   // If this record is TRUE, the said x, y parity is not the same two points of 
48          {
 49              IF (GETF (X) == GETF (Y)) // they are actually in the same set, indicating an error 
50              {
 51 is                  the printf ( " % D \ n- ", I- . 1 );
 52 is                  return  0 ;
 53 is              }
 54 is              DAD [GETF (x)] = GETF (YY); // if not previously exit, then x and yy (y antithesis) in a set of 
55              DAD [GETF (XX)] = GETF (Y); // ibid 
56 is          }
 57 is          the else    // Description of this record is FALSE, the parity of said x, y are the same two points 
58          {
 59              IF (GETF (X) GETF == (YY)) // they are actually in the same set, indicating an error 
60              {
 61 is                  the printf ( " % D \ n- " , I- . 1 );
 62 is                  return 0 ;
 63 is              }
 64              DAD [GETF (x)] = GETF (y); // if not previously exit, then x and y in a set of 
65              DAD [GETF (XX)] = GETF (YY);
 66          }
 67      }
 68      the printf ( " % D \ n- " , n-);
 69      return  0 ;
 70 }

 

Guess you like

Origin www.cnblogs.com/cutepota/p/12557997.html