Submission #544178

#TimeUsernameProblemLanguageResultExecution timeMemory
544178tengiz05Fancy Fence (CEOI20_fancyfence)C++17
100 / 100
39 ms5976 KiB
#include <bits/stdc++.h>

using i64 = long long;

constexpr int P = 1E9 + 7, two = (P + 1) / 2;

i64 norm(i64 x) {
    if (x >= P) {
        x -= P;
    } else if (x < 0) {
        x += P;
    }
    return x;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<i64> h(n + 1), w(n + 1);
    for (int i = 1; i <= n; i++) {
        std::cin >> h[i];
    }
    for (int i = 1; i <= n; i++) {
        std::cin >> w[i];
        w[i] += w[i - 1];
    }
    
    std::vector<std::pair<i64, i64>> s;
    s.emplace_back(0, 0);
    
    i64 a = 0;
    i64 ans = 0;
    
    for (int i = 1; i <= n; i++) {
        while (s.back().first >= h[i]) {
            i64 x = (s.back().second - s[int(s.size()) - 2].second) % P;
            a = norm(a - x * (s.back().first * (s.back().first + 1) / 2 % P) % P);
            s.pop_back();
        }
        if (s.back().second != w[i - 1]) {
            i64 x = (w[i - 1] - s.back().second) % P;
            a = norm(a + x * (h[i] * (h[i] + 1) / 2 % P) % P);
            s.emplace_back(h[i], w[i - 1]);
        }
        i64 x = (w[i] - w[i - 1]) % P;
        i64 y = h[i];
        ans = norm(ans + a % P * x % P);
        ans = norm(ans + (x * (x + 1) % P * two % P) * (y * (y + 1) % P * two % P) % P);
        a = norm(a + (h[i] * (h[i] + 1) / 2 % P) * x % P);
        s.emplace_back(h[i], w[i]);
    }
    
    std::cout << ans << "\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...