답안 #942526

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942526 2024-03-10T19:40:39 Z batsukh2006 Fancy Fence (CEOI20_fancyfence) C++17
15 / 100
22 ms 4360 KB
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map> 
 
using namespace std;
 
#define MOD 1000000007
#define int long long
#define endl '\n'
int cal(int a, int b){
    int f=(a*(a+1))%MOD*((MOD+1)/2)%MOD;
    int s=(b*(b+1))%MOD*((MOD+1)/2)%MOD;
    return (f*s)%MOD;
}
void solve(){
    int n; cin>>n;
    vector<int> pref(n+1);
    vector<int> h(n+1),w(n+1);
    for(int i=1; i<=n; i++) cin>>h[i];
    for(int i=1; i<=n; i++) cin>>w[i];
    vector<int> up(n+1),dw(n+2);
    for(int i=1; i<=n; i++) up[i]=(up[i-1]+w[i])%MOD;
    for(int i=n; i>=1; i--) dw[i]=(dw[i+1]+w[i])%MOD;
    stack<int> f;
    f.push(0);
    int ans=0;
    for(int i=1; i<=n; i++){
        int pos=i-1;
        while(f.size()>1&&h[f.top()]>=h[i]){
            f.pop();
            ans=(ans-cal(up[pos]-up[f.top()],h[i])+MOD)%MOD;
            pos=f.top();
        }
        ans=(ans+cal(up[i]-up[f.top()],h[i]))%MOD;
        f.push(i);
    }
    stack<int> s;
    s.push(n+1);
    for(int i=n; i>=1; i--){
        int cur=0,sum=0;
        int pos=i+1,mark=i;
        while(s.size()>1&&h[s.top()]>=h[i]){
            int num=dw[mark]-dw[s.top()];
            sum=(sum+((num*(num+1))%MOD*((MOD+1)/2)%MOD))%MOD;
            mark=s.top();
            s.pop();
            cur=(cur+cal(dw[pos]-dw[s.top()],h[i]))%MOD;
            pos=s.top();
        }
        int num=dw[mark]-dw[s.top()];
        sum=(sum+((num*(num+1))%MOD*((MOD+1)/2)%MOD))%MOD;
        if(h[s.top()-1]!=h[i]){
            ans=(ans-cur+MOD)%MOD;
            ans=(ans-cal(w[i],h[i])+MOD)%MOD;
            ans=(ans+cal(dw[i]-dw[s.top()],h[i]))%MOD;
            ans=(ans-cal(dw[i]-dw[s.top()],h[s.top()])+MOD)%MOD;
            ans=(ans+(sum*(h[s.top()]*(h[s.top()]+1)%MOD*((MOD+1)/2)%MOD))%MOD)%MOD;
        }
        s.push(i);
    }
    cout<<ans;
}
signed main(){
	// freopen("hps.in", "r", stdin);
	// freopen("hps.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int t=1;
	// cin>>t;
	while(t--){
		solve();
		cout<<endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 3 ms 604 KB Output is correct
3 Correct 11 ms 2396 KB Output is correct
4 Correct 21 ms 4360 KB Output is correct
5 Correct 22 ms 4188 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -