답안 #930377

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930377 2024-02-19T13:42:55 Z haxorman Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
70 ms 13488 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mxN = 1e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
const int MOD = 1e9 + 7;

int pref[mxN], nex[mxN], prv[mxN], seg[4 * mxN], lazy[4 * mxN];
pair<int,int> arr[mxN];

void push(int ind, int l, int r) {
    if (lazy[ind]) {
        seg[ind] = (r - l + 1) * lazy[ind] % MOD;
        if (l != r) {
            lazy[2*ind] = lazy[ind];
            lazy[2*ind+1] = lazy[ind];
        }
        lazy[ind] = 0;
    }
}

void update(int lo, int hi, int val, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind, l, r);
    if (lo > r || l > hi) {
        return;
    }

    if (lo <= l && r <= hi) {
        lazy[ind] = val;
        push(ind, l, r);
        return;
    }

    int mid = (l + r) / 2;
    update(lo, hi, val, 2*ind, l, mid);
    update(lo, hi, val, 2*ind+1, mid+1, r);
}

int query(int pos, int ind = 1, int l = 0, int r = SZ - 1) {
    push(ind, l, r);
    if (l == r) {
        return seg[ind];
    }

    int mid = (l + r) / 2;
    if (pos <= mid) {
        return query(pos, 2*ind, l, mid);
    }
    else {
        return query(pos, 2*ind+1, mid+1, r);
    }
}

int nc2(int n) {
    return n * (n - 1) / 2;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    int n;
    cin >> n;
    
    vector<pair<int,int>> pos;
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i].first;
        pos.push_back({arr[i].first, i});
    }
    sort(pos.begin(), pos.end());

    for (int i = 1; i <= n; ++i) {
        cin >> arr[i].second;
        pref[i] = pref[i - 1] + arr[i].second;
    }
    
    vector<int> st = {n + 1};
    for (int i = n; i >= 1; --i) {
        while (st.size() && arr[i].first <= arr[st.back()].first) {
            st.pop_back();
        }
        assert(st.size());
        nex[i] = st.back();
        st.push_back(i);
    }
    
    st = {0};
    for (int i = 1; i <= n; ++i) {
        while (st.size() && arr[i].first <= arr[st.back()].first) {
            st.pop_back();
        }
        assert(st.size());
        prv[i] = st.back();
        st.push_back(i);
    }

    int ans = 0;
    for (auto [h, i] : pos) {
        int sub = query(i); 
        int w = (pref[nex[i] - 1] - pref[prv[i]]) % MOD;

        ans += (nc2(h + 1) - sub) % MOD * (nc2(w + 1) % MOD) % MOD;
        ans %= MOD;

        update(prv[i] + 1, nex[i] - 1, nc2(h + 1) % MOD);
    }
    cout << ans << "\n";
}

Compilation message

fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:99:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   99 |     for (auto [h, i] : pos) {
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2512 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 26 ms 8144 KB Output is correct
4 Correct 54 ms 11476 KB Output is correct
5 Correct 50 ms 11460 KB Output is correct
6 Correct 46 ms 11560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 6 ms 3296 KB Output is correct
3 Correct 28 ms 7640 KB Output is correct
4 Correct 56 ms 10440 KB Output is correct
5 Correct 63 ms 10528 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 6 ms 3400 KB Output is correct
4 Correct 28 ms 7620 KB Output is correct
5 Correct 57 ms 10440 KB Output is correct
6 Correct 58 ms 10432 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 6 ms 3552 KB Output is correct
9 Correct 28 ms 7984 KB Output is correct
10 Correct 61 ms 12136 KB Output is correct
11 Correct 56 ms 12240 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2904 KB Output is correct
10 Correct 1 ms 2520 KB Output is correct
11 Correct 1 ms 2520 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2472 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 26 ms 8160 KB Output is correct
12 Correct 55 ms 11208 KB Output is correct
13 Correct 50 ms 11236 KB Output is correct
14 Correct 46 ms 11436 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 6 ms 3552 KB Output is correct
17 Correct 28 ms 8292 KB Output is correct
18 Correct 56 ms 11716 KB Output is correct
19 Correct 60 ms 11876 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 6 ms 3704 KB Output is correct
22 Correct 33 ms 8736 KB Output is correct
23 Correct 56 ms 13488 KB Output is correct
24 Correct 70 ms 13416 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 7 ms 3552 KB Output is correct
31 Correct 7 ms 3552 KB Output is correct
32 Correct 31 ms 8464 KB Output is correct
33 Correct 32 ms 8404 KB Output is correct
34 Correct 63 ms 12208 KB Output is correct
35 Correct 61 ms 11524 KB Output is correct
36 Correct 70 ms 11824 KB Output is correct
37 Correct 67 ms 11720 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 66 ms 11836 KB Output is correct
40 Correct 68 ms 12336 KB Output is correct
41 Correct 60 ms 11972 KB Output is correct
42 Correct 58 ms 13252 KB Output is correct