只想看着蓝天是一种任性吗?
分析
感觉我这辈子第一次见到这么厉害的题,真的是神了。
首先先考虑对于给定的序列
发现一个观察是:对于序列
这个东西的证明应该是比较显然的:若上式不成立,则最有方案必定是分别将
直接依靠这个性质做就可以
但是有个 shaber 没有考虑
要求
分析固定左端点,不断枚举右端点的过程。设左端点为
而满足这个东西的
考虑
而根据
易得在
梳理一下,我们的做法是:
从大到小枚举子段的左端点
-
若
[l, r] 内不存在i 满足a_l < a_i ,则子段对答案的贡献为r - l 。 -
否则,
-
设
y 为最小的满足a_l < a_y 的数,若[y, r] 内不存在i 满足a_l > a_x ,则[l, y - 1] 对子段代价的贡献为y - l - 1 。 -
否则,
[l, y - 1] 对子段代价的贡献为y - l 。
最后拿一个
\text{sum}_l 记录所有以l 为左端点的子段的代价和。因为我们拆了贡献,所以要给\text{sum}_l 加上\text{sum}_y 。 -
最后的答案即
正解
发现对于每个左端点
求
做法的再优化
时间
考虑对序列建出双向链表,并按照值从大到小枚举
但是由于
离散化数组
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define mid (l + r >> 1)
#define lowbit(x) (x & -x)
const int N = 3e5 + 5;
int T, n, top, ans;
int a[N], st[N], p[N], cnt[N], pre[N], lst[N], sum[N];
void lsh(int a[], int sz) {
vector<int> v;
for (int i = 1; i <= sz; ++i) v.push_back(a[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= sz; ++i) a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
}
void Solve() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
lsh(a, n); top = 0;
for (int i = n; i > 0; --i) {
while (top && a[st[top]] < a[i]) --top;
cnt[i] = top == 0 ? n + 1 : st[top];
st[++top] = i;
}
for (int i = 1; i <= n; ++i) { p[a[i]] = i; pre[i] = i - 1; lst[i] = i + 1; }
for (int i = n; i > 0; --i) {
int s = p[i], x = lst[cnt[s] - 1];
sum[s] += (cnt[s] - s) * (cnt[s] - s - 1) / 2 // f[s][s + 1] + ... + f[s][cnt[s] - 1]
+ (x - cnt[s]) * (cnt[s] - s - 1) // [s, cnt[s]]'s contribution to f[s][cnt[s] to x - 1]
+ (n - x + 1) * (cnt[s] - s); // [s, cnt[s]]'s contribution to f[s][x to n]
lst[pre[s]] = lst[s];
pre[lst[s]] = pre[s];
}
for (int i = n; i > 0; --i) { sum[i] += sum[cnt[i]]; ans += sum[i]; }
cout << ans << endl; ans = 0;
for (int i = 1; i <= n; ++i) sum[i] = pre[i] = lst[i] = 0; // clear
}
signed main() {
cin >> T;
while (T--) Solve();
return 0;
}
结束了……吗?
因为建链表的方法要从小到大枚举
接下来要介绍一种神仙做法。
注意到求
不过一个问题是:对于每个
冷静分析一下,考虑被删掉的栈内数
考虑
-
若
a_{l'} > a_y ,则l' 对应的x ,记作x' ,必定满足x' \le y ,又因为显然y < p ,所以x' < p ,与p 产生贡献矛盾。 -
若
a_{l'} < a_y ,则考虑将p 从栈中移除的数a_q 。-
若
q > p ,则因为a_{l'} > a_q > a_p ,所以有x' \le q ,又因为显然q < y < p ,所以矛盾。 -
否则,由定义,
\forall i \in (q, y) ,a_q > a_y ,又因为q < p < y' < y ,故有a_{y' + 1} < a_q < a_p < a_{l'} ,故x' = y' + 1 ,矛盾。
-
当然上面的分析并没有考虑到无解的情况,不过无解时比较显然所以就不做赘述了。
于是我们发现,直接单调栈就很神奇地对完了!
最后只剩下了一个细节:对于每个
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define mid (l + r >> 1)
#define lowbit(x) (x & -x)
const int N = 3e5 + 5;
int T, n, top, ans;
int a[N], st[N], cnt[N], sum[N];
vector<int> v[N];
void Solve() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
top = 0;
for (int i = n; i > 0; --i) {
while (top && a[st[top]] < a[i]) --top;
cnt[i] = top == 0 ? n + 1 : st[top];
if (cnt[i] == n + 1) sum[i] = (n + 1 - i) * (n - i) / 2;
else v[st[top]].push_back(i);
st[++top] = i;
}
top = 0;
for (int i = n; i > 0; --i) {
if (v[i].size() > 0) {
for (int j = v[i].size() - 1; j >= 0; --j) {
int s = v[i][j], x = n + 1;
while (top && a[st[top]] > a[s]) --top;
if (top) x = st[top];
sum[s] += (i - s) * (i - s - 1) / 2 // f[s][s + 1] + ... + f[s][cnt[s] - 1]
+ (x - i) * (i - s - 1) // [s, cnt[s]]'s contribution to f[s][cnt[s] to x - 1]
+ (n - x + 1) * (i - s); // [s, cnt[s]]'s contribution to f[s][x to n]
}
}
while (top && a[st[top]] > a[i]) --top;
st[++top] = i;
}
for (int i = n; i > 0; --i) { sum[i] += sum[cnt[i]]; ans += sum[i]; }
cout << ans << endl; ans = 0;
for (int i = 1; i <= n; ++i) { sum[i] = 0; v[i].clear(); } // clear
}
signed main() {
cin >> T;
while (T--) Solve();
return 0;
}
然后这个东西好像还可以扩展成任意的二维单调栈。因为第一次单调栈总满足一个性质是,对于每个