Submission #430965

# Submission time Handle Problem Language Result Execution time Memory
430965 2021-06-17T08:26:29 Z Pety Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
52 ms 4196 KB
#include <bits/stdc++.h>
     
using namespace std;

#define ll long long

const int mod = 1e9+7;

int n;
stack<int>st;
int h[100002], w[100002], dp[100002];
int sum[100002];

int logpow(int a, int b) {
  int ans = 1, d = a;
  while (b) {
    if (b & 1)
      ans = 1LL * ans * d % mod;
    b >>= 1;
    d = 1LL * d * d % mod;
  }
  return ans;
}

int calc (int w, int h) {
  ll a1 = 1ll * w * (w + 1) / 2 % mod;
  ll a2 = 1ll * h * (h + 1) / 2 % mod;
  return 1ll * a1 * a2 % mod;
}


int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  st.push(0);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    while (h[st.top()] >= h[i]) {
      st.pop();
    }
    sum[i] = sum[i - 1] + w[i];
    if (sum[i] >= mod)
      sum[i] -= mod;
    int x = st.top();
    st.push(i);
    if (x) {
      int val = dp[x] - calc(w[x], h[x]);
      if (val < 0)
        val += mod;
      val = 1ll * val * logpow(w[x], mod - 2) % mod;
      dp[i] = 1ll * val * w[i] % mod; 
      int a1 = 1ll * h[x] * (h[x] + 1) / 2 % mod;
      dp[i] = dp[i] + 1ll * w[x] * w[i] % mod * a1 % mod;
      if (dp[i] >= mod)
        dp[i] -= mod;
    }
    int a1 = 1ll * h[i] * (h[i] + 1) / 2 % mod;
    dp[i] = dp[i] + 1ll * w[i] * (sum[i - 1] - sum[x] + mod) % mod * a1 % mod;
    if (dp[i] >= mod)
      dp[i] -= mod;
    dp[i] = dp[i] + calc(w[i], h[i]);
    if (dp[i] >= mod)
      dp[i] -= mod;
    ans = (ans + dp[i]);
    if (ans >= mod)
      ans -= mod;
  }
  cout << ans;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 19 ms 1644 KB Output is correct
4 Correct 31 ms 2928 KB Output is correct
5 Correct 37 ms 2924 KB Output is correct
6 Correct 44 ms 3008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 16 ms 1052 KB Output is correct
4 Correct 29 ms 1784 KB Output is correct
5 Correct 33 ms 1744 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 16 ms 1032 KB Output is correct
5 Correct 28 ms 1752 KB Output is correct
6 Correct 31 ms 1852 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 6 ms 716 KB Output is correct
9 Correct 27 ms 2108 KB Output is correct
10 Correct 51 ms 4052 KB Output is correct
11 Correct 51 ms 4196 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 19 ms 1584 KB Output is correct
12 Correct 35 ms 2940 KB Output is correct
13 Correct 37 ms 3012 KB Output is correct
14 Correct 44 ms 2924 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 16 ms 2072 KB Output is correct
18 Correct 30 ms 3732 KB Output is correct
19 Correct 33 ms 3844 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 6 ms 716 KB Output is correct
22 Correct 26 ms 2124 KB Output is correct
23 Correct 48 ms 4104 KB Output is correct
24 Correct 49 ms 4084 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 6 ms 588 KB Output is correct
31 Correct 6 ms 628 KB Output is correct
32 Correct 27 ms 1992 KB Output is correct
33 Correct 27 ms 1984 KB Output is correct
34 Correct 50 ms 3600 KB Output is correct
35 Correct 49 ms 3512 KB Output is correct
36 Correct 52 ms 3764 KB Output is correct
37 Correct 51 ms 3804 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 52 ms 3700 KB Output is correct
40 Correct 49 ms 3784 KB Output is correct
41 Correct 50 ms 3788 KB Output is correct
42 Correct 40 ms 3904 KB Output is correct