nowcoder106I Neat Tree (单调栈)

Richard神犇出给nowcoder的题

用单调栈找到每个点它向右和向左的第一个大于或小于它的位置,然后它作为最大值/最小值的区间就要在这个范围里,那么它的贡献就是这个区间长度乘一乘再减一减

注意一下值相等的时候怎么处理

 1 #include<bits/stdc++.h>
 2 #define pa pair<int,int>
 3 #define CLR(a,x) memset(a,x,sizeof(a))
 4 using namespace std;
 5 typedef long long ll;
 6 const int maxn=1e6+10;
 7 
 8 inline ll rd(){
 9     ll x=0;char c=getchar();int neg=1;
10     while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
11     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
12     return x*neg;
13 }
14 
15 int N,a[maxn];
16 int stk[maxn][2],sh,mm[maxn];
17 ll ans;
18 
19 int main(){
20     //freopen(".in","r",stdin);
21     int i,j,k;
22     while(~scanf("%d",&N)){
23         for(i=1;i<=N;i++) a[i]=rd();
24         CLR(mm,0);ans=0;
25         for(i=1;i<=N;i++){
26             while(sh&&stk[sh][0]<a[i]){
27                 mm[stk[sh--][1]]=i-1;
28             }
29             stk[++sh][0]=a[i],stk[sh][1]=i;
30         }for(;sh;--sh) mm[stk[sh][1]]=N;
31         
32         for(i=N;i;i--){
33             while(sh&&stk[sh][0]<=a[i]){
34                 if(mm[stk[sh][1]]) ans+=1ll*(stk[sh][1]-i)*(mm[stk[sh][1]]-stk[sh][1]+1)*stk[sh][0];
35                 sh--;
36             }
37             stk[++sh][0]=a[i],stk[sh][1]=i;
38         }for(;sh;--sh) if(mm[stk[sh][1]]) ans+=1ll*stk[sh][1]*(mm[stk[sh][1]]-stk[sh][1]+1)*stk[sh][0];
39         
40         CLR(mm,0);
41         for(i=1;i<=N;i++){
42             while(sh&&stk[sh][0]>a[i]){
43                 mm[stk[sh--][1]]=i-1;
44             }
45             stk[++sh][0]=a[i],stk[sh][1]=i;
46         }for(;sh;--sh) mm[stk[sh][1]]=N;
47         
48         for(i=N;i;i--){
49             while(sh&&stk[sh][0]>=a[i]){
50                 if(mm[stk[sh][1]]) ans-=1ll*(stk[sh][1]-i)*(mm[stk[sh][1]]-stk[sh][1]+1)*stk[sh][0];
51                 sh--;
52             }
53             stk[++sh][0]=a[i],stk[sh][1]=i;
54         }for(;sh;--sh) if(mm[stk[sh][1]]) ans-=1ll*stk[sh][1]*(mm[stk[sh][1]]-stk[sh][1]+1)*stk[sh][0];
55             
56         printf("%lld\n",ans);
57     }
58     
59     return 0;
60 }

 

转载于:https://www.cnblogs.com/Ressed/p/9711152.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值