Submission #293181

# Submission time Handle Problem Language Result Execution time Memory
293181 2020-09-07T17:39:37 Z luciocf Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
122 ms 3368 KB
// CEOI 2020 - Fancy Fence
// Lúcio Cardoso

#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

const int maxn = 1e5+10;
const int mod = 1e9+7;

typedef pair<int, int> pii;

struct Rect
{
	int h, w, ind;
} rect[maxn];

struct DSU
{
	int pai[maxn], peso[maxn];
	int soma[maxn];

	void init(int n)
	{
		for (int i = 1; i <= n; i++)
			pai[i] = i, peso[i] = 1, soma[i] = rect[i].w;
	}

	int Find(int x)
	{
		if (pai[x] == x) return x;
		return pai[x] = Find(pai[x]);
	}

	void Join(int x, int y)
	{
		x = Find(x), y = Find(y);
		if (x == y) return;

		if (peso[x] < peso[y]) swap(x, y);

		pai[y] = x, peso[x] += peso[y], soma[x] = (soma[x]+soma[y])%mod;
 	}
} dsu;

int back[maxn];
bool mark[maxn];

int choose2(int a)
{
	return (1ll*a*(a-1)/2ll)%mod;
}

int pot(int a, int b)
{
	if (!b) return 1;

	int t = pot(a, b/2);

	if (b&1) return (1ll*a*((1ll*t*t)%mod))%mod;
	return (1ll*t*t)%mod;
}

int qtd(int h, int w)
{
	int x = (1ll*h*choose2(w))%mod;
	x = (1ll*x + 1ll*w*choose2(h))%mod;

	int ans = choose2((1ll*h*w)%mod);
	ans = (ans - x + mod)%mod;

	ans = (1ll*ans*pot(2, mod-2))%mod;
	ans = (ans + x)%mod;
	ans = (1ll*ans + 1ll*h*w)%mod;

	return ans;
}

int main(void)
{
	int n;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &rect[i].h);
		rect[i].ind = i;
	}

	int ans = 0;

	for (int i = 1; i <= n; i++)
		scanf("%d", &rect[i].w);

	sort(rect+1, rect+n+1, [&] (Rect a, Rect b) {return a.h > b.h;});

	for (int i = 1; i <= n; i++)
		back[rect[i].ind] = i;

	dsu.init(n);

	for (int i = 1; i <= n; i++)
	{
		int j = rect[i].ind;

		int fora = 0;

		if (j > 1 && mark[j-1])
		{
			fora = (fora + qtd(rect[i].h, dsu.soma[dsu.Find(back[j-1])]))%mod;
			dsu.Join(i, back[j-1]);
		}

		if (j < n && mark[j+1])
		{
			fora = (fora + qtd(rect[i].h, dsu.soma[dsu.Find(back[j+1])]))%mod;
			dsu.Join(i, back[j+1]);
		}

		ans = (ans + qtd(rect[i].h, dsu.soma[dsu.Find(i)]))%mod;
		ans = (ans - fora + mod)%mod;

		mark[j] = 1;
	}

	printf("%d\n", ans);
}

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
fancyfence.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   89 |   scanf("%d", &rect[i].h);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
fancyfence.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   96 |   scanf("%d", &rect[i].w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 52 ms 1740 KB Output is correct
4 Correct 101 ms 3192 KB Output is correct
5 Correct 102 ms 3192 KB Output is correct
6 Correct 102 ms 3368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 11 ms 640 KB Output is correct
3 Correct 54 ms 1784 KB Output is correct
4 Correct 109 ms 3192 KB Output is correct
5 Correct 110 ms 3192 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 11 ms 640 KB Output is correct
4 Correct 55 ms 1912 KB Output is correct
5 Correct 109 ms 3192 KB Output is correct
6 Correct 107 ms 3200 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 11 ms 640 KB Output is correct
9 Correct 53 ms 1784 KB Output is correct
10 Correct 108 ms 3192 KB Output is correct
11 Correct 106 ms 3192 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 51 ms 1792 KB Output is correct
12 Correct 102 ms 3196 KB Output is correct
13 Correct 102 ms 3192 KB Output is correct
14 Correct 101 ms 3192 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 13 ms 640 KB Output is correct
17 Correct 53 ms 1792 KB Output is correct
18 Correct 107 ms 3192 KB Output is correct
19 Correct 107 ms 3192 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 11 ms 688 KB Output is correct
22 Correct 53 ms 1792 KB Output is correct
23 Correct 105 ms 3192 KB Output is correct
24 Correct 108 ms 3192 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 14 ms 640 KB Output is correct
31 Correct 12 ms 640 KB Output is correct
32 Correct 55 ms 1784 KB Output is correct
33 Correct 60 ms 1792 KB Output is correct
34 Correct 120 ms 3192 KB Output is correct
35 Correct 116 ms 3192 KB Output is correct
36 Correct 122 ms 3192 KB Output is correct
37 Correct 119 ms 3192 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 115 ms 3192 KB Output is correct
40 Correct 112 ms 3192 KB Output is correct
41 Correct 108 ms 3192 KB Output is correct
42 Correct 110 ms 3192 KB Output is correct