Submission #961806

# Submission time Handle Problem Language Result Execution time Memory
961806 2024-04-12T13:31:20 Z starchan Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
36 ms 8444 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in array<int, 2>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)

const int mod = 1e9+7;
const int MX = 1e5+5;

int c2(int x)
{
	return ((x*(x+1))/2)%mod;
}

int sum;
vector<int> pa(MX, -1);
vector<int> dp(MX, 0);

int leader(int u)
{
	if(pa[u] < 0)
		return u;
	else
		return pa[u] = leader(pa[u]);
}

void merge(int u, int v)
{
	u = leader(u); v = leader(v);
	if(u==v)
		return;
	if(pa[u] < pa[v])
		swap(u, v);
	pa[v]+=pa[u];
	sum+=((dp[u]*dp[v])%mod); sum%=mod;
	dp[v]+=dp[u]; dp[v]%=mod;
	pa[u] = v;
	return;
}
signed main()
{
	fast();
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	vector<in> ab(n);
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		ab[i][0] = a[i];
	}
	for(int i = 0; i < n; i++)
	{
		cin >> b[i];
		ab[i][1] = i;
	}
	sort(ab.rbegin(), ab.rend());
	vector<int> C{ab[0][0]};
	for(auto p: ab)
	{
		if(C.back() != p[0])
			C.pb(p[0]);
	}
	int ans = 0;
	int l = 0; int D = C.size(); C.pb(0);
	for(int i = 0; i < D; i++)
	{
		int L = l;
		while((l < n) && (ab[l][0] >= C[i]))
		{
			int lp = ab[l][1];
			dp[lp] = b[lp];
			sum+=c2(b[lp]);
			sum%=mod;
			l++;
		}
		l = L;
		while((l < n) && (ab[l][0] >= C[i]))
		{
			int lp = ab[l][1];
			if(((lp+1) < n) && (a[lp+1] >= C[i]))
				merge(lp, lp+1);
			if(((lp-1) >= 0) && (a[lp-1] >= C[i]))
				merge(lp, lp-1);
			l++;
		}
		int val = (sum*((c2(C[i])-c2(C[i+1])+mod)%mod))%mod;
		ans+=val; ans%=mod;
	}
	cout << ans << "\n";
	return 0;
}	
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1864 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 12 ms 3420 KB Output is correct
4 Correct 22 ms 5180 KB Output is correct
5 Correct 23 ms 4956 KB Output is correct
6 Correct 21 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 3 ms 2136 KB Output is correct
3 Correct 12 ms 3420 KB Output is correct
4 Correct 24 ms 4956 KB Output is correct
5 Correct 22 ms 4956 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 1 ms 1880 KB Output is correct
3 Correct 4 ms 2140 KB Output is correct
4 Correct 11 ms 3420 KB Output is correct
5 Correct 22 ms 4956 KB Output is correct
6 Correct 22 ms 5176 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 12 ms 3928 KB Output is correct
10 Correct 22 ms 6108 KB Output is correct
11 Correct 22 ms 6364 KB Output is correct
12 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1880 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1884 KB Output is correct
13 Correct 1 ms 1884 KB Output is correct
14 Correct 1 ms 1880 KB Output is correct
15 Correct 1 ms 1884 KB Output is correct
16 Correct 1 ms 2040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1880 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 2 ms 2136 KB Output is correct
11 Correct 12 ms 4448 KB Output is correct
12 Correct 26 ms 6548 KB Output is correct
13 Correct 24 ms 6184 KB Output is correct
14 Correct 29 ms 6304 KB Output is correct
15 Correct 1 ms 1880 KB Output is correct
16 Correct 3 ms 2396 KB Output is correct
17 Correct 14 ms 4496 KB Output is correct
18 Correct 23 ms 7004 KB Output is correct
19 Correct 23 ms 7004 KB Output is correct
20 Correct 2 ms 1884 KB Output is correct
21 Correct 3 ms 2652 KB Output is correct
22 Correct 12 ms 4876 KB Output is correct
23 Correct 22 ms 8160 KB Output is correct
24 Correct 22 ms 8156 KB Output is correct
25 Correct 1 ms 1884 KB Output is correct
26 Correct 1 ms 1888 KB Output is correct
27 Correct 1 ms 1888 KB Output is correct
28 Correct 1 ms 1888 KB Output is correct
29 Correct 1 ms 1888 KB Output is correct
30 Correct 4 ms 2656 KB Output is correct
31 Correct 4 ms 2656 KB Output is correct
32 Correct 15 ms 4376 KB Output is correct
33 Correct 18 ms 5352 KB Output is correct
34 Correct 30 ms 7904 KB Output is correct
35 Correct 30 ms 6748 KB Output is correct
36 Correct 34 ms 8148 KB Output is correct
37 Correct 31 ms 7640 KB Output is correct
38 Correct 1 ms 1884 KB Output is correct
39 Correct 31 ms 7648 KB Output is correct
40 Correct 32 ms 8148 KB Output is correct
41 Correct 36 ms 8444 KB Output is correct
42 Correct 23 ms 8216 KB Output is correct