Submission #1282198

#TimeUsernameProblemLanguageResultExecution timeMemory
1282198anteknneFancy Fence (CEOI20_fancyfence)C++20
100 / 100
17 ms4088 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false

const int MAXN = 100 * 1000 + 17;
const ll MOD = 1000 * 1000 * 1000 + 7;
ll w[MAXN];
ll h[MAXN];
vector<int> v;
ll ile[MAXN];
ll spref[MAXN];

ll f (ll n) {
    return (n * (n + 1LL)/ 2) % MOD;
}

int main () {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i ++) {
        cin >> h[i];
    }

    for (int i = 1; i <= n; i ++) {
        cin >> w[i];
    }

    for (int i = 1; i <= n; i ++) {
        spref[i] = spref[i - 1] + w[i];
    }

    ll wyn = 0;
    ll poprz = 0;

    v.pb(0);
    for (int i = 1; i <= n; i ++) {
        if (h[i] < h[i - 1]) {
            while (!v.empty() && h[v.back()] > h[i]) {
                v.pop_back();
            }
            int j = v.back();

            poprz = ile[j];
            ll sw = spref[i - 1] - spref[j];
            sw %= MOD;
            poprz += f(h[i]) * sw;
            poprz %= MOD;
        }
     
        wyn += (w[i] - 1LL) * poprz;
        wyn %= MOD;
        poprz += f(h[i]) * w[i];
        poprz %= MOD;
        wyn += poprz;
        wyn += f(h[i]) * f(w[i] - 1);
        wyn %= MOD;

        v.pb(i);
        ile[i] = poprz;
        //cout << poprz << " " << wyn << "\n";
    }
    

    cout << wyn << "\n";
        
    return 0;
}
#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...