版本比较
题解:P17751 「RSOI-2D」星光 / star
正文
增加 22 行删除 21 行
6 行未改动78910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868755 行未改动6 行未改动有 $30$ 颗星星,每颗亮 / 灭对应二进制位 $1/0$。每个小朋友喜欢一个长度为 $30$ 的 $01$ 串,互不相同。给定两个串 $k,r$。两个小朋友 $u,v$ 是好朋友,当且仅当:1. $\text{popcount}(u \& r)=\text{popcount}(v \&r)$2. 以下至少一个成立:- $\text{popcount}(u\oplus v)$ 为奇数;- $u\oplus v=k。$动态加删小朋友,每次询问是否存在至少三人两两为友。$q≤2×10^5$.## 主要思路### 1. 条件一的等价转化记 $c(x)=\text{popcount}(x\&r)\in [0,30]$。条件一即 $c(u)=c(v)。$### 2. 条件二的分析**引理 1**:若 $u\oplus v=k$,则 $u,v$ 的 $\text{popcount}$ 奇偶性相同当且仅当 $\text{popcount}(k)$ 为偶数。:::info[证明]因为 $\text{popcount}(u\oplus v)$ 的奇偶性 $\equiv \text{popcount}(u)+\text{popcount}(v) (\bmod 2)$。:::**引理 2**:三人两两为友,必然恰有一对差为 $k$,另外两对靠 "距离奇数" 相连。:::info[证明]- 不可能三对都靠距离奇数:若 $a\oplus b$, $b\oplus c$ 的 $\text{popcount}$ 都是奇数,则 $a\oplus c=(a\oplus b)\oplus (b\oplus c)$ 的 $\text{popcount}$ 奇偶性为奇数 + 奇数 = 偶数,矛盾。- 不可能两对差为 k:若 $a\oplus b=k,b\oplus c=k$,则 $a=c$,矛盾。:::**引理 3**:设 $a\oplus b=k$,$c$ 与 $a$ 距离奇数。则 $c$ 与 $b$ 距离奇偶性 $=$ 奇数 $\oplus$ $\text{popcount(k)}$ 奇偶性。要它也为奇数,必须 $\text{popcount(k)}$ 为偶数。**推论**:若 $\text{popcount(k)}$ 为奇数,答案恒为 `NO`。**进一步化简**设 $\text{popcount}(k)$ 为偶数。三人结构为:$a \oplus b = k$,$c$ 与 $a,b$ 距离均为奇数,且 $c(a) = c(b) = c(c)$。**差为 $k$ 的一对必须同组**设 $a\oplus b = k$,要求 $c(a) = c(b)$,即 $\text{popcount}(a \& r) = \text{popcount}((a \oplus k) \& r).$令 $y = k \& r$,$m = \text{popcount}(y)$。上式等价于 $m = 2 \cdot \text{popcount}((a \& r) \& y)$,即:- $m$ 必须为偶数;- 且 $a \& r$ 与 $y$ 的交集大小为 $m/2$。推论:若 $m = \text{popcount}(k \& r)$ 为奇数,则不存在同组且差为 $k$ 的一对,答案恒为 `NO`。**距离奇数 $\Leftrightarrow$ popcount 奇偶性不同**$\text{popcount}(c \oplus a) \equiv \text{popcount}(c) + \text{popcount}(a) \pmod 2$。所以“距离奇数”等价于 $c, a$ 的 popcount 奇偶性不同。由**引理 1**,差为 $k$ 的一对 $a, b$ 奇偶性相同(因为 $\text{popcount}(k)$ 偶数)。因此只需第三人 $c$ 与它们奇偶性不同即可。## 算法按 $c(x) = \text{popcount}(x \& r)$ 分成 31 个组。每组维护:- 哈希集合 $S_c$:组内所有元素;- $\text{even}_c, \text{odd}_c$:偶 / 奇 $\text{popcount}$ 元素个数;- $\text{cnt}_c$:组内差为 $k$ 的无序对数(每对只计一次)。**插入 x**:若 $x \oplus k \in S_c$,则 $\text{cnt}_c $ 增加一;然后插入 $x$,更新奇偶计数。**删除 x**:先删除 $x$,更新奇偶计数;若 $x \oplus k \in S_c$(删除后仍在),则 $\text{cnt}_c$ 减小1。维护全局同时满足1. $\text{cnt}_c > 0$2. $\text{even}_c > 0$3. $\text{odd}_c > 0$的组数$\text{ans}$,每次事件后输出 `YES` 当且仅当 $\text{ans} > 0$。复杂度 $O(q)$,空间 $O(q)$。## 代码55 行未改动6 行未改动7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878855 行未改动6 行未改动有 $30$ 颗星星,每颗亮 / 灭对应二进制位 $1/0$。每个小朋友喜欢一个长度为 $30$ 的 $01$ 串,互不相同。给定两个串 $k,r$。两个小朋友 $u,v$ 是好朋友,当且仅当:1. $\text{popcount}(u \operatorname{and} r)=\text{popcount}(v \operatorname{and} r)$2. 以下至少一个成立:- $\text{popcount}(u \operatorname{xor} v)$ 为奇数;- $u\operatorname{xor} v=k。$动态加删小朋友,每次询问是否存在至少三人两两为友。$q≤2×10^5$.## 主要思路### 1. 条件一的等价转化记 $c(x)=\text{popcount}(x\operatorname{and}r)\in [0,30]$。条件一即 $c(u)=c(v)。$### 2. 条件二的分析**引理 1**:若 $u\operatorname{xor} v=k$,则 $u,v$ 的 $\text{popcount}$ 奇偶性相同当且仅当 $\text{popcount}(k)$ 为偶数。:::info[证明]因为 $\text{popcount}(u\operatorname{xor} v)$ 的奇偶性 $\equiv \text{popcount}(u)+\text{popcount}(v) (\bmod 2)$。:::**引理 2**:三人两两为友,必然恰有一对差为 $k$,另外两对靠 "距离奇数" 相连。:::info[证明]- 不可能三对都靠距离奇数:若 $a\operatorname{xor} b$, $b\operatorname{xor} c$ 的 $\text{popcount}$ 都是奇数,则 $a\operatorname{xor} c=(a\operatorname{xor} b)\operatorname{xor} (b\operatorname{xor} c)$ 的 $\text{popcount}$ 奇偶性为奇数 + 奇数 = 偶数,矛盾。- 不可能两对差为 k:若 $a\operatorname{xor} b=k,b\operatorname{xor} c=k$,则 $a=c$,矛盾。:::**引理 3**:设 $a\operatorname{xor} b=k$,$c$ 与 $a$ 距离奇数。则 $c$ 与 $b$ 距离奇偶性 $=$ 奇数 $\operatorname{xor}$ $\text{popcount(k)}$ 奇偶性。要它也为奇数,必须 $\text{popcount(k)}$ 为偶数。**推论**:若 $\text{popcount(k)}$ 为奇数,答案恒为 `NO`。**进一步化简**设 $\text{popcount}(k)$ 为偶数。三人结构为:$a \operatorname{xor} b = k$,$c$ 与 $a,b$ 距离均为奇数,且 $c(a) = c(b) = c(c)$。**差为 $k$ 的一对必须同组**设 $a\operatorname{xor} b = k$,要求 $c(a) = c(b)$,即 $\text{popcount}(a \operatorname{and} r) = \text{popcount}((a \operatorname{xor} k) \operatorname{and} r).$令 $y = k \operatorname{and} r$,$m = \text{popcount}(y)$。上式等价于 $m = 2 \cdot \text{popcount}((a \operatorname{and} r) \operatorname{and} y)$,即:- $m$ 必须为偶数;- 且 $a \operatorname{and} r$ 与 $y$ 的交集大小为 $m/2$。推论:若 $m = \text{popcount}(k \operatorname{and} r)$ 为奇数,则不存在同组且差为 $k$ 的一对,答案恒为 `NO`。**距离奇数 $\Leftrightarrow$ popcount 奇偶性不同**$\text{popcount}(c \operatorname{xor} a) \equiv \text{popcount}(c) + \text{popcount}(a) \pmod 2$。所以“距离奇数”等价于 $c, a$ 的 popcount 奇偶性不同。由**引理 1**,差为 $k$ 的一对 $a, b$ 奇偶性相同(因为 $\text{popcount}(k)$ 偶数)。因此只需第三人 $c$ 与它们奇偶性不同即可。## 算法按 $c(x) = \text{popcount}(x \operatorname{and} r)$ 分成 31 个组。每组维护:- 哈希集合 $S_c$:组内所有元素;- $\text{even}_c, \text{odd}_c$:偶 / 奇 $\text{popcount}$ 元素个数;- $\text{cnt}_c$:组内差为 $k$ 的无序对数(每对只计一次)。**插入 x**:若 $x \operatorname{xor} k \in S_c$,则 $\text{cnt}_c$ 增加 $1$;然后插入 $x$,更新奇偶计数。**删除 x**:先删除 $x$,更新奇偶计数;若 $x \operatorname{xor} k \in S_c$(删除后仍在),则 $\text{cnt}_c$ 减小 $1$。维护全局同时满足1. $\text{cnt}_c > 0$2. $\text{even}_c > 0$3. $\text{odd}_c > 0$的组数 $\text{ans}$,每次事件后输出 `YES` 当且仅当 $\text{ans} > 0$。复杂度 $O(q)$,空间 $O(q)$。## 代码55 行未改动