Submission #1008067

# Submission time Handle Problem Language Result Execution time Memory
1008067 2024-06-26T07:18:56 Z andecaandeci Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
45 ms 7896 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 5;
const ll LOG = 31;
#define vll vector <ll>
#define pll pair <ll, ll>
#define fi first
#define se second
#define endl '\n'

ll n, m;
ll a [MAXN], b [MAXN], pf [MAXN];
vll v;

// V(x, y) = dep[x] + dep[y] - 2*dep[lca]
// cari V(x, y) terkecil ke (1 - 10^5)

ll sum(ll x, ll y){return ((x%MOD)+(y%MOD))%MOD;}
ll sub(ll x, ll y){return ((x%MOD)-(y%MOD) + 2*MOD) % MOD;}
ll mul(ll x, ll y){return ((x%MOD)*(y%MOD))%MOD;}
ll pwr(ll x, ll y){
    if(y == 0) return 1;
    ll ret = pwr(x, y/2);
    ret = mul(ret, ret);
    return (y & 1) ? mul(ret, x) : ret;
}
ll dvi(ll x, ll y){return mul(x, pwr(y, MOD-2));}

ll f(ll x){return dvi(mul(x, x+1), 2);}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(ll i = 1; i <= n; i++) cin >> a[i];
    for(ll i = 1; i <= n; i++){
        cin >> b[i];
        pf[i] = pf[i-1] + b[i];
    }
    ll ans = 0;
    for(ll i = 1; i <= n+1; i++){
        while(!v.empty()){
            if(a[v.back()] < a[i]) break;
            ll h = a[v.back()], tes = a[i];
            ll w = pf[i-1];
            if(v.size() >= 2){
                tes = max(tes, a[v.end()[-2]]);
                w -= pf[v.end()[-2]];
            }
            // cout << h << "," << tes << " " << w << " << " << mul(sub(f(h), f(tes)), f(w)) << endl;
            ans = sum(ans, mul(sub(f(h), f(tes)), f(w)));
            v.pop_back();
        }
        v.push_back(i);
    }
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4556 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 18 ms 5320 KB Output is correct
4 Correct 35 ms 5724 KB Output is correct
5 Correct 38 ms 5720 KB Output is correct
6 Correct 36 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 6 ms 4700 KB Output is correct
3 Correct 21 ms 5464 KB Output is correct
4 Correct 38 ms 6488 KB Output is correct
5 Correct 39 ms 6748 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 4 ms 4700 KB Output is correct
4 Correct 18 ms 5712 KB Output is correct
5 Correct 33 ms 6492 KB Output is correct
6 Correct 45 ms 6756 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 5 ms 4956 KB Output is correct
9 Correct 19 ms 5980 KB Output is correct
10 Correct 41 ms 7644 KB Output is correct
11 Correct 38 ms 7640 KB Output is correct
12 Correct 1 ms 4696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4556 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 18 ms 5212 KB Output is correct
12 Correct 35 ms 5724 KB Output is correct
13 Correct 35 ms 5724 KB Output is correct
14 Correct 37 ms 5720 KB Output is correct
15 Correct 1 ms 4592 KB Output is correct
16 Correct 4 ms 4700 KB Output is correct
17 Correct 20 ms 5716 KB Output is correct
18 Correct 39 ms 6620 KB Output is correct
19 Correct 42 ms 6872 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 5 ms 4956 KB Output is correct
22 Correct 25 ms 5952 KB Output is correct
23 Correct 39 ms 7896 KB Output is correct
24 Correct 42 ms 7644 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 2 ms 4592 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 6 ms 4840 KB Output is correct
31 Correct 5 ms 4840 KB Output is correct
32 Correct 20 ms 5468 KB Output is correct
33 Correct 23 ms 5616 KB Output is correct
34 Correct 40 ms 6460 KB Output is correct
35 Correct 40 ms 6368 KB Output is correct
36 Correct 40 ms 6492 KB Output is correct
37 Correct 39 ms 6620 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 42 ms 6648 KB Output is correct
40 Correct 42 ms 6492 KB Output is correct
41 Correct 44 ms 6872 KB Output is correct
42 Correct 39 ms 7244 KB Output is correct