답안 #958911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958911 2024-04-07T04:04:23 Z TAhmed33 Fancy Fence (CEOI20_fancyfence) C++
100 / 100
32 ms 17748 KB
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int TWOINV = 5e8 + 4;
const int MAXN = 1e5 + 25;
int add (int a, int b) {
    a += b; if (a >= MOD) a -= MOD;
    return a;
}
int sub (int a, int b) {
    a -= b; if (a < 0) a += MOD;
    return a;
}
int mul (int a, int b) {
    return (a * 1ll * b) % MOD;
}
int h[MAXN], w[MAXN], n;
int nxt[MAXN], prv[MAXN], sze[MAXN];
vector <int> adj[MAXN];
bool comp (int x, int y) {
    return h[x] == h[y] ? x < y : h[x] < h[y];
}
int ans;
void dfs (int pos) {
    sze[pos] = w[pos];
    int x = mul(h[pos], mul(h[pos] + 1, TWOINV));
    for (auto j : adj[pos]) {
        dfs(j);
        ans = add(ans, mul(sze[j], mul(sze[pos], x)));
        sze[pos] = add(sze[pos], sze[j]);
    }
}
int main () {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n; memset(nxt, -1, sizeof(nxt)); memset(prv, -1, sizeof(prv));
    for (int i = 1; i <= n; i++) cin >> h[i];
    for (int i = 1; i <= n; i++) cin >> w[i];
    for (int i = 1; i <= n; i++) {
        int x = w[i], y = h[i];
        x = mul(x, mul(x + 1, TWOINV));
        y = mul(y, mul(y + 1, TWOINV));
        ans = add(ans, mul(x, y));
    }
    stack <int> cur;
    for (int i = 1; i <= n; i++) {
        while (!cur.empty() && comp(i, cur.top())) {
            nxt[cur.top()] = i;
            cur.pop();
        }
        cur.push(i);
    }
    while (!cur.empty()) cur.pop();
    for (int i = n; i >= 1; i--) {
        while (!cur.empty() && comp(i, cur.top())) {
            prv[cur.top()] = i;
            cur.pop();
        }
        cur.push(i);
    }
    while (!cur.empty()) cur.pop();
    int root = -1;
    for (int i = 1; i <= n; i++) {
        if (nxt[i] == -1 && prv[i] == -1) {
            root = i;
            continue;
        }
        if (nxt[i] == -1) {
            adj[prv[i]].push_back(i);
        } else if (prv[i] == -1) {
            adj[nxt[i]].push_back(i);
        } else if (!comp(nxt[i], prv[i])) {
            adj[nxt[i]].push_back(i);
        } else {
            adj[prv[i]].push_back(i);
        }
    }
    dfs(root);
    cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 11 ms 7516 KB Output is correct
4 Correct 22 ms 14676 KB Output is correct
5 Correct 22 ms 11344 KB Output is correct
6 Correct 20 ms 9308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4696 KB Output is correct
2 Correct 3 ms 5724 KB Output is correct
3 Correct 14 ms 11004 KB Output is correct
4 Correct 27 ms 17620 KB Output is correct
5 Correct 31 ms 17748 KB Output is correct
6 Correct 1 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 4 ms 5724 KB Output is correct
4 Correct 15 ms 11100 KB Output is correct
5 Correct 27 ms 17484 KB Output is correct
6 Correct 30 ms 17664 KB Output is correct
7 Correct 1 ms 4696 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 14 ms 11100 KB Output is correct
10 Correct 26 ms 17492 KB Output is correct
11 Correct 27 ms 17500 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 2 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 2 ms 4952 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 12 ms 7516 KB Output is correct
12 Correct 23 ms 14684 KB Output is correct
13 Correct 22 ms 11356 KB Output is correct
14 Correct 20 ms 9300 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 4 ms 5980 KB Output is correct
17 Correct 14 ms 11100 KB Output is correct
18 Correct 32 ms 17520 KB Output is correct
19 Correct 28 ms 17744 KB Output is correct
20 Correct 1 ms 4696 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 14 ms 11100 KB Output is correct
23 Correct 26 ms 17488 KB Output is correct
24 Correct 27 ms 17572 KB Output is correct
25 Correct 2 ms 4696 KB Output is correct
26 Correct 2 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 2 ms 4696 KB Output is correct
30 Correct 4 ms 4952 KB Output is correct
31 Correct 4 ms 4956 KB Output is correct
32 Correct 13 ms 6492 KB Output is correct
33 Correct 13 ms 6656 KB Output is correct
34 Correct 24 ms 8540 KB Output is correct
35 Correct 24 ms 8528 KB Output is correct
36 Correct 29 ms 8572 KB Output is correct
37 Correct 25 ms 8568 KB Output is correct
38 Correct 1 ms 4696 KB Output is correct
39 Correct 24 ms 9492 KB Output is correct
40 Correct 23 ms 9820 KB Output is correct
41 Correct 23 ms 11400 KB Output is correct
42 Correct 27 ms 17488 KB Output is correct