Submission #48202

# Submission time Handle Problem Language Result Execution time Memory
48202 2018-05-10T13:59:42 Z Kmcode Palembang Bridges (APIO15_bridge) C++14
100 / 100
245 ms 8372 KB
#include "bits/stdc++.h"
using namespace std;

int n;
int k;

vector<pair<long long int, long long int> > v;

vector<long long int> vv;

bool cmp(pair<long long int, long long int> a, pair<long long int, long long int> b) {
	return a.first + a.second < b.first + b.second;
}

#define MAX 300002

long long int ans[MAX];

long long int bit[MAX];
int cnt[MAX];

void add(int i, long long int x) {
	i++;
	while (i < MAX) {
		bit[i] += x;
		cnt[i]++;
		i += i&-i;
	}
}
long long int sum(int i) {
	long long int r = 0;
	i++;
	while (i) {
		r += bit[i];
		i -= i&-i;
	}
	return r;
}
long long int rng(int l, int r) {
	long long int ret = sum(r);
	if (l)ret -= sum(l - 1);
	return ret;
}

int get_id(long long int val) {
	return lower_bound(vv.begin(), vv.end(),val) - vv.begin();
}

long long int sum_cnt(int idx) {
	int r = 0;
	idx++;
	while (idx) {
		r += cnt[idx];
		idx -= idx&-idx;
	}
	return r;
}

long long int rng_cnt(int l, int r) {
	int ret = sum_cnt(r);
	if (l)ret -= sum_cnt(l - 1);
	return ret;
}

long long int tmp[MAX];

