Submission #710118

# Submission time Handle Problem Language Result Execution time Memory
710118 2023-03-15T04:52:23 Z Spade1 Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
27 ms 3536 KB
#include<bits/stdc++.h>
//#include "grader.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;

const int N = 1e5 + 10;
const int M = 1e9+7;

ll h[N], w[N];

stack<pll> s;

ll ans;
ll add(ll a, ll b) {return ((((a%M)+(b%M))%M)+M)%M;}
ll mul(ll a, ll b) {return ((a%M)*(b%M))%M;}

ll power(ll a, ll b) {
    if (b==0) return 1;
    ll ret = power(a, b/2);
    if (b&1) return mul(ret, mul(ret, a));
    else return mul(ret, ret);
}

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

    s.push({0, 0});
    ll inv = power(2, M-2);
    for (int i = 1; i <= n; ++i) {
        while (s.top().st > h[i]) {
            auto [H, W] = s.top(); s.pop();
            if (s.top().st > h[i]) {
                H -= s.top().st;
                auto [HH, WW] = s.top(); s.pop();
                s.push({HH, WW + W});
                ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
            }
            else {
                H -= h[i];
                s.push({h[i], W});
                ans = add(ans, mul(mul(H, h[i]), mul(mul(W, W+1), inv)));
            }
            ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
        }
        if (s.top().st == h[i]) {
            auto [HH, WW] = s.top(); s.pop();
            s.push({HH, WW + w[i]});
        }
        else
            s.push({h[i], w[i]});
    }
    while (s.size() > 1) {
        auto [H, W] = s.top(); s.pop();
        H -= s.top().st;
        auto [HH, WW] = s.top(); s.pop();
        s.push({HH, WW + W});
        ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
        ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Compilation message

fancyfence.cpp: In function 'void solve()':
fancyfence.cpp:40:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |             auto [H, W] = s.top(); s.pop();
      |                  ^
fancyfence.cpp:43:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |                 auto [HH, WW] = s.top(); s.pop();
      |                      ^
fancyfence.cpp:55:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   55 |             auto [HH, WW] = s.top(); s.pop();
      |                  ^
fancyfence.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |         auto [H, W] = s.top(); s.pop();
      |              ^
fancyfence.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   64 |         auto [HH, WW] = s.top(); s.pop();
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
5 Correct 0 ms 224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 9 ms 1236 KB Output is correct
4 Correct 17 ms 2132 KB Output is correct
5 Correct 17 ms 2260 KB Output is correct
6 Correct 17 ms 2000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 12 ms 1108 KB Output is correct
4 Correct 20 ms 1852 KB Output is correct
5 Correct 22 ms 1876 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 10 ms 1108 KB Output is correct
5 Correct 19 ms 1848 KB Output is correct
6 Correct 27 ms 1844 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 11 ms 1440 KB Output is correct
10 Correct 23 ms 3532 KB Output is correct
11 Correct 23 ms 3536 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 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 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 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 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 9 ms 1264 KB Output is correct
12 Correct 17 ms 2132 KB Output is correct
13 Correct 18 ms 2192 KB Output is correct
14 Correct 16 ms 1920 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 10 ms 1108 KB Output is correct
18 Correct 19 ms 1876 KB Output is correct
19 Correct 21 ms 1876 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 3 ms 596 KB Output is correct
22 Correct 12 ms 1492 KB Output is correct
23 Correct 23 ms 3408 KB Output is correct
24 Correct 27 ms 3424 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 11 ms 1016 KB Output is correct
33 Correct 13 ms 1108 KB Output is correct
34 Correct 22 ms 1832 KB Output is correct
35 Correct 23 ms 1876 KB Output is correct
36 Correct 25 ms 1816 KB Output is correct
37 Correct 23 ms 1876 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 24 ms 1788 KB Output is correct
40 Correct 23 ms 1876 KB Output is correct
41 Correct 23 ms 1964 KB Output is correct
42 Correct 24 ms 2616 KB Output is correct