2 条题解

  • 0
    @ 2023-5-29 1:11:58
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int MX=300005;
    int n,le_cnt[MX],array[MX];ll tot;struct STR{int pos,num;};stack<STR>stk;
    STR pack(int pos,int num){STR a;a.pos=pos;a.num=num;return a;}
    void stk_push1(){
    	for(int i=1;i<=n;i++){
    		while(!stk.empty()&&stk.top().num<array[i]){
    		    tot+=(ll)(array[stk.top().pos])*(ll)(i-stk.top().pos)*(ll)(le_cnt[stk.top().pos]+1);stk.pop();
    	    }
    		le_cnt[i]=i-(stk.empty()?1:stk.top().pos+1);stk.push(pack(i,array[i]));
    	}
    	while(!stk.empty()){
    		tot+=(ll)(array[stk.top().pos])*(ll)(n-stk.top().pos+1)*(ll)(le_cnt[stk.top().pos]+1);stk.pop();
    	}
    }
    void stk_push2(){
    	for(int i=1;i<=n;i++){
    		while(!stk.empty()&&stk.top().num>array[i]){
    		    tot-=(ll)(array[stk.top().pos])*(ll)(i-stk.top().pos)*(ll)(le_cnt[stk.top().pos]+1);stk.pop();
    	    }
    		le_cnt[i]=i-(stk.empty()?1:stk.top().pos+1);stk.push(pack(i,array[i]));
    	}
    	while(!stk.empty()){
    		tot-=(ll)(array[stk.top().pos])*(ll)(n-stk.top().pos+1)*(ll)(le_cnt[stk.top().pos]+1);stk.pop();
    	}
    }
    int main(){
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)scanf("%d",&array[i]);
    	stk_push1();stk_push2();printf("%lld",tot);
    	return 0;
    }
    
  • -13
    @ 2022-12-27 15:25:32
    scghscgdytvnschfv
    
    • 1

    信息

    ID
    1839
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    186
    已通过
    48
    上传者