P10144 [WC2024] 水镜
P10144 [WC2024] 水镜
给定长为 \(n\) 的序列 \(a_i\),称点对 \((l,r)\) 是合法的,当且仅当:
- \(1 \le l <r \le n\)。
- 存在 \(x \in \mathbb{R}^{+}\),使得存在序列 \(a_l'\sim a_r'\) 满足 \(a_i' \in \{a_i,x-a_i\}\),且 \(a_l' \sim a_r'\) 严格递增。
求合法点对的数目。
\(n \le 5\times 10^5\),\(1 \le a_i \le 10^{12}\),1 秒,512 MB。
\(O(n^32^n)\)
枚举 \((l,r)\) 后,考虑 \(2^n\) 枚举 \(a_i'=a_i\) 或 \(a_i'=x-a_i\)。由 \(a_i'\) 严格递增的条件列若干不等式得到 \(x\) 的范围。
具体地,设 \(a_i'=a_i\) 的 \(i\) 组成集合 \(S\),得到下列约束:
- 若 \(i \in S \land i+1 \in S\),有 \(a_i < a_{i+1}\)。
- 若 \(i\in S \land i+1 \notin S\),有 \(a_i<x-a_{i+1} \Rightarrow x>a_i+a_{i+1}\)。
- 若 \(i \notin S \land i+1 \in S\),有 \(x-a_i<a_{i+1} \Rightarrow x<a_i+a_{i+1}\)。
- 若 \(i \notin S \land i+1 \notin S\),有 \(x-a_i<x-a_{i+1} \Rightarrow a_i>a_{i+1}\)。
扫描 \(i=l\to r\) 对解集取交,若得到 \(\varnothing\) 则不合法。复杂度 \(O(2^n \times \text{poly}(n))\),期望得分 \(8\)。
\(O(n^4)\)
注意到解集端点值必然是某个 \(a_i+a_{i+1}\),从而有用的 \(x\) 只有 \(O(n)\) 种。
设 \(U=\{a_{i}+a_{i+1}\pm 0.5\mid i \in [1,n)\} \cup \{+\infty\}\),我们断言,只要检查 \(x \in U\) 即可确定 \((l,r)\) 的合法性。
枚举 \(x\) 后判定是简单贪心,只要扫描 \(i=l\to r\) 令 \(a_i'\) 取 \(> a'_{i-1}\) 的最小值。复杂度 \(O(n^4)\),期望得分 \(16 \sim 24\)。
\(O(n^3)\)
合法性满足区间包含单调性,即 \((l,r)\) 合法时 \([l',r'] \subseteq [l,r]\) 的 \((l',r')\) 必然也合法。
扫描 \(r\) 对 \(l\) 双指针即可,复杂度 \(O(n^3)\),期望得分 \(24\)。
\(O(n\log n)\)
考虑直接求 \(x\) 必须满足的范围。分讨邻位关系:
- 若 \(a_i\le a_{i+1}\) 则 \(i \in S \lor i+1 \in S\)。
- 若 \(a_i+a_{i+1} \le x\),必有 \(i \in S \Leftrightarrow a_i'=a_i\)。
- 若 \(a_i+a_{i+1} \ge x\),必有 \(i+1 \in S \Leftrightarrow a_{i+1}'=a_{i+1}\)。
- 若 \(a_i \ge a_{i+1}\) 则 \(i\notin S \lor i+1 \notin S\)。
- 若 \(a_i+a_{i+1} \le x\),必有 \(i+1 \notin S \Leftrightarrow a_{i+1}'=x-a_{i+1}\)。
- 若 \(a_i+a_{i+1} \ge x\),必有 \(i \in S \Leftrightarrow a_{i}'=x-a_{i}\)。
逻辑关系是 \((P \lor Q) \land (\lnot P\lor Q)\) 为真,等价于命题 \(Q\) 为真。
对整段序列,约束命题可以写作
其中每个括号都可以等价替换为若干约束 \(Q\)。只要所有 \(Q\) 无矛盾,\(x\) 必然合法。
现在取 \((i-1,i)\) 和 \((i,i+1)\) 推导约束矛盾的条件:
-
\(a_i=a_{i+1} \land x=2a_i\)。
-
左边强制 \(a_i'=a_i\),右边强制 \(a_i'=x-a_i\)。条件为
\[a_{i-1} \le a_i \ge a_{i+1} \land a_{i-1}+a_i \ge x \land a_i+a_{i+1} \ge x \]容易根据条件排除掉 \(x=2a_i\) 的选择,这样必然无解。
改写为 \(a_i \ge \max(a_{i-1},a_{i+1})\) 且 \(x \le a_i+\min(a_{i-1},a_{i+1})\),从而 \(x\) 的下界约束
\[x>a_i+\min(a_{i-1},a_{i+1}),\quad a_i\ge \max(a_{i-1},a_{i+1}) \] -
左边强制 \(a_i'=x-a_i\),右边强制 \(a_i'=a_i\),条件为
\[a_{i-1} \ge a_i \le a_{i+1} \land a_{i-1}+a_i \le x\land a_i+a_{i+1} \le x \]同理可得 \(x\) 的上界约束
\[x<a_i+\max(a_{i-1},a_{i+1}),\quad a_i \le \min(a_{i-1},a_{i+1}) \]
设数组 \(l_i,r_i\) 表示位置 \(i\) 对 \(x\) 的上下界约束,不存在设为 \(0\) 或 \(+\infty\)。从而 \(x\) 的范围是
注意到 \(x \in \mathbb{R}\) 而 \(2a_i\) 只去掉有限个值,可以忽略。合法条件就转化成
对 \(l_i\) 和 \(r_i\) 建立两个 ST 表后双指针即可,复杂度 \(O(n \log n)\)。期望得分 \(100\)。
\(O(n)\)
使用线性 RMQ。更简单地,用双栈模拟队列维护不删除双指针即可。
复杂度 \(O(n)\)。期望得分 \(100\)。
const int N = 5e5 + 5;
const i64 inf = 1e18;
int n; i64 a[N], x[N], y[N];
template <class F>
struct stqueue {
vector<pair<i64, i64>> A, B;
F add{};
void push(i64 x) {
B.pb(x, B.empty() ? x : add(B.back().se, x));
}
void pop() {
if (A.empty()) while (!B.empty()) {
i64 x = B.back().fi; B.pop_back();
A.pb(x, A.empty() ? x : add(x, A.back().se));
}
A.pop_back();
}
i64 ask() {
i64 res = F::id();
if (!A.empty()) res = add(res, A.back().se);
if (!B.empty()) res = add(res, B.back().se);
return res;
}
};
struct MAX {
static i64 id() {return -inf;}
i64 operator() (i64 a, i64 b) const {return a > b ? a : b;}
};
struct MIN {
static i64 id() {return inf;}
i64 operator() (i64 a, i64 b) const {return a < b ? a : b;}
};
stqueue<MAX> L;
stqueue<MIN> R;
void _main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i < n; i++) {
x[i] = 0, y[i] = inf;
if (a[i] >= a[i-1] && a[i] >= a[i+1])
x[i] = a[i] + min(a[i-1], a[i+1]);
if (a[i] <= a[i-1] && a[i] <= a[i+1])
y[i] = a[i] + max(a[i-1], a[i+1]);
}
auto check = [&](int x, int y) -> bool {
if (y-x+1 <= 2) return true;
i64 lo = L.ask(), hi = R.ask();
return lo < hi;
};
i64 res = 0;
for (int r = 2, l = 1; r <= n; ) {
while (l < r && !check(l, r)) L.pop(), R.pop(), ++l;
if (l < r) res += r - l;
++r, L.push(x[r-1]), R.push(y[r-1]);
}
cout << res;
}

浙公网安备 33010602011771号