답안 #1091109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091109 2024-09-19T20:33:08 Z DaciejMaciej Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
21 ms 6260 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

template <class T>

using VV = vector<vector<T>>;

using VI = vector<int>;
using VVI = vector<vector<int>>;

using VL = vector<long long>;
using VVL = vector<vector<long long>>;

using VC = vector<char>;
using VVC = vector<vector<char>>;

using VB = vector<bool>;
using VVB = vector<vector<bool>>;

using VD = vector<double>;
using VVD = vector<vector<double>>;

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;

using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;

#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())

#define debug(x) cerr << #x << " = " << x << endl;

// zero indexed
template <class T>
struct segtree
{
    const T def = 0;
    int n;
    vector<T> seg;

    segtree(int _size) : n(_size), seg(2 * _size, def) {}

    T merge(T a, T b)
    {
        return max(a, b);
    }
    void update(int pos, T val)
    {
        for (seg[pos += n] = val; pos /= 2;)
        {
            seg[pos] = merge(seg[pos * 2], seg[pos * 2 + 1]);
        }
    }
    T query(int l, int r)
    { // get [l, r]
        T a = def, b = def;
        for (l += n, r += n + 1; l < r; l /= 2, r /= 2)
        {
            if (l % 2)
                a = merge(a, seg[l++]);
            if (r % 2)
                b = merge(b, seg[--r]);
        }
        return merge(a, b);
    }
};

const ll MOD = 1e9 + 7;

template <class T>
void maxi(T &a, T b)
{
    a = max(a, b);
}

template <class T>
void mini(T &a, T b)
{
    a = min(a, b);
}

template <class T>
void addi(T &a, T b)
{
    a = (a + b) % MOD;
}

template <class T>
void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}

template <class T>
T add(T a, T b)
{
    return (a + b) % MOD;
}

template <class T>
T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}

template <class T>
T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}

ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }

    return res;
}

const int INF = 1e9;

const int MAX_N = 1e5 + 3;

struct SEG
{
    ll h, w, sum;
};

void solve()
{
    int n;
    cin >> n;

    VL h(n);
    VL w(n);
    FOR(i, 0, n)
        cin >> h[i];
    FOR(i, 0, n)
        cin >> w[i];

    stack<SEG> st;
    ll ans = 0;
    ll inv = binpow(2, MOD - 2, MOD);

    FOR(i, 0, n)
    {
        ll w_sum = w[i];
        ll h_val = mul(mul(h[i], h[i] + 1), inv); // ways of choosing from height on i
        ll w_val = mul(mul(w[i], w[i] + 1), inv);


        // adding for bigger than h[i]
        while (sz(st) && st.top().h >= h[i])
        {
            auto [h_seg, w_seg, _] = st.top();

            ll inter = mul(w_seg, mul(w[i], h_val));
            addi(ans, inter);

            addi(w_sum, w_seg);
            st.pop();
        }
        //cerr << ans << LINE;

        // adding only for current rectangle
        ll t1 =  mul(h_val, w_val);
        //cerr << "ADDED: " << t1 << LINE;
        addi(ans, t1);



        // if there are smaller on the left
        ll new_sum = 0;
        if(sz(st)) {
            auto [h_seg, w_seg, sum_seg] = st.top();

            // calc all intersection for smaller rectangles on the left
            ll t2 = mul(sum_seg, w[i]); 
            addi(ans, t2);

            new_sum = sum_seg;
        }

        addi(new_sum, mul(w_sum, h_val));
   
        //cerr << "PUSHED TO TOP: " << h[i] << SPACE << w_sum << SPACE << new_sum << LINE;
        st.push({h[i], w_sum, new_sum});
        //cerr << ans << LINE;
        //cerr << "NEW" << LINE << LINE;
    }

    cout << ans << LINE;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 1 ms 456 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 0 ms 348 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 7 ms 1628 KB Output is correct
4 Correct 13 ms 3176 KB Output is correct
5 Correct 14 ms 3160 KB Output is correct
6 Correct 14 ms 3032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 8 ms 2236 KB Output is correct
4 Correct 17 ms 3944 KB Output is correct
5 Correct 18 ms 3936 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 456 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 12 ms 2008 KB Output is correct
5 Correct 17 ms 3816 KB Output is correct
6 Correct 17 ms 4080 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 2 ms 816 KB Output is correct
9 Correct 9 ms 2656 KB Output is correct
10 Correct 17 ms 6236 KB Output is correct
11 Correct 17 ms 6248 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 464 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 612 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 7 ms 1632 KB Output is correct
12 Correct 14 ms 3212 KB Output is correct
13 Correct 13 ms 3168 KB Output is correct
14 Correct 14 ms 3164 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 2 ms 728 KB Output is correct
17 Correct 8 ms 2140 KB Output is correct
18 Correct 17 ms 3924 KB Output is correct
19 Correct 18 ms 3948 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 864 KB Output is correct
22 Correct 9 ms 2752 KB Output is correct
23 Correct 18 ms 6260 KB Output is correct
24 Correct 18 ms 6236 KB Output is correct
25 Correct 1 ms 356 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 352 KB Output is correct
28 Correct 0 ms 352 KB Output is correct
29 Correct 0 ms 496 KB Output is correct
30 Correct 2 ms 612 KB Output is correct
31 Correct 2 ms 616 KB Output is correct
32 Correct 8 ms 1892 KB Output is correct
33 Correct 9 ms 2148 KB Output is correct
34 Correct 17 ms 3572 KB Output is correct
35 Correct 16 ms 3688 KB Output is correct
36 Correct 17 ms 3940 KB Output is correct
37 Correct 18 ms 3960 KB Output is correct
38 Correct 0 ms 360 KB Output is correct
39 Correct 21 ms 3940 KB Output is correct
40 Correct 17 ms 3940 KB Output is correct
41 Correct 17 ms 4084 KB Output is correct
42 Correct 17 ms 4964 KB Output is correct