Submission #533293

# Submission time Handle Problem Language Result Execution time Memory
533293 2022-03-05T09:59:25 Z ddy888 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
47 ms 8652 KB
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long 
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;  
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)

const int MOD = 1e9 + 7;
int N, H[100010], W[100010], par[100010], sz[100010], ans;
vector<pi> blocks, proc;

int root(int x) { return (par[x]==x)? x:par[x]=root(par[x]); }
bool same(int a, int b) { return root(a) == root(b); }
void merge(int a, int b) {
    if (same(a, b)) return;
    sz[root(b)] += sz[root(a)];
    par[root(a)] = root(b);
}
int getsize(int x) {return sz[root(x)];}

int binom(int x) {
    x %= MOD;
    return (x * (x + 1) / 2) % MOD;
}

int f(int x, int y) {
    return (binom(x) * binom(y)) % MOD;
}

signed main() {
    fast;
    cin >> N;
    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) {
        if (H[i] == H[i - 1]) proc.back().si += W[i];
        else proc.pb({H[i], W[i]});
    }
    int lim = proc.size();
    for (int i = 0; i < lim; ++i) blocks.pb({proc[i].fi, i});
    for (int i = 0; i < lim; ++i) par[i] = i, sz[i] = proc[i].si;
    sort(blocks.begin(), blocks.end(), [](pi a, pi b) {
        return a.fi > b.fi;
    });
    for (auto i: blocks) {
        if (i.si > 0 && proc[i.si].fi <= proc[i.si - 1].fi) {
            ans = ans - f(getsize(i.si - 1), proc[i.si].fi) + MOD; ans %= MOD;
            merge(i.si, i.si - 1);
        } 
        if (i.si < lim - 1 && proc[i.si].fi <= proc[i.si + 1].fi) {
            ans = ans - f(getsize(i.si + 1), proc[i.si].fi) + MOD; ans %= MOD;
            merge(i.si, i.si + 1);
        }
        ans += f(getsize(i.si), proc[i.si].fi);
        ans %= MOD;
    }
    cout << ans;
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 13 ms 2888 KB Output is correct
4 Correct 19 ms 4680 KB Output is correct
5 Correct 19 ms 5332 KB Output is correct
6 Correct 18 ms 4040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 9 ms 1100 KB Output is correct
4 Correct 18 ms 1832 KB Output is correct
5 Correct 22 ms 1852 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 11 ms 1228 KB Output is correct
5 Correct 26 ms 2004 KB Output is correct
6 Correct 30 ms 1896 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 1244 KB Output is correct
9 Correct 13 ms 3152 KB Output is correct
10 Correct 39 ms 8580 KB Output is correct
11 Correct 31 ms 8584 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 11 ms 2916 KB Output is correct
12 Correct 19 ms 4580 KB Output is correct
13 Correct 20 ms 5340 KB Output is correct
14 Correct 17 ms 4052 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 3 ms 612 KB Output is correct
17 Correct 10 ms 1992 KB Output is correct
18 Correct 21 ms 3724 KB Output is correct
19 Correct 23 ms 3888 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 3 ms 1236 KB Output is correct
22 Correct 13 ms 3156 KB Output is correct
23 Correct 28 ms 8512 KB Output is correct
24 Correct 29 ms 8644 KB Output is correct
25 Correct 0 ms 328 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 5 ms 1256 KB Output is correct
31 Correct 4 ms 1228 KB Output is correct
32 Correct 17 ms 4424 KB Output is correct
33 Correct 18 ms 4568 KB Output is correct
34 Correct 40 ms 8456 KB Output is correct
35 Correct 36 ms 8388 KB Output is correct
36 Correct 47 ms 8604 KB Output is correct
37 Correct 36 ms 8580 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 44 ms 8572 KB Output is correct
40 Correct 35 ms 8576 KB Output is correct
41 Correct 37 ms 8652 KB Output is correct
42 Correct 29 ms 8596 KB Output is correct