int main() {
	cin >> k >> n;
	long long int ans = 0;
	for (int i = 0; i < n; i++) {
		long long int a, b,aa,bb;
		char A[2], B[2];
		scanf("%s%lld%s%lld", A, &aa, B, &bb);
		if (A[0] == B[0]) {
			ans += abs(aa-bb);
		}
		else {
			ans++;
			tie(a, b) = minmax(aa, bb);
			v.push_back(make_pair(a, b));
			vv.push_back(a);
			vv.push_back(b);
		}
	}
	sort(vv.begin(), vv.end());
	vv.erase(unique(vv.begin(), vv.end()), vv.end());
	sort(v.begin(), v.end(), cmp);
	long long int AA = LLONG_MAX;
	{
		int idx = 0;
		int cn = 0;
		for (int i = 0; i < v.size(); i++) {
			while (idx <= i) {
				add(get_id(v[idx].first), v[idx].first);
				add(get_id(v[idx].second), v[idx].second);
				cn++;
				idx++;
			}
			int mint = 0;
			int maxt = vv.size() - 1;
			while (mint + 1 < maxt) {
				int mid = (mint + maxt) >> 1;
				if (sum_cnt(mid) >= cn) {
					maxt = mid;
				}
				else {
					mint = mid;
				}
			}
			if (sum_cnt(mint) >= cn) {
				maxt = mint;
			}
			else{
				mint = maxt;
			}
			long long int sm =rng_cnt(0, mint)*vv[mint] - rng(0, mint);
			sm += rng(mint + 1, vv.size() - 1) - rng_cnt(mint+1,vv.size()-1)*vv[mint];
			tmp[i]=sm;
		}
		AA = min(AA, tmp[v.size() - 1]);
	}
	{
		long long int las = -11111111;
		memset(cnt, 0, sizeof(cnt));
		memset(bit, 0, sizeof(bit));
		int idx = v.size()-1;
		int cn = 0;
		for (int i = v.size()-1; i>=0; i--) {
			while (idx >=i) {
				add(get_id(v[idx].first), v[idx].first);
				add(get_id(v[idx].second), v[idx].second);
				cn++;
				idx--;
			}
			int mint = 0;
			int maxt = vv.size() - 1;
			while (mint + 1 < maxt) {
				int mid = (mint + maxt) >> 1;
				if (sum_cnt(mid) >= cn) {
					maxt = mid;
				}
				else {
					mint = mid;
				}
			}
			if (sum_cnt(mint) >= cn) {
				maxt = mint;
			}
			else {
				mint = maxt;
			}
			long long int sm = rng_cnt(0, mint)*vv[mint] - rng(0, mint);
			sm += rng(mint + 1, vv.size() - 1) - rng_cnt(mint + 1, vv.size() - 1)*vv[mint];
			if (i)sm += tmp[i - 1];
			AA = min(AA, sm);
			las = sm;
		}
		if (k == 1)AA = las;
		ans += AA;
	}
	if (k == 1) {
		printf("%lld\n", ans - AA + tmp[v.size()-1]);
		return 0;
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:92:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < v.size(); i++) {
                   ~~^~~~~~~~~~
bridge.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%lld%s%lld", A, &aa, B, &bb);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3832 KB Output is correct
2 Correct 5 ms 3832 KB Output is correct
3 Correct 5 ms 3864 KB Output is correct
4 Correct 6 ms 4120 KB Output is correct
5 Correct 6 ms 4132 KB Output is correct
6 Correct 5 ms 4132 KB Output is correct
7 Correct 6 ms 4132 KB Output is correct
8 Correct 5 ms 4132 KB Output is correct
9 Correct 6 ms 4132 KB Output is correct
10 Correct 5 ms 4132 KB Output is correct
11 Correct 6 ms 4132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4132 KB Output is correct
2 Correct 4 ms 4132 KB Output is correct
3 Correct 5 ms 4176 KB Output is correct
4 Correct 6 ms 4176 KB Output is correct
5 Correct 6 ms 4176 KB Output is correct
6 Correct 5 ms 4176 KB Output is correct
7 Correct 6 ms 4176 KB Output is correct
8 Correct 6 ms 4180 KB Output is correct
9 Correct 6 ms 4180 KB Output is correct
10 Correct 6 ms 4180 KB Output is correct
11 Correct 6 ms 4180 KB Output is correct
12 Correct 93 ms 8280 KB Output is correct
13 Correct 241 ms 8284 KB Output is correct
14 Correct 143 ms 8284 KB Output is correct
15 Correct 133 ms 8284 KB Output is correct
16 Correct 95 ms 8284 KB Output is correct
17 Correct 155 ms 8284 KB Output is correct
18 Correct 143 ms 8284 KB Output is correct
19 Correct 176 ms 8284 KB Output is correct
20 Correct 102 ms 8372 KB Output is correct
21 Correct 167 ms 8372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8372 KB Output is correct
2 Correct 4 ms 8372 KB Output is correct
3 Correct 4 ms 8372 KB Output is correct
4 Correct 4 ms 8372 KB Output is correct
5 Correct 4 ms 8372 KB Output is correct
6 Correct 4 ms 8372 KB Output is correct
7 Correct 4 ms 8372 KB Output is correct
8 Correct 5 ms 8372 KB Output is correct
9 Correct 4 ms 8372 KB Output is correct
10 Correct 4 ms 8372 KB Output is correct
11 Correct 5 ms 8372 KB Output is correct
12 Correct 4 ms 8372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8372 KB Output is correct
2 Correct 4 ms 8372 KB Output is correct
3 Correct 4 ms 8372 KB Output is correct
4 Correct 5 ms 8372 KB Output is correct
5 Correct 4 ms 8372 KB Output is correct
6 Correct 4 ms 8372 KB Output is correct
7 Correct 4 ms 8372 KB Output is correct
8 Correct 4 ms 8372 KB Output is correct
9 Correct 5 ms 8372 KB Output is correct
10 Correct 4 ms 8372 KB Output is correct
11 Correct 5 ms 8372 KB Output is correct
12 Correct 5 ms 8372 KB Output is correct
13 Correct 5 ms 8372 KB Output is correct
14 Correct 5 ms 8372 KB Output is correct
15 Correct 7 ms 8372 KB Output is correct
16 Correct 5 ms 8372 KB Output is correct
17 Correct 5 ms 8372 KB Output is correct
18 Correct 6 ms 8372 KB Output is correct
19 Correct 5 ms 8372 KB Output is correct
20 Correct 6 ms 8372 KB Output is correct
21 Correct 5 ms 8372 KB Output is correct
22 Correct 7 ms 8372 KB Output is correct
23 Correct 5 ms 8372 KB Output is correct
24 Correct 5 ms 8372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8372 KB Output is correct
2 Correct 4 ms 8372 KB Output is correct
3 Correct 4 ms 8372 KB Output is correct
4 Correct 5 ms 8372 KB Output is correct
5 Correct 5 ms 8372 KB Output is correct
6 Correct 4 ms 8372 KB Output is correct
7 Correct 4 ms 8372 KB Output is correct
8 Correct 5 ms 8372 KB Output is correct
9 Correct 5 ms 8372 KB Output is correct
10 Correct 4 ms 8372 KB Output is correct
11 Correct 4 ms 8372 KB Output is correct
12 Correct 4 ms 8372 KB Output is correct
13 Correct 5 ms 8372 KB Output is correct
14 Correct 5 ms 8372 KB Output is correct
15 Correct 6 ms 8372 KB Output is correct
16 Correct 5 ms 8372 KB Output is correct
17 Correct 5 ms 8372 KB Output is correct
18 Correct 5 ms 8372 KB Output is correct
19 Correct 5 ms 8372 KB Output is correct
20 Correct 6 ms 8372 KB Output is correct
21 Correct 5 ms 8372 KB Output is correct
22 Correct 6 ms 8372 KB Output is correct
23 Correct 5 ms 8372 KB Output is correct
24 Correct 6 ms 8372 KB Output is correct
25 Correct 91 ms 8372 KB Output is correct
26 Correct 99 ms 8372 KB Output is correct
27 Correct 228 ms 8372 KB Output is correct
28 Correct 236 ms 8372 KB Output is correct
29 Correct 245 ms 8372 KB Output is correct
30 Correct 122 ms 8372 KB Output is correct
31 Correct 98 ms 8372 KB Output is correct
32 Correct 175 ms 8372 KB Output is correct
33 Correct 144 ms 8372 KB Output is correct
34 Correct 181 ms 8372 KB Output is correct
35 Correct 105 ms 8372 KB Output is correct
36 Correct 176 ms 8372 KB Output is correct
37 Correct 91 ms 8372 KB Output is correct