题目
有一条长度为n的路,第i段路的高度hi,若hl.....hr这段路按高度排序后相邻的两段高度差不超过1则就不颠簸,求不颠簸的路段数量
思路&&题解
自己的思路:
突发奇想发现这个东西如果找到最大值maxx,最小值minn,它们的差如果等于r-l,那么好像就是一个可行解,再标记一下哪些h是相同的个数为ep,那么maxx-minn+ep等于r-l时就满足了
可是这是O(N^2)的,怎么优化这个东西呢???(自闭ing)
正解:
按照上面的思路继续推,考虑一种这样的思路:如果我们枚举r,那么maxx与minn好像是可以用单调队列维护出来的,用两个即可,仔细一想,其实ep好像也可以维护一下,那么令F[i]表示以i为左端点,r为右端点的maxx-minn+ep
现在考虑怎样修改F[]数组,这就需要熟练掌握单调栈了,以单调递减的栈为例子,对于栈中的相邻两个元素在原序列的位置的这段区间,它们的最大值是相同的,那么修改的时候就是区间修改max,对于ep的修改,就是找到从右往左找到第一个跟它相等的点i,那么[1,i]这段区间的ep都要加1
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 1e5 + 3;
int n , quea[MAXN] , heada , taila , queb[MAXN] , headb , tailb , a[MAXN];
int h[MAXN];
struct tree{
int l , r ;
ll num, minn , lazy;
}tre[MAXN<<2];
map<int,int>mp;
void pushup( int i ){
if( tre[i<<1].minn < tre[i<<1|1].minn ) tre[i].minn = tre[i<<1].minn , tre[i].num = tre[i<<1].num;
else if( tre[i<<1].minn > tre[i<<1|1].minn ) tre[i].minn = tre[i<<1|1].minn , tre[i].num = tre[i<<1|1].num;
else tre[i].minn = tre[i<<1].minn , tre[i].num = tre[i<<1].num + tre[i<<1|1].num;
}
void build( int i , int l , int r ){
tre[i].l = l , tre[i].r = r , tre[i].lazy = 0;
if( l == r ){
tre[i].minn = l;tre[i].num = 1;
return ;
}
int mid = l + r >> 1;
build( i << 1 , l , mid );
build( i << 1 |1 , mid + 1 , r );
pushup( i );
}
void pushdown( int i ){
if( tre[i].lazy ){
tre[i<<1].minn += tre[i].lazy , tre[i<<1|1].minn += tre[i].lazy;
tre[i<<1].lazy += tre[i].lazy , tre[i<<1|1].lazy += tre[i].lazy;
tre[i].lazy = 0;
}
}
void modify( int i , int l , int r , ll delta ){
if( tre[i].l > r || tre[i].r < l ) return ;
if( tre[i].l >= l && tre[i].r <= r ){
tre[i].minn += delta;
tre[i].lazy += delta;
return ;
}
pushdown( i );
modify( i << 1 , l , r , delta );
modify( i << 1 | 1 , l , r , delta );
pushup( i );
}
ll tot , minn;
void query( int i , int l , int r , ll &minn , ll &tot ){
if( tre[i].l > r || tre[i].r < l ) return ;
if( tre[i].l >= l && tre[i].r <= r ){
if( minn > tre[i].minn ) tot = tre[i].num , minn = tre[i].minn;
else if( minn == tre[i].minn ) tot += tre[i].num;
return ;
}
pushdown( i );
query( i << 1 , l , r , minn , tot );
query( i << 1 | 1, l , r , minn , tot );
}
int main(){
scanf( "%d" , &n );
for( int i = 1 ; i <= n ; i ++ ) scanf( "%d" , &a[i] );
build( 1 , 1 , n );
ll ans = 0;
for( int i = 1 ; i <= n ; i ++ ){
int st = 0;
while( heada < taila && a[quea[taila]] < a[i] ){
if( heada < taila - 1 ){
modify( 1 , quea[taila-1] + 1 , quea[taila] , -a[quea[taila]] );
modify( 1 , quea[taila-1] + 1 , quea[taila] , a[i] );
}
st = quea[taila];
taila --;
}
if( st && taila == heada ){
modify( 1 , 1 , st , -a[st] );
modify( 1 , 1 , st , a[i] );
}
st = 0;
while( headb < tailb && a[queb[tailb]] > a[i]){
if( headb < tailb - 1 ){
modify( 1 , queb[tailb-1] + 1 , queb[tailb] , a[queb[tailb]] );
modify( 1 , queb[tailb-1] + 1 , queb[tailb] , -a[i] );
}
st = queb[tailb];
tailb --;
}
if( st && tailb == headb ){
modify( 1 , 1 , st , a[st] );
modify( 1 , 1 , st , -a[i] );
}
int rep = mp[a[i]];
if( rep )
modify( 1 , 1 , rep , 1 );
mp[a[i]] = i;
quea[++taila] = i;
queb[++tailb] = i;
minn = 0x3f3f3f3f;tot = 0;
query( 1 , 1 , i , minn , tot );
if( minn == i )
ans += tot;
}
printf( "%lld\n" , ans );
return 0;
}