제출 #1283674

#제출 시각아이디문제언어결과실행 시간메모리
1283674HiepVu217Fancy Fence (CEOI20_fancyfence)C++20
100 / 100
28 ms4448 KiB
//Proud of You//
#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define int long long
using namespace std;
const int N = 1e5 + 17, M = 1e9 + 7;
int n, r[N], w[N], m[N], d2, ans;
pair <int, int> a[N];
bool ex[N], vi[N];
inline int lt (long long a, long long b)
{
    a %= M;
    if (!b)
    {
        return 1;
    }
    if (b == 1)
    {
        return a;
    }
    int z = lt (a, b / 2);
    z = 1LL * z * z % M;
    if (b & 1)
    {
        z = 1LL * z * a % M;
    }
    return z;
}
inline int c2n (int x)
{
    return 1LL * x * (x - 1) % M * d2 % M;
}
inline int root (int u)
{
    if (u == r[u])
    {
        return u;
    }
    return r[u] = root(r[u]);
}
inline bool join (int x, int y)
{
    x = root(x), y = root(y);
    if (x == y)
    {
        return 0;
    }
    r[y] = x, ans = (ans + 1LL * ((m[x] - m[y]) * m[x] % M - c2n (m[x] - m[y]) + M) * c2n (w[x] + 1)) % M;
    w[x] = (w[x] + w[y]) % M, m[x] = min (m[x], m[y]);
    return 1;
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1, h; i <= n; ++i)
    {
        cin >> h;
        a[i] = {h, i}, m[i] = h;
    }
    for (int i = 1; i <= n; ++i)
    {
        cin >> w[i];
        r[i] = i;
    }
    sort (a + 1, a + n + 1, greater<>());
    d2 = lt (2, M - 2);
    for (int j = 1; j <= n; ++j)
    {
        auto [h, i] = a[j];
        ex[i] = 1;
        if (ex[i - 1])
        {
            join (i - 1, i);
        }
        if (ex[i + 1])
        {
            join (i + 1, i);
        }
    }
    for (int i = 1; i <= n; ++i)
    {
        int r = root(i);
        if (!vi[r])
        {
            vi[r] = 1;
            ans = (ans + 1LL * c2n (m[r] + 1) * c2n (w[r] + 1)) % M;
        }
    }
    cout << ans;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...