답안 #907523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
907523 2024-01-15T19:15:42 Z sysia Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
34 ms 9568 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 1e9+7;
const int I = (mod+1)/2;

int mul(int a, int b){
    return (a*b)%mod;
}

int C2(int x){
    return mul(x, mul(x-1, I));
}

void add(int &a, int b){
    a += b;
    if (a >= mod) a-=mod;
}

int add2(int a, int b){
    a += b;
    if (a >= mod) a-=mod;
    return a;
}

int sub2(int a, int b){
    a -= b;
    if (a < 0) a += mod;
    return a;
}

struct DSU{
    vector<int>rep, mn, mx, sz;

    DSU(int n){
        rep.resize(n+1);
        iota(rep.begin(), rep.end(), 0);
        mx = rep; mn = rep;
        sz.assign(n+1, 1);
    }

    T get(int x){
        x = f(x);
        return {mn[x], mx[x]};
    }

    int f(int a){
        return a == rep[a] ? a : rep[a] = f(rep[a]);
    }

    bool u(int a, int b){
        a = f(a);b = f(b);
        if (a == b) return false;
        if (sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        mn[a] = min(mn[a], mn[b]);
        mx[a] = max(mx[a], mx[b]);
        rep[b] = a;
        return true;
    }
};


void solve(){
    int n; cin >> n;
    vector<int>h(n+1), w(n+1), pref(n+1);
    vector<T>s;
    for (int i = 1; i<=n; i++) {
        cin >> h[i];
        s.emplace_back(h[i], i);
    }
    DSU dsu(n+2);
    for (int i = 1; i<=n; i++) {
        cin >> w[i];
        pref[i] = add2(pref[i-1], w[i]);
    }
    sort(s.rbegin(), s.rend());
    int ans = 0;
    vector<bool>vis(n+1);
    auto sum = [&](int l, int r){
        return sub2(pref[r], pref[l-1]);
    };
    for (auto [h, i]: s){
        int tmp = C2(w[i]+1);
        if (i + 1 <= n && vis[i+1]) dsu.u(i, i+1);
        if (i - 1 >= 1 && vis[i-1]) dsu.u(i, i-1);
        vis[i] = 1;
        auto [L, R] = dsu.get(i);
        add(tmp, mul(w[i], sub2(sum(L, R), w[i])));
        add(tmp, mul(sum(L, i-1), sum(i+1, R)));
        add(ans, mul(C2(h+1), tmp));
    }
    cout << ans << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 16 ms 4448 KB Output is correct
4 Correct 23 ms 8660 KB Output is correct
5 Correct 25 ms 8656 KB Output is correct
6 Correct 22 ms 8660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 3 ms 1240 KB Output is correct
3 Correct 12 ms 4824 KB Output is correct
4 Correct 23 ms 9284 KB Output is correct
5 Correct 24 ms 9512 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 416 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 12 ms 4744 KB Output is correct
5 Correct 25 ms 9420 KB Output is correct
6 Correct 26 ms 9568 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 1240 KB Output is correct
9 Correct 12 ms 4824 KB Output is correct
10 Correct 23 ms 9160 KB Output is correct
11 Correct 23 ms 9428 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 460 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 14 ms 4568 KB Output is correct
12 Correct 25 ms 8708 KB Output is correct
13 Correct 24 ms 8656 KB Output is correct
14 Correct 23 ms 8944 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 1240 KB Output is correct
17 Correct 12 ms 4884 KB Output is correct
18 Correct 22 ms 9424 KB Output is correct
19 Correct 24 ms 9328 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 3 ms 1240 KB Output is correct
22 Correct 12 ms 5012 KB Output is correct
23 Correct 23 ms 9164 KB Output is correct
24 Correct 24 ms 9416 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 3 ms 1376 KB Output is correct
31 Correct 4 ms 1240 KB Output is correct
32 Correct 17 ms 4688 KB Output is correct
33 Correct 16 ms 4824 KB Output is correct
34 Correct 32 ms 9260 KB Output is correct
35 Correct 32 ms 9172 KB Output is correct
36 Correct 31 ms 9428 KB Output is correct
37 Correct 33 ms 9416 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 31 ms 9428 KB Output is correct
40 Correct 34 ms 9396 KB Output is correct
41 Correct 26 ms 9428 KB Output is correct
42 Correct 24 ms 9428 KB Output is correct