1375 字
7 分钟
2289C题解

2289C Problem from Ust-Katav#

题目描述#

You are given a permutation of integers p1,p2,…,pnp_1,p_2,\dots,p_n.

We will call a subarray of the permutation pl,pl+1,…,prp_l,p_{l+1},\dots,p_r good if there does not exist another subarray of the same length with the same value of the minimum element in the permutation pp.

Find the number of good subarrays of the permutation pp.

A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order.

输入格式#

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041\le t\le 10^4). The description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤2⋅1051\le n\le 2\cdot 10^5).

The second line of each test case contains nn distinct integers p1,p2,…,pnp_1,p_2,\dots,p_n (1≤pi≤n1\le p_i\le n).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输出格式#

For each test case, output the number of good subarrays in one line.

说明/提示#

题面里的样例没有拿到,此处从略。

思路#

「同一个最小值出现了几次」这件事,按最小值所在的位置来分类最自然:一个子区间的最小值唯一(排列里没有相等的数),所以每个子区间都对应一个「最小值位置」。

对位置 ii,哪些子区间的⼩值恰好是 pip_i?就是那些包含 ii、又不伸出 ii 两侧最近更小数的区间。记

  • lil_i = ii 左边最近的 pj<pip_j<p_i 的位置(没有记 00);
  • rir_i = ii 右边最近的 pj<pip_j<p_i 的位置(没有记 n+1n+1);
  • A=i−liA=i-l_i、B=ri−iB=r_i-i。

那么以 pip_i 为最小值的区间,左端点可以在 li+1…il_i+1\dots i 里选,右端点可以在 i…ri−1i\dots r_i-1 里选。

固定长度数一数#

设区间长度为 LL。左端点离 ii 的距离记 dd(0≤d≤L−10\le d\le L-1,左端点是 i−di-d),那么右端点是 i+L−1−di+L-1-d。要求

d≤A−1,L−1−d≤B−1d\le A-1,\qquad L-1-d\le B-1

也就是 L−B≤d≤min⁡(L−1, A−1)L-B\le d\le \min(L-1,\ A-1)。满足的 dd 个数就是「以 pip_i 为最小值、长度为 LL 的区间个数」:

ci(L)=min⁡(L−1, A−1)−max⁡(0, L−B)+1c_i(L)=\min(L-1,\ A-1)-\max(0,\ L-B)+1

一个区间是 good,当且仅当在它自己的长度 LL 上,ci(L)=1c_i(L)=1。

哪些 LL 满足 ci(L)=1c_i(L)=1#

分段看:

LL 的范围ci(L)c_i(L)什么时候等于 1
L≤min⁡(A,B)L\le\min(A,B)LL只有 L=1L=1
min⁡(A,B)<L≤max⁡(A,B)\min(A,B)<L\le\max(A,B)min⁡(A,B)\min(A,B)当且仅当 min⁡=1\min=1
L>max⁡(A,B)L>\max(A,B)A+B−LA+B-L只有 L=A+B−1L=A+B-1

于是:

  • 若 A=1A=1 或 B=1B=1:好的长度是 L=1,2,…,max⁡(A,B)L=1,2,\dots,\max(A,B),一共 max⁡(A,B)=A⋅B\max(A,B)=A\cdot B 个(因为 min⁡=1\min=1);
  • 否则:只有 L=1L=1 和 L=A+B−1L=A+B-1 两个。

把每个 ii 的这些个数加起来就是答案。

求 li,ril_i,r_i#

两遍单调栈。栈里存下标、栈对应的 pp 单调递增,遇到 pip_i 就把所有 ≥pi\ge p_i 的弹掉,剩下的栈顶就是「左边最近且更小」的。右边那遍把循环方向反过来、哨兵换成 n+1n+1。

判的是 >=,所以相等的会互相弹——求的是严格更小。排列里没有重复值,这一点在这里不出错,但换个题(可重数组)就会。

提交曲线:0 → 100#

#状态用时 / 内存
1Unaccepted 0281ms / 3.20MB
2Accepted 100375ms / 6.30MB

错误代码(0 分):32 位装不下 A⋅BA\cdot B#

第一版和最终版算法一模一样,唯一的差别是开头那行宏:

