Submission #719076

# Submission time Handle Problem Language Result Execution time Memory
719076 2023-04-05T09:57:47 Z esomer Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
44 ms 6004 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#define endl "\n"

typedef long long int ll;
 
const int MOD = 1e9 + 7;

struct DSU{
	vector<int> v;
	vector<ll> sum;
	void init(vector<tuple<ll, ll, int>>& a){
		int n = (int)a.size();
		v.assign(n, -1);
		sum.assign(n, 0);
		for(int i = 0; i < n; i++){
			sum[get<2>(a[i])] = get<1>(a[i]);
		}
	}
	int gt(int x){return v[x] < 0 ? x : v[x] = gt(v[x]);}
	ll gets(int x){return sum[gt(x)] % MOD;}
	void unite(int x, int y){
		x = gt(x); y = gt(y);
		if(x == y) return;
		if(v[x] > v[y]) swap(x, y);
		v[x] += v[y]; v[y] = x; sum[x] += sum[y]; sum[x] %= MOD;
	}
};

ll calc(ll x){
	if(x % 2 == 0){
		ll f = x / 2; f %= MOD;
		ll s = x - 1; s %= MOD;
		return (f * s) % MOD;
	}else{
		ll f = x; f %= MOD;
		ll s = (x - 1) / 2; s %= MOD;
		return (f * s) % MOD;
	}
}

ll self(ll w, ll h){
	w++; h++;
	return (calc(w) * calc(h)) % MOD;
}

ll two(ll w, ll h, ll w1, ll h1){
	h++;
	ll x = w * w1; x %= MOD;
	return (x * calc(h)) % MOD;
}

void solve(){
	int n; cin >> n;
	vector<tuple<ll, ll, int>> v(n);
	for(int i = 0; i < n; i++){
		int x; cin >> x;
		get<0>(v[i]) = x;
	}
	for(int i = 0; i < n; i++){
		int x; cin >> x;
		get<1>(v[i]) = x;
		get<2>(v[i]) = i;
	}
	sort(v.begin(), v.end());
	if(n == 1){
		ll w = get<1>(v[0]);
		ll h = get<0>(v[0]);
		cout << self(w, h) << endl;
		return;
	}
	DSU UF; UF.init(v);
	vector<bool> done(n, 0);
	ll ans = 0;
	for(int i = n - 1; i >= 0; i--){
		int j = get<2>(v[i]);
		ll w = get<1>(v[i]);
		ll h = get<0>(v[i]);
		done[j] = 1;
		ans += self(w, h); ans %= MOD;
		if(j == 0){
			if(done[1]){
				ans += two(w, h, UF.gets(1), h); ans %= MOD;
				UF.unite(0, 1);
			}
		}else if(j == n - 1){
			if(done[n-2]){
				ans += two(w, h, UF.gets(n-2), h); ans %= MOD;
				UF.unite(j, n-2);
			}
		}else{
			if(done[j+1]){
				ans += two(w, h, UF.gets(j+1), h); ans %= MOD;
				if(done[j-1]){
					ans += two(w, h, UF.gets(j-1), h); ans %= MOD;
					ans += two(UF.gets(j+1), h, UF.gets(j-1), h); ans %= MOD;
					UF.unite(j-1, j);
				}
				UF.unite(j+1, j);
			}else if(done[j-1]){
				ans += two(w, h, UF.gets(j-1), h); ans %= MOD;
				UF.unite(j-1, j);
			}
		}
	}
	cout << ans << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    //~ int tt; cin >> tt;
    int tt = 1;
    for(int t = 1; t <= tt; t++){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 16 ms 2644 KB Output is correct
4 Correct 33 ms 4948 KB Output is correct
5 Correct 31 ms 5076 KB Output is correct
6 Correct 30 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 4 ms 768 KB Output is correct
3 Correct 16 ms 3028 KB Output is correct
4 Correct 35 ms 5716 KB Output is correct
5 Correct 44 ms 5856 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 272 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
4 Correct 18 ms 3024 KB Output is correct
5 Correct 36 ms 5736 KB Output is correct
6 Correct 39 ms 5860 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 4 ms 852 KB Output is correct
9 Correct 17 ms 3016 KB Output is correct
10 Correct 28 ms 5612 KB Output is correct
11 Correct 28 ms 5720 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 15 ms 2644 KB Output is correct
12 Correct 33 ms 5080 KB Output is correct
13 Correct 31 ms 5088 KB Output is correct
14 Correct 31 ms 5088 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 840 KB Output is correct
17 Correct 17 ms 3056 KB Output is correct
18 Correct 38 ms 5836 KB Output is correct
19 Correct 40 ms 6004 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 5 ms 808 KB Output is correct
22 Correct 17 ms 3020 KB Output is correct
23 Correct 26 ms 5604 KB Output is correct
24 Correct 27 ms 5720 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 328 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 328 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 4 ms 764 KB Output is correct
31 Correct 4 ms 768 KB Output is correct
32 Correct 18 ms 2900 KB Output is correct
33 Correct 18 ms 3036 KB Output is correct
34 Correct 33 ms 5588 KB Output is correct
35 Correct 35 ms 5576 KB Output is correct
36 Correct 33 ms 5732 KB Output is correct
37 Correct 34 ms 5808 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 34 ms 5724 KB Output is correct
40 Correct 33 ms 5732 KB Output is correct
41 Correct 29 ms 5728 KB Output is correct
42 Correct 29 ms 5708 KB Output is correct