Submission #340195

# Submission time Handle Problem Language Result Execution time Memory
340195 2020-12-27T09:20:34 Z blue Fancy Fence (CEOI20_fancyfence) C++11
100 / 100
373 ms 18884 KB
#include <iostream>
#include <algorithm>
using namespace std;

/*
Sort all the rectangles by height.
Repeat:
    Look at the tallest rectangle with height H and width W.
    Let h be the larger height among its neighbors.
    Count all fancy rectangles whose top edge is in the range [h+1, H] = (W*(W+1)/2) * ((H-h)*(H-h+1)/2)
    Reduce the height of the tallest rectangle to h and merge it with the right neighbors

Reverse the above algorithm

Solution
Repeat:
    Look at the lowest rectangle with height H
    Find the rectangles to the left and right closest to this rectangle which have height strictly less than H.
        (Sparse table + Binary search)
    Let the total width bounded between (not inside either rectangle) these rectangles be W
    Add (Hchoose2 + Hchoose1) * (Wchoose2 + Wchoose1)
*/

long long N;
long long h[100002][18];
long long w[100001];
long long l2[100002];
long long rect[100001];

long long mod = 1e9 + 7;

long long hmin(long long l, long long r)
{
    return min(h[l][l2[r-l+1]], h[r - (1LL << l2[r-l+1]) + 1][l2[r-l+1]]);
}

int main()
{
    cin >> N;

    h[0][0] = h[N+1][0] = 0;
    for(long long i = 1; i <= N; i++)
    {
        cin >> h[i][0];
    }
    for(long long j = 1; j <= 17; j++)
    {
        for(long long i = 1; i + (1LL << j) - 1 <= N; i++)
        {
            h[i][j] = min(h[i][j-1], h[i + (1LL << (j-1))][j-1]);
        }
    }

    w[0] = 0;
    for(long long i = 1; i <= N; i++)
    {
        cin >> w[i];
        w[i] = (w[i] + w[i-1]) % mod;
    }

    l2[1] = 0;
    for(long long i = 2; i <= N; i++) l2[i] = l2[i/2] + 1;

    for(long long i = 0; i <= N; i++) rect[i] = i;
    sort(rect+1, rect+N+1,
        [] (long long a, long long b)
        {
            if(h[a][0] == h[b][0]) return a < b;
            return h[a][0] < h[b][0];
        }
    );
    //for(int i = 1; i <= N; i++) cout << rect[i] << ' ';
    //cout << '\n';

    long long res = 0;

    long long curr, L, R;
    long long x, y, m;
    for(long long i = 1; i <= N; i++)
    {
        curr = rect[i];
        if(h[curr][0] == h[rect[i-1]][0] && hmin(rect[i-1], curr) == h[curr][0]) continue;
    //    cout << curr << ' ' << h[curr][0] << ' ';

        x = 0;
        y = curr-1;
        while(x != y)
        {
            m = (x+y)/2 + 1;
            if(hmin(m, curr-1) >= h[curr][0]) y = m-1;
            else x = m;
        }
        L = x;
                                //Problem is, with nodes of equal heights
        x = curr+1;
        y = N+1;
        while(x != y)
        {
            m = (x+y)/2;
            if(hmin(curr+1, m) >= h[curr][0]) x = m+1;
            else y = m;
        }
        R = y;

        long long y1 = h[curr][0];
        //long long y2 = h[rect[i-1]][0];
        long long y2 = max(h[L][0], h[R][0]);
        long long x = w[R-1] - w[L];

        res = (res + (((x*(x+1))/2)%mod) * (( mod + (((y1*(y1+1))/2)%mod)  -  (((y2*(y2+1))/2)%mod)  ) % mod)) % mod;
        //cout << res << '\n';
    }
    cout << res << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 53 ms 9196 KB Output is correct
4 Correct 111 ms 17900 KB Output is correct
5 Correct 119 ms 18028 KB Output is correct
6 Correct 108 ms 18028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 13 ms 2156 KB Output is correct
3 Correct 56 ms 9580 KB Output is correct
4 Correct 122 ms 18668 KB Output is correct
5 Correct 126 ms 18796 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 11 ms 2156 KB Output is correct
4 Correct 55 ms 9580 KB Output is correct
5 Correct 121 ms 18668 KB Output is correct
6 Correct 130 ms 18796 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 13 ms 2156 KB Output is correct
9 Correct 63 ms 9580 KB Output is correct
10 Correct 146 ms 18540 KB Output is correct
11 Correct 149 ms 18668 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 520 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 54 ms 9196 KB Output is correct
12 Correct 117 ms 17900 KB Output is correct
13 Correct 119 ms 17900 KB Output is correct
14 Correct 110 ms 17900 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 10 ms 2156 KB Output is correct
17 Correct 55 ms 9580 KB Output is correct
18 Correct 124 ms 18728 KB Output is correct
19 Correct 122 ms 18796 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 13 ms 2156 KB Output is correct
22 Correct 63 ms 9580 KB Output is correct
23 Correct 154 ms 18612 KB Output is correct
24 Correct 164 ms 18796 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
29 Correct 2 ms 492 KB Output is correct
30 Correct 15 ms 2156 KB Output is correct
31 Correct 16 ms 2176 KB Output is correct
32 Correct 125 ms 9452 KB Output is correct
33 Correct 150 ms 9580 KB Output is correct
34 Correct 340 ms 18668 KB Output is correct
35 Correct 302 ms 18540 KB Output is correct
36 Correct 350 ms 18668 KB Output is correct
37 Correct 350 ms 18792 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 373 ms 18796 KB Output is correct
40 Correct 308 ms 18884 KB Output is correct
41 Correct 215 ms 18668 KB Output is correct
42 Correct 164 ms 18668 KB Output is correct