#include<bits/stdc++.h>
using namespace std;
int T;
int n;int p[200060];
int l[200060],r[200060];
int s[200060];
int ans=0,top=0;
int main()
{
cin>>T;
while(T--){
cin>>n;ans=0;top=0;
...
for(int i=1;i<=n;i++){
int a=i-l[i];
int b=r[i]-i;
if(a==1||b==1) ans+=a*b; // a*b 在 32 位里溢出
else ans+=2;
}

ans、a、b 全是 int。nn 到 2×1052\times10^5 时 A⋅BA\cdot B 最大能到 (105)2=1010(10^5)^2=10^{10},早就超了 int 的 2.1×1092.1\times10^9。加进 ans 之后还会继续累加,溢出得更多。

第二版只改了一处:

#define int long long

把 int main() 换成 signed main(),分数从 0 到 100。

怎么修改:一张对照表#

错误写法正确写法理由
int ans / int a / int b#define int long longA⋅BA\cdot B 上界 101010^{10},超 int
—signed main()宏换了之后 int main() 会变成 long long main(),编译不过

正确代码#

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T;
int n;int p[200060];
int l[200060],r[200060];
int s[200060];
int ans=0,top=0;
signed main()
{
cin>>T;
while(T--){
cin>>n;ans=0;top=0;
for(int i=1;i<=n;i++){l[i]=r[i]=0;s[i]=0;}
for(int i=1;i<=n;i++)cin>>p[i];
for(int i=1;i<=n;i++){
while(top&&p[s[top]]>=p[i]) top--;
l[i]=top?s[top]:0;
s[++top]=i;
}
memset(s,0,sizeof(s));
top=0;
for(int i=n;i>=1;i--){
while(top&&p[s[top]]>=p[i]) top--;
r[i]=top?s[top]:n+1;
s[++top]=i;
}
for(int i=1;i<=n;i++){
int a=i-l[i];
int b=r[i]-i;
if(a==1||b==1) ans+=a*b;
else ans+=2;
}
cout<<ans<<endl;
}
return 0;
}

我想了什么#

这题的关键转化是「按最小值位置分类」,而不是按区间长度扫。 如果想着「枚举长度 LL,统计每个长度的最小值分布」,那是 O(n2)O(n^2) 起步。反过来,固定最小值位置 ii,它管辖的区间是 (li,ri)(l_i,r_i) 里包含 ii 的那些,一次就能算出固定长度下的个数。

公式里 A⋅BA\cdot B 那支只在 A=1A=1 或 B=1B=1 时用,看着奇怪,其实是 max⁡(A,B)\max(A,B)。 因为这两支里某个是 11,max⁡=A⋅B\max= A\cdot B;写成乘法是为了少一个 max 调用,但会让人以为在数二维的东西。

两遍单调栈的写法完全对称:一遍从左往右填 l,一遍从右往左填 r,中间 memset(s,0,sizeof(s)) 加 top=0 重来,共用同一个栈数组。

l[i]/r[i] 在循环前被清了一次(for(i){l[i]=r[i]=0;s[i]=0;}),其实 l、r 后面每个位置都会被写到,这次清多余;s 是真需要,虽然紧跟着又有一次 memset,等于清了两遍。

哨兵用 00 和 n+1n+1 而不是 −1-1 和 n+2n+2。 因为 A=i−liA=i-l_i、B=ri−iB=r_i-i 直接拿来做乘法,取 00/n+1n+1 之后距离分别是 ii 和 n+1−in+1-i,正好是「一路延伸到边界」的长度;用 −1-1 会多出 11。

那次 0 分是纯溢出。 当时看样例能过,就没往 long long 上想——直到大数据的 A⋅BA\cdot B 把 int 撑爆。这类错在小数据上是看不见的,只能靠估上界。

复杂度#

每组时间 O(n)O(n):两遍单调栈各 O(n)O(n),每个下标进出栈各一次。空间 O(n)O(n)。

小结#

思路是「固定最小值位置 → 数出固定长度下的区间个数 → 判断哪些长度只有一个」,再配上两遍单调栈求最近更小数。

真正踩的坑不是算法,是没估上界:A⋅BA\cdot B 最大 101010^{10},int 装不下。

第 119 篇,共 147 篇本文属于系列

2289C题解
https://www.hhluckyex.cn/posts/problen_solution/2289c/
作者
HHluckyex
发布于
2026-11-22
许可协议
CC BY-NC-SA 4.0

分享文章

生成精美分享图或复制链接,与更多人分享本文。

继续阅读

沿着主题读

基于共同的标签与分类

换条路线

从其他文章中稳定抽取