Submission #47183

# Submission time Handle Problem Language Result Execution time Memory
47183 2018-04-28T15:40:08 Z szawinis Palembang Bridges (APIO15_bridge) C++17
100 / 100
945 ms 68580 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<pair<ll, ll> > f;
void update(int i, ll v) {
	++i;
	while(i < f.size()) {
		f[i].first += v;
		++f[i].second;
		i += i & -i;
	}
}
pair<ll, ll> query(int l, int r) {
	pair<ll, ll> ret = {0, 0};
	if(l > r) return ret;
	for(int i = r+1; i > 0; i -= i & -i)
		ret.first += f[i].first, ret.second += f[i].second;
	for(int i = l; i > 0; i -= i & -i)
		ret.first -= f[i].first, ret.second -= f[i].second;
	return ret;
}

int k, n;
ll ans;
vector<pair<int, int> > a;
vector<int> vals, sums;
unordered_map<int, int> mp;

vector<ll> solve(function<bool(int,int)> comp) { // comparator
	vector<ll> res;
	ordered_set<pair<int,int> > oset;
	res.resize(sums.size());
	f.assign(vals.size() + 1, make_pair(0, 0));

	for(int i = 0, j = 0; i < sums.size(); i++) {
		while(j < a.size() && comp(a[j].first + a[j].second, sums[i])) {
			oset.insert(make_pair(a[j].first, j << 1));
			oset.insert(make_pair(a[j].second, j << 1 | 1));
			update(mp[a[j].first], a[j].first);
			update(mp[a[j].second], a[j].second);
			++j;
		}
		int place_first = mp[oset.find_by_order(oset.size() >> 1)->first];
		ll sum, cnt;
		tie(sum, cnt) = query(place_first, vals.size()-1);
		res[i] += sum - cnt * vals[place_first];
		tie(sum, cnt) = query(0, place_first);
		res[i] += cnt * vals[place_first] - sum;
	}
	return res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> k >> n;
	for(int i = 0; i < n; i++) {
		char P, Q; int s, t;
		cin >> P >> s >> Q >> t;
		if(P == Q) ans += abs(s-t);
		else {
			a.emplace_back(s, t);
			vals.push_back(s);
			vals.push_back(t);
			sums.push_back(s+t);
		}
	}
	ans += a.size();

	if(a.empty()) {
		cout << ans;
		return 0;
	}

	sort(a.begin(), a.end(), [] (auto x, auto y) { return x.first + x.second < y.first + y.second; });
	sort(sums.begin(), sums.end());
	sums.resize(unique(sums.begin(), sums.end()) - sums.begin());
	sort(vals.begin(), vals.end());
	vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
	for(int i = 0; i < vals.size(); i++) mp[vals[i]] = i;

	vector<ll> res1 = solve(less_equal<int>());
	if(k == 1) {
		ans += res1[sums.size()-1];
		cout << ans;
		return 0;
	}
	reverse(a.begin(), a.end());
	reverse(sums.begin(), sums.end());
	vector<ll> res2 = solve(greater<int>());
	vector<ll> res = vector<ll>(sums.size());
	for(int i = 0; i < sums.size(); i++) res[i] = res1[i] + res2[sums.size()-1-i];

	// for(ll x: res1) cout << x << ' ';
	// cout << endl;
	// for(ll x: res2) cout << x << ' ';
	// cout << endl;

	ans += *min_element(res.begin(), res.end());
	cout << ans;
}

Compilation message

bridge.cpp: In function 'void update(int, ll)':
bridge.cpp:13:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(i < f.size()) {
        ~~^~~~~~~~~~
bridge.cpp: In function 'std::vector<long long int> solve(std::function<bool(int, int)>)':
bridge.cpp:41:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0, j = 0; i < sums.size(); i++) {
                        ~~^~~~~~~~~~~~~
bridge.cpp:42:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(j < a.size() && comp(a[j].first + a[j].second, sums[i])) {
         ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < vals.size(); i++) mp[vals[i]] = i;
                 ~~^~~~~~~~~~~~~
bridge.cpp:98:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < sums.size(); i++) res[i] = res1[i] + res2[sums.size()-1-i];
                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 3 ms 732 KB Output is correct
