답안 #577552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577552 2022-06-15T04:29:40 Z talant117408 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
51 ms 3568 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define long                unsigned long 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

void solve() {
    int n;
    cin >> n;
    
    ll mod = 1e9+7;
    function <ll(ll, ll)> add = [&](ll a, ll b) {
        return ((a+b)%mod+mod)%mod;
    };
    function <ll(ll, ll)> mult = [&](ll a, ll b) {
        return ((a*b)%mod+mod)%mod;
    };
    function <ll(ll, ll)> binpow = [&](ll a, ll b) {
        ll res = 1;
        while (b) {
            if (b&1) res = mult(res, a);
            a = mult(a, a);
            b >>= 1;
        }
        return res;
    };
    ll inv2 = binpow(2, mod-2);
    function <ll(ll)> calc = [&](ll n) {
        return mult(mult(n, n+1), inv2);
    };
    
    vector <ll> h(n), w(n);
    for (auto &to : h) cin >> to;
    for (auto &to : w) cin >> to;
    stack <pll> heights;
    ll ans = 0, sum = 0;
    for (int i = 0; i < n; i++) {
        ll left = 0;
        while (sz(heights) && heights.top().first >= h[i]) {
            auto v = heights.top(); heights.pop();
            left = add(left, v.second);
            sum = add(sum, -mult(calc(v.first), v.second));
        }
        ans = add(ans, mult(calc(h[i]), calc(w[i])));
        ans = add(ans, mult(calc(h[i]), mult(w[i], left)));
        ans = add(ans, mult(sum, w[i]));
        sum = add(sum, mult(calc(h[i]), add(w[i], left)));
        heights.push(mp(h[i], add(w[i], left)));
    }
    cout << ans << endl;
}

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 20 ms 1116 KB Output is correct
4 Correct 40 ms 1896 KB Output is correct
5 Correct 39 ms 1876 KB Output is correct
6 Correct 42 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 5 ms 468 KB Output is correct
3 Correct 24 ms 1108 KB Output is correct
4 Correct 51 ms 1876 KB Output is correct
5 Correct 48 ms 1876 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 7 ms 468 KB Output is correct
4 Correct 24 ms 1108 KB Output is correct
5 Correct 48 ms 1880 KB Output is correct
6 Correct 50 ms 1876 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 5 ms 596 KB Output is correct
9 Correct 22 ms 1364 KB Output is correct
10 Correct 40 ms 3420 KB Output is correct
11 Correct 43 ms 3464 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 19 ms 1108 KB Output is correct
12 Correct 38 ms 1892 KB Output is correct
13 Correct 39 ms 1876 KB Output is correct
14 Correct 38 ms 1876 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 468 KB Output is correct
17 Correct 23 ms 1108 KB Output is correct
18 Correct 49 ms 1876 KB Output is correct
19 Correct 50 ms 1876 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 5 ms 596 KB Output is correct
22 Correct 23 ms 1380 KB Output is correct
23 Correct 41 ms 3428 KB Output is correct
24 Correct 42 ms 3568 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 5 ms 468 KB Output is correct
31 Correct 6 ms 468 KB Output is correct
32 Correct 26 ms 1108 KB Output is correct
33 Correct 26 ms 1108 KB Output is correct
34 Correct 45 ms 1876 KB Output is correct
35 Correct 44 ms 1888 KB Output is correct
36 Correct 50 ms 1884 KB Output is correct
37 Correct 47 ms 1876 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 47 ms 1876 KB Output is correct
40 Correct 47 ms 1904 KB Output is correct
41 Correct 49 ms 2004 KB Output is correct
42 Correct 50 ms 2712 KB Output is correct