Submission #1231382

#TimeUsernameProblemLanguageResultExecution timeMemory
1231382tminhFancy Fence (CEOI20_fancyfence)C++20
0 / 100
0 ms328 KiB
#include "bits/stdc++.h"
using namespace std;

#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>


#define ep emplace_back
#define pb push_back
#define pf push_front

const ll mod = 1e9 + 7;
const int N = 1e5 + 5;
const ll inv2 = 500000004;

int n;
ll h[N], w[N], pre[N];
stack<ll> s;
int l[N], r[N];

ll S(ll n) {
    n %= mod;
    ll res = n;
    res = (res * (n + 1)) % mod;
    res = (res * inv2) % mod;
    return res;
}

void solve() {
    for (int i = 1; i <= n; ++i) pre[i] = (pre[i - 1] + w[i]);

    for (int i = 1; i <= n; ++i) {
        while (!s.empty() && h[s.top()] >= h[i]) s.pop();
        l[i] = (s.empty() ? 0 : s.top());
        s.push(i);
    }
    while (!s.empty()) s.pop();

    for (int i = n; i >= 1; --i) {
        while (!s.empty() && h[s.top()] >= h[i]) s.pop();
        r[i] = (s.empty() ? n + 1 : s.top());
        s.push(i);
    }

    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
        ll W = (pre[r[i] - 1] - pre[l[i]]);
       
        ll prev_h = max(h[l[i]], h[r[i]]);
        ll H = h[i] - prev_h;
         
        ll count_W = S(W);
        ll count_H_total = S(H);        
        ll count_H_new = (count_H_total + prev_h * H) % mod;
        
        ll contribution = (count_W * count_H_new) % mod;
        ans = (ans + contribution) % mod;
    }
    cout << ans << endl;
}

void input() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> h[i];
    for (int i = 1; i <= n; ++i) cin >> w[i];
    return solve();
}

int main() {
    if(fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    input();
    
    
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
    return 0;
}

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...