AcWing 239. 奇偶游戏 并查集,拓展域

AcWing 239. 奇偶游戏

题意

有一个长度为 n n n 仅由0和1组成的序列。有 m m m 个问题和回答:在区间 [ l i , R i ] [l_i,R_i] [li,Ri] 内有奇数个或者偶数个 1 1 1

输出一个整数 k k k ,表示这个01序列满足 1 ∼ k 1\sim k 1k 个回答,但不满足 1 ∼ k + 1 1\sim k+1 1k+1 个回答。如果满足所有回答,则输出问题的总数量。

解法

维护 1 1 1 的数量的前缀和的奇偶性 s [ i ] s[i] s[i]

  • 如果区间 [ l , r ] [l,r] [l,r] 内有奇数个 1 1 1 ,则说明 s [ l − 1 ] s[l-1] s[l1] s [ r ] s[r] s[r] 的奇偶性不同,即 s [ l − 1 ] ⊕ s [ r ] = 1 s[l-1]\oplus s[r]=1 s[l1]s[r]=1
  • 如果区间 [ l , r ] [l,r] [l,r] 内有偶数个 1 1 1 ,则说明 s [ l − 1 ] s[l-1] s[l1] s [ r ] s[r] s[r] 的奇偶性相同,即 s [ l − 1 ] ⊕ s [ r ] = s[l-1]\oplus s[r]= s[l1]s[r]= 0
  • 先离散化区间的范围,即对 l i − 1 l_i-1 li1 r i r_i ri 离散化。
  • 如果点 x x x 和点 y y y 之间的关系是同奇偶,那么我们就 l i n k ( x , y ) , l i n k ( x + 2 ⋅ m , y + 2 ⋅ m ) link(x,y),link(x+2\cdot m,y+2\cdot m) link(x,y),link(x+2m,y+2m)
  • 如果点 x x x 和点 y y y 之间的关系是奇偶不同,那么我们就 l i n k ( x + 2 ⋅ m , y ) , l i n k ( x , y + 2 ⋅ m ) link(x+2\cdot m,y),link(x,y+2\cdot m) link(x+2m,y),link(x,y+2m)
  • 同时判断关系是否冲突。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (sign) x = -x;
    return true;
}
inline bool R(double &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (ch == '.') {
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
    c = nc();
    if (IOerror) {
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 1e5 + 5;
int n, m;
int a[maxn], b[maxn];
char op[maxn][10];
int fa[maxn];
int findroot(int x) { return x == fa[x] ? x : fa[x] = findroot(fa[x]); }
void link(int x, int y) { fa[findroot(x)] = findroot(y); }
int main() {
    R(n, m);
    rep(i, 1, maxn - 1) fa[i] = i;
    rep(i, 1, m) {
        R(a[i * 2 - 1], a[i * 2]);
        R(op[i] + 1);
        a[i * 2 - 1]--;
    }
    memcpy(b, a, sizeof(b));
    sort(b + 1, b + 1 + 2 * m);
    rep(i, 1, 2 * m) a[i] = lower_bound(b + 1, b + 1 + 2 * m, a[i]) - b;
    rep(i, 1, m) {
        int x = a[i * 2 - 1], y = a[i * 2];
        int x1 = findroot(x), x2 = findroot(x + m * 2 + 1);
        int y1 = findroot(y), y2 = findroot(y + m * 2 + 1);
        if (op[i][1] == 'o') {
            if (x1 == y1) return 0 * printf("%d\n", i - 1);
            link(x1, y2);
            link(x2, y1);
        } else {
            if (x1 == y2) return 0 * printf("%d\n", i - 1);
            link(x1, y1);
            link(x2, y2);
        }
    }
    W(m);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值