https://www.zhihu.com/question/566839745/answer/2778776188
上周六考完周测回到家,无聊看了眼今年的 NOIp。
不到一个小时就嘴巴 400pts 了,依旧很无聊。
过了三天尝试写了一下,发现了 T3 的思维错误但是很快重新想到了正确的做法,还是很无聊。
下面简单写一下我的思路。以下思路均经过代码检验。
T1
记录 表示第 行第 列位置作为左边端点的最长可种植线段长度 - 1。比如说:
C 0 0 0 1 0 1 1 0 0 0 0
A 2 1 0 0 0 0 0 3 2 1 0这个数组可以直接从右往左线性递推,预处理复杂度 。
然后就是计算。我们依然可以线性求出所有的竖段,对于每一个竖段,我们从上到下枚举,并维护相关变量。
对于 这一段,枚举到第 位时,维护一个变量:
那么就可以累加答案:
整个的时间复杂度是 。
T2
先考虑 。此时我们设定一个栈为空栈,其他 个栈分别处理两个数字。在遇到一个数字的时候:
- 如果它没有出现过,直接加到负责它的栈里面。
- 如果它在栈顶,直接抵消。
- 如果它在栈底,扔到空栈后用第二个操作抵消。
然后考虑 。根据前面的讨论,如果场上无法保证有空栈的话,只要遇上成片成片的栈底元素,就很有可能出现卡死的情况。因此我们尝试维护一直有空栈的情况。
思路和上面一样,我们还是让每个栈只负责两个数字,但是每个栈负责的数字可能会发生改变。在此刻,如果仍然使用上面提到的策略,只要还有空栈,我们就依然可以快乐消消乐。接下来考虑最坏情况:场上有 个栈塞了两个数字,且它们两两不同,此时仅剩的最后一种数字(设为 )马上就要加进来。
我们发现,对于一个栈底为 ,栈顶为 的栈,只要在此之后 出现的位置比 靠前(此时称其为 “优势在我” 的栈),我们就可以直接把 放在这个栈顶部。此时有若干种情况:
- 野生的 出现了!此时直接扔到空栈里面用第二个操作消掉。
- 野生的 出现了!此时直接扔到 头上消掉。
- 野生的 出现了!此时 比 后出现,目前有可能触发最坏情况,重新考虑就好了。
- 野生的其他数字出现了!此时使用一开始的策略。
最坏的情况无非就没有 “优势在我” 的栈,而此时我们只需要直接把 扔到空栈里。然后我们找到,在接下来的牌堆中,目前 个在栈底的数字中最先出现的数字(设为 )。显然, 上方的数字先于它出现,那么我们就需要 “保住” 这个栈,让它在 到来时恰好只有 一个数字。值得庆幸的是,由于在 前只会有 种数字,那么只需要不往这个栈里面扔新的数字就好了。
因而,我们可以保证,在没有空栈的情况下,我们有策略在新的空栈出现前,应对接下来的情况。这道题也就得以解决了。
复杂度挺清奇,但是利用一些队列技术压到 不是问题。
我们先记录下目前的空栈下标队列和单元素栈下标队列,需要的时候就弹出来一个下标用。对于 “空栈要逃走了!” 的情况,只需要在一个单元素栈即将添加一个元素的时候,额外处理一下这个栈是否 “优势在我”,是的话就扔进一个新的队列,要的时候取出来用。如果实在没有 “优势在我” 的栈的话,我们只需要将要 “保住” 的栈的栈顶数字对应到逃走的空栈上就好了。
没想到退役选手居然真的在想代码细节,哈哈。
T3
先使用 Tarjan 算法求出割桥。我们不妨断开所有的割桥,就会发现这个图被分成了若干个连通块,而每个连通块随便删一条边都是联通的,这也就意味着这里面的点和边可以乱选。然后,将这些连通块缩成一个个点,这道题目就变成经典的树上 dp 套路题(至于为什么是树的话……如果有一个环,那环上的点就又得缩起来,而且环上的边也就不是割边了)。然后就可以在新的树上快乐 dp 了。
过程稍微有点繁琐,在此简单讲解思路。考虑在一个点上,统计选的点 LCA 为该点的方案。其中又分为若干个部分:当前点没选内部点、当前点选了内部点但是子孙都没有选、当前点选了内部点而且恰好有一个儿子选了内部点,以及当前点选了内部点而且有多个儿子选了内部点。
T4
一眼离线答案(虽然好像不是很一眼)。
有一个很经典的思维模板:对于一个序列,维护后缀最大值的时候,我们会发现其自然形成了一个个值递减的段。我们自然可以使用单调栈将这些段存起来。而当这个序列后面多了一个值的时候,变化有两种:
- 加进来的数字是目前的最小值,那么前面不会改变,只有最后多了一个小段。
- 加进来的数字不是目前的最小值,此时只有最靠右的一些段被覆盖成了新加的数字,然后最后一段长长了一点。
利用单调栈的特性,整个维护的时间复杂度在 内,其中 代表序列长度。
利用这个特性,我们将离线后的答案按照右端点排序,然后考虑一个个将两个序列的数字加进两个空序列中,使用单调栈维护后缀最大值,然后计算答案。
这里需要用到线段树维护答案。我们需要维护两个序列 和 ,分别表示两个序列目前的后缀最大值。我们需要在线段树的每个最小节点 维护 ,然后维护这个值的区间和。
我们发现,目前的需求只有 个区间覆盖的请求,当我们将这个覆盖操作下传到被完全覆盖的线段树节点的时候,我们惊讶地发现:此时这段区间的 或者 被修改成了同一个值,那么只需要额外分别维护两个序列在这个区间内的加和,以及一些附加信息,就可以算出新的 区间和,从而完成一次标准的线段树上修改。这两个新的变量相对比较麻烦,这里不再展开(实际上这里的线段树要开六个 Tag 维护四个值,但是主要都是 Tag 在区间覆盖 Tag 下的退化,所以还算是好写)。
复杂度 ,常数比较大,但是也足够稳健了。
代码
// T1 - O(N M) solution
// Z => Modulo structure
int T, id;
int N, M, C, F;
char ch[1010][1010];
int A[1010][1010];
Z c, f;
inline void calc (int u, int d, int y) {
Z v = 0;
for (int x = u + 1; x <= d; x ++) {
c += v * A[x][y];
f += v * A[x][y] * (d - x);
v += A[x-1][y];
}
}
int main(){
cin >> T >> id;
while (T --) {
cin >> N >> M >> C >> F;
for (int i = 1; i <= N; i ++) {
cin >> (ch[i] + 1);
A[i][M] = 0;
for (int j = M - 1; j; j --)
A[i][j] = ((ch[i][j+1] == '0') ? A[i][j+1] + 1 : 0);
}
c = f = 0;
for (int j = 1; j <= M; j ++) {
int l = 0;
for (int i = 1; i <= N; i ++) {
if (ch[i][j] == '1') {
if (l != 0)
calc (l, i - 1, j);
l = 0;
}
else if (l == 0)
l = i;
}
if (l != 0)
calc (l, N, j);
}
printf("%d %d\n", (c * C).val, (f * F).val);
}
return 0;
}// T2 - O(M log N) solution
int N, M, K;
vector<vector<int> > vs;
unordered_set<int> stk0, stk1, potential;
vector<pair<int, int> > stks[310];
int S[2000010];
int nxt[2000010];
int lbl[610];
int sv, wt;
bool isEnd[610];
void place (int i, int x) {
if (stk0.size() > 1) {
int v = *stk0.begin();
stk0.erase(stk0.begin());
stks[v].push_back({x, nxt[i]});
lbl[x] = v;
stk1.insert(v);
vs.push_back({1, v});
isEnd[x] = true;
}
else if (!stk1.empty()){
int v = *stk1.begin();
stk1.erase(stk1.begin());
stks[v].push_back({x, nxt[i]});
lbl[x] = v;
vs.push_back({1, v});
// check potential
if (stks[v][0].second < stks[v][1].second)
potential.insert(v);
}
else {
// crisis
if (!potential.empty()) {
int v = *potential.begin();
potential.erase(potential.begin());
stks[v].push_back({x, nxt[i]});
lbl[x] = v;
vs.push_back({1, v});
}
else {
int v = *stk0.begin();
stk0.erase(stk0.begin());
stks[v].push_back({x, nxt[i]});
lbl[x] = v;
stk1.insert(v);
vs.push_back({1, v});
isEnd[x] = true;
// seek the waiting time
int p;
for (p = i + 1; ; p ++)
if (isEnd[S[p]]) break;
p = S[p];
wt = p;
sv = lbl[p];
if (sv == v)
stk1.erase(v);
}
}
}
void remove (int x) {
// it must be one of them
if (stks[lbl[x]].back().first == x) {
vs.push_back({1, lbl[x]});
stks[lbl[x]].pop_back();
}
else {
int u = *stk0.begin();
vs.push_back({1, u});
vs.push_back({2, u, lbl[x]});
stks[lbl[x]].erase(stks[lbl[x]].begin());
isEnd[stks[lbl[x]][0].first] = true;
}
if (x == wt) {
// yeah the job have done
if (stks[sv].size() == 0)
stk0.insert(sv);
else
stk1.insert(sv);
sv = wt = 0;
}
else if (lbl[x] == sv) {
// ignore all of them
}
else if (stks[lbl[x]].size() == 0) {
stk1.erase(lbl[x]);
stk0.insert(lbl[x]);
}
else if (stks[lbl[x]].size() == 1) {
potential.erase(lbl[x]);
stk1.insert(lbl[x]);
}
else {
// 3 -> 2 & back to potential
if (stks[lbl[x]][0].second < stks[lbl[x]][1].second)
potential.insert(lbl[x]);
}
lbl[x] = 0;
isEnd[x] = false;
}
int main(){
multiCase () {
cin >> N >> M >> K;
vs.clear();
stk0.clear(); stk1.clear(); potential.clear();
for (int i = 1; i <= N; i ++) {
stks[i].clear();
stk0.insert(i);
}
readI (1, M, S);
for (int i = 1; i <= K; i ++)
lbl[i] = M + 1;
for (int i = M; i; i --) {
nxt[i] = lbl[S[i]];
lbl[S[i]] = i;
}
for (int i = 1; i <= K; i ++)
lbl[i] = 0;
for (int i = 1; i <= M; i ++) {
if (lbl[S[i]] == 0)
place(i, S[i]);
else
remove(S[i]);
}
printf("%d\n", (int)vs.size());
for (auto x: vs) {
for (auto v: x)
printf("%d ", v);
puts("");
}
}
return 0;
}// T3 - O(N + M) solution
// Z => Modulo structure
// Graph => Undirected graph structure
int N, M;
Graph G;
int dfn[500010], low[500010], idx;
int nN;
vector<int> stk;
int nIdx[500010];
void tarjan (int x, int fa) {
dfn[x] = low[x] = ++ idx;
stk.push_back(x);
for (auto v: G[x]) {
if (v == fa)
continue;
if (!dfn[v]) {
tarjan(v, x);
low[x] = min(low[x], low[v]);
}
else
low[x] = min(low[x], dfn[v]);
}
if (dfn[x] == low[x]) {
++ nN;
int xx = 0;
while (xx != x) {
xx = stk.back();
stk.pop_back();
nIdx[xx] = nN;
}
}
}
vector<int> nDir[500010];
int uN[500010], uM[500010];
Z pw2[1000010];
Z f[500010];
Z inv2, ans = 0;
void dfs (int x, int fa) {
f[x] = pw2[uN[x]];
Z t1 = 0, t2 = 0;
for (auto v: nDir[x]) {
if (v == fa)
continue;
dfs(v, x);
t2 += (t1 + t2) * (f[v] - 1) * inv2;
t1 += inv2 * (f[v] - 1);
f[x] *= (1 + inv2 * (f[v] - 1));
}
ans += t2 + (pw2[uN[x]] - 1) * (1 + t1 + t2);
}
int main(){
inv2 = Z(2).inv();
cin >> N >> M;
G = Graph(N, M);
G.init();
tarjan(1, 0);
for (int i = 1; i <= N; i ++) {
++ uN[nIdx[i]];
for (auto j: G[i])
if (nIdx[i] != nIdx[j])
nDir[nIdx[i]].push_back(nIdx[j]);
else if (i < j)
++ uM[nIdx[i]];
}
pw2[0] = 1;
for (int i = 1; i <= M || i <= N; i ++)
pw2[i] = pw2[i-1] * 2;
dfs(1, 0);
printf("%d", (ans * pw2[M]).val);
return 0;
}// T4 - O(N log N + Q log Q) solution
// Z => Modulo structure
typedef unsigned long long ull;
int N, Q;
int A[250010], B[250010];
struct Query {
int l, r, id;
Query (int l = 0, int r = 0, int id = 0)
:l(l), r(r), id(id) {}
bool operator < (const Query& q) const {
return r < q.r;
}
} Qs[250010];
vector<pair<int, int> > stkA, stkB;
ull ans [250010];
ull addA[1000010], addB[1000010], addAB[1000010], addC[1000010];
ull tagA[1000010], tagB[1000010];
ull sumA[1000010], sumB[1000010];
ull sumAB[1000010], tot[1000010];
int len[1000010];
void applyAC (int x, ull a) {
tot[x] += len[x] * a;
addC[x] += a;
}
void applyA (int x, ull a) {
sumA[x] = a * len[x];
sumAB[x] = sumB[x] * a;
tagA[x] = a;
}
void applyB (int x, ull a) {
sumB[x] = a * len[x];
sumAB[x] = sumA[x] * a;
tagB[x] = a;
}
void applyAA (int x, ull a) {
if (tagA[x])
applyAC (x, a * tagA[x]);
else {
tot[x] += a * sumA[x];
addA[x] += a;
}
}
void applyAB (int x, ull a) {
if (tagB[x])
applyAC (x, a * tagB[x]);
else {
tot[x] += a * sumB[x];
addB[x] += a;
}
}
void applyAAB (int x, ull a) {
if (tagA[x] && tagB[x])
applyAC (x, a * tagA[x] * tagB[x]);
else if (tagA[x])
applyAB (x, a * tagA[x]);
else if (tagB[x])
applyAA (x, a * tagB[x]);
else
tot[x] += a * sumAB[x], addAB[x] += a;
}
void pd (int x) {
if (addAB[x]) {
applyAAB (x << 1, addAB[x]);
applyAAB (x << 1 | 1, addAB[x]);
addAB[x] = 0;
}
if (addA[x]) {
applyAA (x << 1, addA[x]);
applyAA (x << 1 | 1, addA[x]);
addA[x] = 0;
}
if (addB[x]) {
applyAB (x << 1, addB[x]);
applyAB (x << 1 | 1, addB[x]);
addB[x] = 0;
}
if (addC[x]) {
applyAC (x << 1, addC[x]);
applyAC (x << 1 | 1, addC[x]);
addC[x] = 0;
}
if (tagA[x]) {
applyA (x << 1, tagA[x]);
applyA (x << 1 | 1, tagA[x]);
tagA[x] = 0;
}
if (tagB[x]) {
applyB (x << 1, tagB[x]);
applyB (x << 1 | 1, tagB[x]);
tagB[x] = 0;
}
}
void pu (int x) {
sumA[x] = sumA[x << 1] + sumA[x << 1 | 1];
sumB[x] = sumB[x << 1] + sumB[x << 1 | 1];
sumAB[x] = sumAB[x << 1] + sumAB[x << 1 | 1];
tot[x] = tot[x << 1] + tot[x << 1 | 1];
}
void mdfA (int x, int l, int r, int ll, int rr, int a) {
if (ll <= l && r <= rr) {
applyA (x, a);
return;
}
pd(x);
int m = (l + r) >> 1;
if (ll <= m)
mdfA (x << 1, l, m, ll, rr, a);
if (rr > m)
mdfA (x << 1 | 1, m + 1, r, ll, rr, a);
pu(x);
}
void mdfB (int x, int l, int r, int ll, int rr, int a) {
if (ll <= l && r <= rr) {
applyB (x, a);
return;
}
pd(x);
int m = (l + r) >> 1;
if (ll <= m)
mdfB (x << 1, l, m, ll, rr, a);
if (rr > m)
mdfB (x << 1 | 1, m + 1, r, ll, rr, a);
pu(x);
}
ull que (int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr)
return tot[x];
ull ret = 0;
pd(x);
int m = (l + r) >> 1;
if (ll <= m)
ret = que (x << 1, l, m, ll, rr);
if (rr > m)
ret += que (x << 1 | 1, m + 1, r, ll, rr);
return ret;
}
void bul (int x, int l, int r) {
len[x] = r - l + 1;
if (l == r)
return;
int m = (l + r) >> 1;
bul (x << 1, l, m);
bul (x << 1 | 1, m + 1, r);
}
int main(){
cin >> N; cin >> N;
readI(1, N, A);
readI(1, N, B);
cin >> Q;
for (int i = 1; i <= Q; i ++) {
cin >> Qs[i].l >> Qs[i].r;
Qs[i].id = i;
}
sort (Qs + 1, Qs + Q + 1);
bul (1, 1, N);
stkA.push_back({N + 1, 0});
stkB.push_back({N + 1, 0});
int p = 1;
for (int i = 1; i <= N; i ++) {
while (stkA.back().first < A[i])
stkA.pop_back();
mdfA (1, 1, N, stkA.back().second + 1, i, A[i]);
stkA.push_back({A[i], i});
while (stkB.back().first < B[i])
stkB.pop_back();
mdfB (1, 1, N, stkB.back().second + 1, i, B[i]);
stkB.push_back({B[i], i});
applyAAB (1, 1);
while (Qs[p].r == i)
ans[Qs[p].id] = que (1, 1, N, Qs[p].l, Qs[p].r), ++ p;
}
for (int i = 1; i <= Q; i ++)
printf("%llu\n", ans[i]);
return 0;
}