跳到正文

题解

题解:P17751 「RSOI-2D」星光 / star

StarForger约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年11月6日
收录
2026年11月6日
更新
2026年11月8日
篇幅
约 8 分钟
题目
P17751

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

P17751 「RSOI - 2D」星光

提示:本题解引理证明由 deepseek 辅助人类完成,保证人类做出主要贡献

题目大意

有 3030 颗星星,每颗亮 / 灭对应二进制位 1/01/0。每个小朋友喜欢一个长度为 3030 的 0101 串,互不相同。给定两个串 k,rk,r。

两个小朋友 u,vu,v 是好朋友,当且仅当:

  1. popcount(uand⁡r)=popcount(vand⁡r)\text{popcount}(u \operatorname{and} r)=\text{popcount}(v \operatorname{and} r)
  2. 以下至少一个成立:
    • popcount(uxor⁡v)\text{popcount}(u \operatorname{xor} v) 为奇数;
    • uxor⁡v=k。u\operatorname{xor} v=k。

动态加删小朋友,每次询问是否存在至少三人两两为友。

q≤2×105q≤2×10^5.

主要思路

1. 条件一的等价转化

记 c(x)=popcount(xand⁡r)∈[0,30]c(x)=\text{popcount}(x\operatorname{and}r)\in [0,30]。条件一即 c(u)=c(v)。c(u)=c(v)。

2. 条件二的分析

引理 1:若 uxor⁡v=ku\operatorname{xor} v=k,则 u,vu,v 的 popcount\text{popcount} 奇偶性相同当且仅当 popcount(k)\text{popcount}(k) 为偶数。

证明

因为 popcount(uxor⁡v)\text{popcount}(u\operatorname{xor} v) 的奇偶性 ≡popcount(u)+popcount(v)( mod 2)\equiv \text{popcount}(u)+\text{popcount}(v) (\bmod 2)。

引理 2:三人两两为友,必然恰有一对差为 kk,另外两对靠 “距离奇数” 相连。

证明
  • 不可能三对都靠距离奇数:若 axor⁡ba\operatorname{xor} b, bxor⁡cb\operatorname{xor} c 的 popcount\text{popcount} 都是奇数,则 axor⁡c=(axor⁡b)xor⁡(bxor⁡c)a\operatorname{xor} c=(a\operatorname{xor} b)\operatorname{xor} (b\operatorname{xor} c) 的 popcount\text{popcount} 奇偶性为奇数 + 奇数 = 偶数,矛盾。
  • 不可能两对差为 k:若 axor⁡b=k,bxor⁡c=ka\operatorname{xor} b=k,b\operatorname{xor} c=k,则 a=ca=c,矛盾。

引理 3:设 axor⁡b=ka\operatorname{xor} b=k,cc 与 aa 距离奇数。则 cc 与 bb 距离奇偶性 == 奇数 xor⁡\operatorname{xor} popcount(k)\text{popcount(k)} 奇偶性。要它也为奇数,必须 popcount(k)\text{popcount(k)} 为偶数。

推论:若 popcount(k)\text{popcount(k)} 为奇数,答案恒为 NO。

进一步化简

设 popcount(k)\text{popcount}(k) 为偶数。三人结构为:axor⁡b=ka \operatorname{xor} b = k,cc 与 a,ba,b 距离均为奇数,且 c(a)=c(b)=c(c)c(a) = c(b) = c(c)。

差为 kk 的一对必须同组

设 axor⁡b=ka\operatorname{xor} b = k,要求 c(a)=c(b)c(a) = c(b),即 popcount(aand⁡r)=popcount((axor⁡k)and⁡r).\text{popcount}(a \operatorname{and} r) = \text{popcount}((a \operatorname{xor} k) \operatorname{and} r).

令 y=kand⁡ry = k \operatorname{and} r,m=popcount(y)m = \text{popcount}(y)。上式等价于 m=2⋅popcount((aand⁡r)and⁡y)m = 2 \cdot \text{popcount}((a \operatorname{and} r) \operatorname{and} y),即:

  • mm 必须为偶数;
  • 且 aand⁡ra \operatorname{and} r 与 yy 的交集大小为 m/2m/2。

推论:若 m=popcount(kand⁡r)m = \text{popcount}(k \operatorname{and} r) 为奇数,则不存在同组且差为 kk 的一对,答案恒为 NO。

距离奇数 ⇔\Leftrightarrow popcount 奇偶性不同

popcount(cxor⁡a)≡popcount(c)+popcount(a)(mod2)\text{popcount}(c \operatorname{xor} a) \equiv \text{popcount}(c) + \text{popcount}(a) \pmod 2。所以“距离奇数”等价于 c,ac, a 的 popcount 奇偶性不同。

由引理 1,差为 kk 的一对 a,ba, b 奇偶性相同(因为 popcount(k)\text{popcount}(k) 偶数)。因此只需第三人 cc 与它们奇偶性不同即可。

算法

按 c(x)=popcount(xand⁡r)c(x) = \text{popcount}(x \operatorname{and} r) 分成 31 个组。每组维护:

  • 哈希集合 ScS_c:组内所有元素;
  • evenc,oddc\text{even}_c, \text{odd}_c:偶 / 奇 popcount\text{popcount} 元素个数;
  • cntc\text{cnt}_c:组内差为 kk 的无序对数(每对只计一次)。

插入 x:若 xxor⁡k∈Scx \operatorname{xor} k \in S_c,则 cntc\text{cnt}_c 增加 11;然后插入 xx,更新奇偶计数。

删除 x:先删除 xx,更新奇偶计数;若 xxor⁡k∈Scx \operatorname{xor} k \in S_c(删除后仍在),则 cntc\text{cnt}_c 减小 11。

维护全局同时满足

  1. cntc>0\text{cnt}_c > 0
  2. evenc>0\text{even}_c > 0
  3. oddc>0\text{odd}_c > 0

的组数 ans\text{ans},每次事件后输出 YES 当且仅当 ans>0\text{ans} > 0。

复杂度 O(q)O(q),空间 O(q)O(q)。

代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 31;
unordered_set<int> st[N];
int a[N], b[N], pa[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    string ks, rs;
    cin >> q >> ks >> rs;
    int K = 0, R = 0;
    for(char c : ks) K = K * 2 + (c - '0');
    for(char c : rs) R = R * 2 + (c - '0');
    if(__builtin_popcount(K) % 2 || __builtin_popcount(K & R) % 2){
        while(q--){
            char op; string x;
            cin >> op >> x;
            cout << "NO\n";
        }
        return 0;
    }
    int ans = 0;
    auto it = [&](int x){
        return pa[x] > 0 && a[x] > 0 && b[x] > 0;
    };
    while(q--){
        char op;
        string xs;
        cin >> op >> xs;
        int x = 0;
        for(char c : xs) x = x * 2 + (c - '0');
        int c = __builtin_popcount(x & R), p = __builtin_popcount(x) % 2;
        bool f1 = it(c);
        if(op == '+'){
            if(st[c].count(x ^ K)) pa[c]++;
            st[c].insert(x);
            if(p == 0) a[c]++;
            else b[c]++;
        }else{
            st[c].erase(x);
            if(p == 0) a[c]--;
            else b[c]--;
            if(st[c].count(x ^ K)) pa[c]--;
        }
        bool f2 = it(c);
        if(f1 && !f2) ans--;
        else if(!f1 && f2) ans++;
        cout << (ans > 0 ? "YES\n" : "NO\n");
    }
    return 0;
}
原文
存档评论2 条抓取于