令 A = \min(s, a),并记 \mathrm{need} \leq a 的总状态数为 tot = \mathrm{case_1}(s, A + 1)。如果 code < tot,则说明状态属于 \mathrm{need} \leq a 的情况,否则属于 \mathrm{need} > a 的情况。
先减去 \mathrm{need} \leq a 的状态数:code \leftarrow code - tot。
二分找到最大的 l,满足 \mathrm{sum}f(s - a) - \mathrm{sum}f(s - a - l) \leq code。于是:
\mathrm{need} = a + 1 + l
令 off = code - \left(\mathrm{sum}f(s - a) - \mathrm{sum}f(s - a - l)\right),再令 c = \frac{s - a - l + 1}{2}。二分找到最大的 g,满足 \mathrm{before}(c, g) \leq off。于是:
\mathrm{fir} = a + 2g
剩下的偏移量同理:
\mathrm{mxv} = 2(off - \mathrm{before}(c, g))
发送与接收
由于发送方和接收方都知道相同的 s 和 a,所以双方需要选择相同的消息格式。
发送时,如果 \mathrm{cntstat}(s, a) \leq 2^{64}-1,则发送 \mathrm{encode}(s, a, \mathrm{need}, \mathrm{fir}, \mathrm{mxv}) 编码后的状态;否则退化为发送三个独立的 int。
接收时,如果 \mathrm{cntstat}(s, a) \leq 2^{64}-1,则接收并调用 \mathrm{decode} 恢复状态;否则直接接收三个 int。
注意:\mathrm{encode} 得到的是 unsigned long long,而通信接口通常只能传 long long。应使用 memcpy 保持二进制位不变,避免符号转换导致错误。
通信量分析
左到右仍然每条边传一个 int,共 15 \times 4 = 60 字节。
右到左时,如果能够压缩,则一条边只传一个 long long(8 字节);否则传三个 int(12 字节)。由 \mathrm{cntstat}(s, a) 的公式可以验证,在最大数据范围 N \leq 16, L \leq 10^6 下,最多只有 8 条右到左的边需要退化成 12 字节,其余 7 条边可以用 8 字节表示,这里略去证明。
:::success[代码]
来自 ChatGPT。[回复](/paste/8nqqztoj)。
```cpp
int GetN();
int GetMyId();
long long GetM();
char GetCharAt(long long i);
void PutInt(int target, int val);
void PutLL(int target, long long val);
void Send(int target);
void Receive(int source);
int GetInt(int source);
long long GetLL(int source);
#include <bits/stdc++.h>
using namespace std;
using u128 = unsigned __int128;
using ull = unsigned long long;
static const int PACK_LIMIT = 9000000;
static inline long long BOpen(int K, int f) {
int t = K - f - 1;
if (t <= f) return 1;
return 1LL + t / 2 - f / 2;
}
static inline long long BClose(int K, int f, int r) {
int t = K - f - r;
if (t <= f) return 1;
return 1LL + t / 2 - f / 2;
}
static inline long long OpenChoices(int p, int f) {
int m = min(p, f);
if (f & 1) return (m + 1) / 2;
return m / 2 + 1;
}
static inline long long SumFloor2(long long t) {
if (t < 0) return 0;
long long q = t / 2;
if (t & 1) return q * (q + 1);
return q * q;
}
static inline unsigned long long CloseChoices(int K, int f) {
long long n = K - f;
long long res = n;
long long a = f / 2;
long long lo = 2 * (a + 1);
long long hi = n - 1;
if (hi >= lo) {
long long cnt = hi - lo + 1;
res += SumFloor2(hi) - SumFloor2(lo - 1) - cnt * a;
}
return (unsigned long long)res;
}
static inline int BIndex(int f, int b) {
if (b == f) return 0;
return b / 2 - f / 2;
}
static inline int DecodeB(int f, unsigned long long idx) {
if (idx == 0) return f;
return (int)(2ULL * (idx + (unsigned long long)(f / 2)));
}
static inline bool CanPack(int K) {
return K <= PACK_LIMIT;
}
static inline long long ToLL(ull x) {
const ull HIGH = 1ULL << 63;
if (x <= (ull)LLONG_MAX) return (long long)x;
return (long long)(x - HIGH) + LLONG_MIN;
}
static inline ull ToULL(long long x) {
const ull HIGH = 1ULL << 63;
if (x >= 0) return (ull)x;
return (ull)(x - LLONG_MIN) + HIGH;
}
static long long PackSummary(int K, int p, int need, int firstOffset, int best) {
int lim = min(p, K);
u128 code = 0;
if (firstOffset == K) {
code = (u128)(unsigned int)need;
} else {
code = (u128)lim + 1;
if (need <= p) {
int f = firstOffset;
for (int ff = 0; ff < f; ++ff) {
code += (u128)OpenChoices(p, ff) * (u128)BOpen(K, ff);
}
unsigned long long bc = (unsigned long long)BOpen(K, f);
int rankC = (need - (f & 1)) / 2;
code += (u128)(unsigned int)rankC * (u128)bc;
code += (u128)(unsigned int)BIndex(f, best);
} else {
for (int ff = 0; ff < K; ++ff) {
code += (u128)OpenChoices(p, ff) * (u128)BOpen(K, ff);
}
int f = firstOffset;
for (int ff = p; ff < f; ff += 2) {
code += (u128)CloseChoices(K, ff);
}
int r = need - p;
for (int rr = 1; rr < r; ++rr) {
code += (u128)BClose(K, f, rr);
}
code += (u128)(unsigned int)BIndex(f, best);
}
}
return ToLL((ull)code);
}
static void UnpackSummary(
int K,
int p,
long long val,
int &need,
int &firstOffset,
int &best
) {
u128 x = (u128)ToULL(val);
int lim = min(p, K);
if (x <= (u128)lim) {
need = (int)(ull)x;
firstOffset = K;
best = K;
return;
}
x -= (u128)lim + 1;
for (int f = 0; f < K; ++f) {
unsigned long long bc = (unsigned long long)BOpen(K, f);
u128 cnt = (u128)OpenChoices(p, f) * (u128)bc;
if (x >= cnt) {
x -= cnt;
} else {
unsigned long long y = (unsigned long long)x;
unsigned long long rankC = y / bc;
unsigned long long bidx = y % bc;
need = (int)((f & 1) + 2ULL * rankC);
firstOffset = f;
best = DecodeB(f, bidx);
return;
}
}
for (int f = p; f < K; f += 2) {
u128 cnt = (u128)CloseChoices(K, f);
if (x >= cnt) {
x -= cnt;
} else {
for (int r = 1; r <= K - f; ++r) {
unsigned long long bc = (unsigned long long)BClose(K, f, r);
if (x >= (u128)bc) {
x -= (u128)bc;
} else {
need = p + r;
firstOffset = f;
best = DecodeB(f, (unsigned long long)x);
return;
}
}
}
}
need = 0;
firstOffset = K;
best = K;
}
long long LongestValidParentheses() {
int lvper = 0;
(void)lvper;
int N = GetN();
int id = GetMyId();
int M = (int)GetM();
int L = M / N;
int base = id * L;
int closeCnt = 0;
int openCnt = 0;
for (int i = 0; i < L; ++i) {
char ch = GetCharAt((long long)base + i);
if (ch == '(') {
++openCnt;
} else {
if (openCnt > 0) --openCnt;
else ++closeCnt;
}
}
int openBefore = 0;
if (id != 0) {
Receive(id - 1);
openBefore = GetInt(id - 1);
}
int openAfter = (openBefore > closeCnt ? openBefore - closeCnt : 0) + openCnt;
if (id + 1 < N) {
PutInt(id + 1, openAfter);
Send(id + 1);
}
int needAfter = 0;
int sufFirst = M;
int sufBest = 0;
if (id + 1 < N) {
int rightStart = (id + 1) * L;
int rightK = M - rightStart;
int rightOpenBefore = openAfter;
Receive(id + 1);
if (CanPack(rightK)) {
long long code = GetLL(id + 1);
int firstOffset;
UnpackSummary(rightK, rightOpenBefore, code, needAfter, firstOffset, sufBest);
sufFirst = rightStart + firstOffset;
} else {
needAfter = GetInt(id + 1);
unsigned long long info = (unsigned long long)GetLL(id + 1);
sufFirst = (int)(info >> 32);
sufBest = (int)(info & 0xffffffffULL);
}
}
int skipClose = min(openBefore, closeCnt);
int keepOpen = (openCnt > needAfter ? openCnt - needAfter : 0);
vector<int> st;
st.reserve((size_t)L);
bool has = false;
int localFirst = -1;
int localLast = -1;
int localBest = 0;
auto add_pos = [&](int p) {
if (!has) {
has = true;
localFirst = localLast = p;
} else {
localBest = max(localBest, p - localLast - 1);
localLast = p;
}
};
int seenClose = 0;
for (int i = 0; i < L; ++i) {
int pos = base + i;
char ch = GetCharAt((long long)pos);
if (ch == '(') {
st.push_back(pos);
} else {
if (!st.empty()) {
st.pop_back();
} else {
if (seenClose >= skipClose) {
add_pos(pos);
}
++seenClose;
}
}
}
for (int i = 0; i < keepOpen; ++i) {
add_pos(st[i]);
}
int combFirst;
int combBest;
if (!has) {
combFirst = sufFirst;
combBest = max(sufBest, sufFirst - base);
} else {
combFirst = localFirst;
int startGap = localFirst - base;
int crossGap = sufFirst - localLast - 1;
combBest = localBest;
combBest = max(combBest, startGap);
combBest = max(combBest, sufBest);
combBest = max(combBest, crossGap);
}
int needBefore = closeCnt + (needAfter > openCnt ? needAfter - openCnt : 0);
if (id != 0) {
int K = M - base;
int firstOffset = combFirst - base;
if (CanPack(K)) {
long long code = PackSummary(K, openBefore, needBefore, firstOffset, combBest);
PutLL(id - 1, code);
Send(id - 1);
} else {
unsigned long long info =
((unsigned long long)(unsigned int)combFirst << 32)
| (unsigned int)combBest;
PutInt(id - 1, needBefore);
PutLL(id - 1, (long long)info);
Send(id - 1);
}
return 0;
}
return (long long)combBest;
}
```
:::