4 Correct 5 ms 804 KB Output is correct
5 Correct 4 ms 872 KB Output is correct
6 Correct 3 ms 872 KB Output is correct
7 Correct 4 ms 1140 KB Output is correct
8 Correct 3 ms 1140 KB Output is correct
9 Correct 4 ms 1140 KB Output is correct
10 Correct 3 ms 1140 KB Output is correct
11 Correct 4 ms 1140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1140 KB Output is correct
2 Correct 2 ms 1140 KB Output is correct
3 Correct 3 ms 1140 KB Output is correct
4 Correct 4 ms 1188 KB Output is correct
5 Correct 4 ms 1208 KB Output is correct
6 Correct 3 ms 1232 KB Output is correct
7 Correct 3 ms 1308 KB Output is correct
8 Correct 3 ms 1332 KB Output is correct
9 Correct 4 ms 1444 KB Output is correct
10 Correct 3 ms 1444 KB Output is correct
11 Correct 3 ms 1444 KB Output is correct
12 Correct 206 ms 16464 KB Output is correct
13 Correct 564 ms 30740 KB Output is correct
14 Correct 260 ms 30740 KB Output is correct
15 Correct 268 ms 30740 KB Output is correct
16 Correct 217 ms 30740 KB Output is correct
17 Correct 418 ms 37236 KB Output is correct
18 Correct 185 ms 38356 KB Output is correct
19 Correct 407 ms 41200 KB Output is correct
20 Correct 227 ms 41200 KB Output is correct
21 Correct 290 ms 45440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 45440 KB Output is correct
2 Correct 2 ms 45440 KB Output is correct
3 Correct 2 ms 45440 KB Output is correct
4 Correct 2 ms 45440 KB Output is correct
5 Correct 2 ms 45440 KB Output is correct
6 Correct 2 ms 45440 KB Output is correct
7 Correct 2 ms 45440 KB Output is correct
8 Correct 2 ms 45440 KB Output is correct
9 Correct 2 ms 45440 KB Output is correct
10 Correct 2 ms 45440 KB Output is correct
11 Correct 2 ms 45440 KB Output is correct
12 Correct 2 ms 45440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 45440 KB Output is correct
2 Correct 2 ms 45440 KB Output is correct
3 Correct 2 ms 45440 KB Output is correct
4 Correct 2 ms 45440 KB Output is correct
5 Correct 2 ms 45440 KB Output is correct
6 Correct 2 ms 45440 KB Output is correct
7 Correct 2 ms 45440 KB Output is correct
8 Correct 2 ms 45440 KB Output is correct
9 Correct 2 ms 45440 KB Output is correct
10 Correct 2 ms 45440 KB Output is correct
11 Correct 2 ms 45440 KB Output is correct
12 Correct 2 ms 45440 KB Output is correct
13 Correct 3 ms 45440 KB Output is correct
14 Correct 4 ms 45440 KB Output is correct
15 Correct 5 ms 45440 KB Output is correct
16 Correct 2 ms 45440 KB Output is correct
17 Correct 3 ms 45440 KB Output is correct
18 Correct 3 ms 45440 KB Output is correct
19 Correct 3 ms 45440 KB Output is correct
20 Correct 4 ms 45440 KB Output is correct
21 Correct 3 ms 45440 KB Output is correct
22 Correct 4 ms 45440 KB Output is correct
23 Correct 3 ms 45440 KB Output is correct
24 Correct 5 ms 45440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 45440 KB Output is correct
2 Correct 2 ms 45440 KB Output is correct
3 Correct 2 ms 45440 KB Output is correct
4 Correct 2 ms 45440 KB Output is correct
5 Correct 2 ms 45440 KB Output is correct
6 Correct 2 ms 45440 KB Output is correct
7 Correct 2 ms 45440 KB Output is correct
8 Correct 2 ms 45440 KB Output is correct
9 Correct 2 ms 45440 KB Output is correct
10 Correct 2 ms 45440 KB Output is correct
11 Correct 2 ms 45440 KB Output is correct
12 Correct 2 ms 45440 KB Output is correct
13 Correct 3 ms 45440 KB Output is correct
14 Correct 4 ms 45440 KB Output is correct
15 Correct 5 ms 45440 KB Output is correct
16 Correct 2 ms 45440 KB Output is correct
17 Correct 3 ms 45440 KB Output is correct
18 Correct 3 ms 45440 KB Output is correct
19 Correct 3 ms 45440 KB Output is correct
20 Correct 5 ms 45440 KB Output is correct
21 Correct 3 ms 45440 KB Output is correct
22 Correct 4 ms 45440 KB Output is correct
23 Correct 4 ms 45440 KB Output is correct
24 Correct 5 ms 45440 KB Output is correct
25 Correct 286 ms 45440 KB Output is correct
26 Correct 398 ms 45440 KB Output is correct
27 Correct 863 ms 49412 KB Output is correct
28 Correct 945 ms 52788 KB Output is correct
29 Correct 928 ms 55104 KB Output is correct
30 Correct 390 ms 55104 KB Output is correct
31 Correct 235 ms 55104 KB Output is correct
32 Correct 693 ms 60344 KB Output is correct
33 Correct 204 ms 60716 KB Output is correct
34 Correct 626 ms 64096 KB Output is correct
35 Correct 296 ms 64096 KB Output is correct
36 Correct 507 ms 68580 KB Output is correct
37 Correct 223 ms 68580 KB Output is correct