题解:P11822 [湖北省选模拟 2025] 团队分组 / divide
WZwangchongming · · 题解
美味的根号分治,但被本奶龙写了一个神秘做法过了。
Solution:
首先明显可以贪心,对于每个
考虑观察,感性理解,
而且我们打表可以发现,给定
注意更新时上一次查询和上上一次查询分别是什么值。
Main code:
pii solve(int x, int y) {
if(s[x] - y <= 0 || x == 0) return {0, 0};
if(c1[x] == y) return r1[x];
if(c2[x] == y) return r2[x];
int t = lower_bound(s + 1, s + x + 1, s[x] - y) - s;
pii tmp = solve(t - 1, s[x] - s[t - 1]);
tmp.F += 1ll * (tmp.S + 1) * t, tmp.S++;
if(c1[x] == -1) c1[x] = y, r1[x] = tmp;
else if(c2[x] == -1) c2[x] = y, r2[x] = tmp;
else c1[x] = c2[x], r1[x] = r2[x], c2[x] = y, r2[x] = tmp;
return tmp;
}