답안 #373467

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373467 2021-03-04T18:01:02 Z jenkinsser Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
55 ms 4972 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/*
A not too simple solution:
Sort sections in decreasing order by height
Use DSU (why not?, it's a good practice) when merging intervals
Note that you don't need so many long long, i just wanted to be sure
Note that this is an O(NlogN) solution because of the sorting

Made for you by Mate Busa
*/

const long long mod = 1000000007;
long long N, ans;
long long h[100000];
long long w[100000];
vector<int> sections;
int parent[100000];
bool done[100000];

//calculate binom(x,2)
long long bin(long long x){
	x %= mod;
    return ((x*(x+1))/2)%mod;
}
//count fancy rectangles in a big rectangle of size x*y
long long rec(long long x, long long y){
    return (bin(x)*bin(y))%mod;
}

//DSU
int find_set(int x){
    if(parent[x]<0) return x;
    return (parent[x] = find_set(parent[x]));
}
void union_sets(int x, int y, long long high){
    x = find_set(x); y = find_set(y);
    long long h = (rec(w[x],high)+rec(w[y],high))%mod;
    h = (rec(w[x]+w[y],high)-h+mod)%mod; ans = (ans+h)%mod;
    if(parent[x]<parent[y]){
        parent[y] = x;
        w[x] = (w[x]+w[y])%mod;
    } else {
        if(parent[x]==parent[y]) parent[y]--;
        parent[x] = y;
        w[y] = (w[x]+w[y])%mod;
    }
}

bool smaller(int x, int y){
    return h[x]>h[y];
}

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

    cin >> N;

    for(int i=0; i<N; i++){
        cin >> h[i];
        sections.push_back(i); parent[i] = -1;
    }
    for(int i=0; i<N; i++) cin >> w[i];
    //sort sections by height
    sort(sections.begin(),sections.end(),smaller);

    for(int i=0; i<N; i++) ans = (ans+rec(w[i],h[i]))%mod;

    for(int i:sections){
        //merge intervals
        if(i!=0 && done[i-1]) union_sets(i-1,i,h[i]);
        if(i!=N-1 && done[i+1]) union_sets(i,i+1,h[i]);
        done[i] = true;
    }

    cout << ans << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 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 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 17 ms 2288 KB Output is correct
4 Correct 32 ms 4076 KB Output is correct
5 Correct 30 ms 4076 KB Output is correct
6 Correct 33 ms 4076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 4 ms 620 KB Output is correct
3 Correct 17 ms 2672 KB Output is correct
4 Correct 37 ms 4844 KB Output is correct
5 Correct 44 ms 4972 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 4 ms 620 KB Output is correct
4 Correct 17 ms 2672 KB Output is correct
5 Correct 34 ms 4844 KB Output is correct
6 Correct 39 ms 4972 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 4 ms 876 KB Output is correct
9 Correct 20 ms 2804 KB Output is correct
10 Correct 34 ms 4716 KB Output is correct
11 Correct 34 ms 4844 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 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 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 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
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 1 ms 364 KB Output is correct
11 Correct 15 ms 2288 KB Output is correct
12 Correct 37 ms 4076 KB Output is correct
13 Correct 37 ms 4076 KB Output is correct
14 Correct 30 ms 4224 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 4 ms 876 KB Output is correct
17 Correct 18 ms 2800 KB Output is correct
18 Correct 35 ms 4972 KB Output is correct
19 Correct 37 ms 4972 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 4 ms 876 KB Output is correct
22 Correct 19 ms 2800 KB Output is correct
23 Correct 36 ms 4716 KB Output is correct
24 Correct 35 ms 4844 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 5 ms 876 KB Output is correct
31 Correct 5 ms 876 KB Output is correct
32 Correct 20 ms 2544 KB Output is correct
33 Correct 23 ms 2672 KB Output is correct
34 Correct 46 ms 4716 KB Output is correct
35 Correct 40 ms 4716 KB Output is correct
36 Correct 47 ms 4844 KB Output is correct
37 Correct 48 ms 4844 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 55 ms 4844 KB Output is correct
40 Correct 51 ms 4844 KB Output is correct
41 Correct 38 ms 4844 KB Output is correct
42 Correct 36 ms 4844 KB Output is correct