poj1733 (union search + discretization)

The main idea of ​​the title: There is a 0,1 string of length n, and m pieces of information are given. Each piece of information indicates that the number of 1s in the middle of the xth to yth characters is even or odd. If the k+ 1 is the first time it contradicts the previous words, output k;

Idea: If the number of 1s between x and y is an even number, then the parity of 1 in 1~x and 1~y is the same; if the number of 1s between x and y is an odd number, then 1~x and 1~y have the same parity. The parity of 1 in 1~y is the same. Join the same set with the same parity

eg: The data range is large, discretize first

#include<stdio.h>
#include<algorithm>
#define N 100006 
using namespace std;  
struct Node  
{  
    int u,v;  
    char str[10];  
}that[N];  
int a[N*2],n,q,cnt;  
int pre[N*2],r[N*2];  
int Bin(int x)  
{  
    int low=0,high=cnt-1,mid;  
    while(low<=high)  
    {  
        mid=(low+high)/2;  
        if(a[mid]==x) return mid;  
        if(a[mid]<x) low=mid+1;  
        else high=mid-1;  
    }  
    return 0;  
}  
int find(int x)  
{  
    if(x!=pre[x])  
    {  
        int f=pre[x];  
        pre[x]=find(pre[x]);  
        r[x]=r[x]^r[f];  
    }  
    return pre[x];  
}  
intmain ()  
{  
    while(scanf("%d",&n)!=EOF)  
    {  
        scanf("%d",&q);  
        cnt=0;  
        for(int i=0;i<q;i++)  
        {  
            scanf("%d%d%s",&que[i].u,&que[i].v,&que[i].str);  
            that[i].u -- ;  
            a [cnt ++] = que [i] .v; a [cnt ++] = que [i] .u;  
        }  
        sort(a,a+cnt);  
        cnt=unique(a,a+cnt)-a;  
        for(int i=0;i<cnt;i++) pre[i]=i,r[i]=0;  
        int ans=0;  
        for(int i=0;i<q;i++)  
        {  
            int u=Bin(que[i].u),v=Bin(que[i].v);  
            int ra=find(u),rb=find(v);  
            if(ra==rb)  
            {  
                if(r[u]==r[v]&&que[i].str[0]=='o') break;  
                if(r[u]!=r[v]&&que[i].str[0]=='e') break;  
                years ++ ;  
            }  
            else  
            {  
                if(que[i].str[0]=='o')  
                {  
                    for [ra] = rb;  
                    r [ra] = r [u] ^ r [v] ^ 1 ;  
                }  
                else  
                {  
                    for [ra] = rb;  
                    r [ra] = r [u] ^ r [v];  
                }  
                years ++ ;  
            }  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303171&siteId=291194637