POJ 1733 and query the Set offset Vector

Source: Internet
Author: User

The offset vector is also used.

A numerical string consisting of 0, 1 ~~, Now, you have some questions about whether the number of numbers from I to j is an odd or even number. He will tell you the answer, but the answer may be self-contradictory. Now, it is no conflict to have the first few answers.


If r [I] is set, it indicates the parity of the Number 1 from the first place to the second place. r [I] = 0 indicates that there are even numbers of 1, r [I] = 1 indicates that there is an odd number of 1.
If the I-th to j-TH is an even number of 1, r [I-1] = 1, r [j] = 1 or r [I-1] = 0, r [j] = 0 So r [I-1] ^ r [j] = 0
If it is an odd number of 1, r [I-1] = 1, r [j] = 0 or r [I-1] = 0, r [j] = 1 so r [I-1] ^ r [j] = 1


Then we can use and query the set, and use an array d [I] to represent the difference or value between r [I] and its ancestor.

If the endpoint of the two intervals has appeared before, you can determine whether the difference or value is equal to the previous one.

If it does not appear, merge it directly to update the difference or value.


[Cpp]
# Include <iostream>
# Include <vector>
# Include <map>
# Include <set>
# Include <queue>
# Include <stack>
# Include <algorithm>
# Include <cstdio>
# Include <cstdlib>
# Include <string>
# Include <cstring>
# Include <cmath>
# Define maxn22222
# Define MAXM 55555
# Define INF 100000000
Using namespace std;
Int n, m;
Int fa [MAXN];
Int l [MAXN], r [MAXN], c [MAXN];
Int x [MAXN], d [MAXN];
Int find (int x)
{
If (fa [x] = x) return x;
Int t = find (fa [x]);
D [x] ^ = d [fa [x];
Fa [x] = t;
Return t;
}
Bool join (int x, int y, int v)
{
Int fx = find (x );
Int fy = find (y );
If (fx = fy) return (d [x] ^ d [y]) = v );
Else
{
Fa [fx] = fy;
D [fx] = d [x] ^ d [y] ^ v;
Return 1;
}
}
Int bin (int low, int high, int v)
{
While (low <= high)
{
Int mid = (low + high)> 1;
If (x [mid] = v) return mid;
Else if (x [mid]> v) high = mid-1;
Else low = mid + 1;
}
Return-1;
}
Int main ()
{
Char s [15];
Scanf ("% d", & n, & m );
Int cnt = 0;
For (int I = 1; I <= m; I ++)
{
Scanf ("% d % s", & l [I], & r [I], s );
X [cnt ++] = l [I]-1;
X [cnt ++] = r [I];
If (s [0] = 'E') c [I] = 0;
Else c [I] = 1;
}
Sort (x, x + cnt );
Cnt = unique (x, x + cnt)-x;
For (int I = 1; I <MAXN; I ++) fa [I] = I, d [I] = 0;
Int fg = 0;
For (int I = 1; I <= m; I ++)
{
Int L = bin (0, cnt-1, l [I]-1) + 1;
Int R = bin (0, cnt-1, r [I]) + 1;
If (! Join (L, R, c [I])
{
Fg = 1;
Printf ("% d \ n", I-1 );
Break;
}
}
If (! Fg) printf ("% d \ n", m );
Return 0;
}

# Include <iostream>
# Include <vector>
# Include <map>
# Include <set>
# Include <queue>
# Include <stack>
# Include <algorithm>
# Include <cstdio>
# Include <cstdlib>
# Include <string>
# Include <cstring>
# Include <cmath>
# Define maxn22222
# Define MAXM 55555
# Define INF 100000000
Using namespace std;
Int n, m;
Int fa [MAXN];
Int l [MAXN], r [MAXN], c [MAXN];
Int x [MAXN], d [MAXN];
Int find (int x)
{
If (fa [x] = x) return x;
Int t = find (fa [x]);
D [x] ^ = d [fa [x];
Fa [x] = t;
Return t;
}
Bool join (int x, int y, int v)
{
Int fx = find (x );
Int fy = find (y );
If (fx = fy) return (d [x] ^ d [y]) = v );
Else
{
Fa [fx] = fy;
D [fx] = d [x] ^ d [y] ^ v;
Return 1;
}
}
Int bin (int low, int high, int v)
{
While (low <= high)
{
Int mid = (low + high)> 1;
If (x [mid] = v) return mid;
Else if (x [mid]> v) high = mid-1;
Else low = mid + 1;
}
Return-1;
}
Int main ()
{
Char s [15];
Scanf ("% d", & n, & m );
Int cnt = 0;
For (int I = 1; I <= m; I ++)
{
Scanf ("% d % s", & l [I], & r [I], s );
X [cnt ++] = l [I]-1;
X [cnt ++] = r [I];
If (s [0] = 'E') c [I] = 0;
Else c [I] = 1;
}
Sort (x, x + cnt );
Cnt = unique (x, x + cnt)-x;
For (int I = 1; I <MAXN; I ++) fa [I] = I, d [I] = 0;
Int fg = 0;
For (int I = 1; I <= m; I ++)
{
Int L = bin (0, cnt-1, l [I]-1) + 1;
Int R = bin (0, cnt-1, r [I]) + 1;
If (! Join (L, R, c [I])
{
Fg = 1;
Printf ("% d \ n", I-1 );
Break;
}
}
If (! Fg) printf ("% d \ n", m );
Return 0;
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.