Parity game 【POJ - 1733】【种类并查集+离散化】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/85094797

题目链接


  判断哪一个最先出现的区间不符合要求的,就是我们知道有这样的区间even:偶数;odd:奇数。然后,就是用向量的方法向下推即可。

  但是,这样会RE,一开始只看到了那个5000次的查询,所以当时就小了数组,然后发现是1e9的时候,就决定,离散化吧,然后就A了。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define efs 1e-7
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2e4 + 7;
int N, Q, ans, L, R, root[maxN], sum[maxN], lsan[maxN], cnt = 0, lis_len;
char op[10];
struct node
{
    int l, r, val;
    node(int a=0, int b=0, int c=0):l(a), r(b), val(c) {}
}a[maxN];
bool cmp(int e1, int e2) { return e1<e2; }
int fid(int x)
{
    if(x == root[x]) return x;
    int tmp = root[x];
    root[x] = fid(root[x]);
    sum[x] = (sum[tmp] + sum[x] + 2)%2;
    return root[x];
}
void init()
{
    ans = Q;    cnt = 0;
    for(int i=0; i<maxN; i++) root[i] = i;
    memset(sum, 0, sizeof(sum));
}
int main()
{
    while(scanf("%d%d", &N, &Q)!=EOF)
    {
        init();
        for(int i=1; i<=Q; i++)
        {
            scanf("%d%d%s", &L, &R, op);
            L--;
            a[i] = node(L, R, (op[0] == 'e'?0:1));
            lsan[++cnt] = L;
            lsan[++cnt] = R;
        }
        sort(lsan+1, lsan+1+cnt, cmp);
        lis_len = (int)((unique(lsan+1, lsan+1+cnt) - lsan - 1));
        for(int i=1; i<=Q; i++)
        {
            int tmp = a[i].val;
            int l = (int)(lower_bound(lsan+1, lsan+1+lis_len, a[i].l) - lsan - 1), r = (int)(lower_bound(lsan+1, lsan+1+lis_len, a[i].r) - lsan - 1);
            int u = fid(l), v = fid(r);
            if(u != v)
            {
                root[v] = u;
                sum[v] = (sum[l] - sum[r] + tmp + 2)%2;
            }
            else
            {
                if(((sum[r] - sum[l] + 2)%2!=tmp) && ans == Q) ans = i-1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/85094797
今日推荐