#include <bits/stdc++.h>
using namespace std;
int n, k, m;
pair <int, int> a[100010], s[100010];
int cmp(pair <int, int> a, pair <int, int> b) {
return a.first + a.second < b.first + b.second;
}
long long res = 1e18, sum;
struct node {
int num;
long long val;
} seg[2][800010];
void update(int type, int id, int l, int r, int i, int val) {
if (i > r || i < l) return;
if (l == r) {
if (val == -1) seg[type][id] = {0, 0};
else seg[type][id] = {1, val};
return;
}
int mid = l + r >> 1;
update(type, id * 2, l, mid, i, val);
update(type, id * 2 + 1, mid + 1, r, i, val);
seg[type][id].val = seg[type][id * 2].val + seg[type][id * 2 + 1].val;
seg[type][id].num = seg[type][id * 2].num + seg[type][id * 2 + 1].num;
}
long long curl, curr;
long long get(int type, int id, int l, int r, int k, int pos) {
if (pos == 0) return 0;
if (l == r) {
return (seg[type][id].val * (pos - 1) - curl) + (curr - seg[type][id].val * (seg[type][1].num - pos));
}
int mid = l + r >> 1;
if (seg[type][id * 2].num < k) {
curl += seg[type][id * 2].val;
return get(type, id * 2 + 1, mid + 1, r, k - seg[type][id * 2].num, pos);
}
else {
curr += seg[type][id * 2 + 1].val;
return get(type, id * 2, l, mid, k, pos);
}
}
void compress() {
vector <pair <int, int> > data;
for (int i = 1; i <= n; ++i) {
data.push_back({a[i].first, i});
data.push_back({a[i].second, i + n});
}
sort(data.begin(), data.end());
for (int i = 0; i < data.size(); ++i) {
if (data[i].second <= n) s[data[i].second].first = i + 1;
else s[data[i].second - n].second = i + 1;
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> k >> m;
for (int i = 1; i <= m; ++i) {
char c1, c2;
int x1, x2;
cin >> c1 >> x1 >> c2 >> x2;
if (c1 == c2) {
sum += abs(x1 - x2);
continue;
}
a[++n] = {x1, x2};
}
sort(a + 1, a + n + 1, cmp);
compress();
for (int i = 1; i <= n; ++i) {
update(0, 1, 1, n * 2, s[i].first, a[i].first);
update(0, 1, 1, n * 2, s[i].second, a[i].second);
}
if (k == 1) {
curl = 0, curr = 0;
cout << get(0, 1, 1, n * 2, (2 * n + 1) / 2, (2 * n + 1) / 2) + sum + n << "\n";
exit(0);
}
res = get(0, 1, 1, n * 2, (2 * n + 1) / 2, (2 * n + 1) / 2) + sum + n;
for (int i = 1; i <= n; ++i) {
curl = curr = 0;
update(1, 1, 1, n * 2, s[i].first, a[i].first);
update(1, 1, 1, n * 2, s[i].second, a[i].second);
update(0, 1, 1, n * 2, s[i].first, -1);
update(0, 1, 1, n * 2, s[i].second, -1);
long long tmp0 = get(0, 1, 1, n * 2, (seg[0][1].num + 1) / 2, (seg[0][1].num + 1) / 2);
curl = curr = 0;
long long tmp1 = get(1, 1, 1, n * 2, (seg[1][1].num + 1) / 2, (seg[1][1].num + 1) / 2);
res = min(res, tmp0 + tmp1 + sum + n);
}
cout << res;
}
Compilation message
bridge.cpp: In function 'void update(int, int, int, int, int, int)':
bridge.cpp:26:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l + r >> 1;
| ~~^~~
bridge.cpp: In function 'long long int get(int, int, int, int, int, int)':
bridge.cpp:40:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid = l + r >> 1;
| ~~^~~
bridge.cpp: In function 'void compress()':
bridge.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < data.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
81 ms |
11004 KB |
Output is correct |
13 |
Correct |
126 ms |
12452 KB |
Output is correct |
14 |
Correct |
112 ms |
11272 KB |
Output is correct |
15 |
Correct |
64 ms |
6796 KB |
Output is correct |
16 |
Correct |
85 ms |
11856 KB |
Output is correct |
17 |
Correct |
99 ms |
12436 KB |
Output is correct |
18 |
Correct |
102 ms |
12120 KB |
Output is correct |
19 |
Correct |
84 ms |
12456 KB |
Output is correct |
20 |
Correct |
86 ms |
12112 KB |
Output is correct |
21 |
Correct |
110 ms |
12236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
472 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
484 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
172 ms |
19148 KB |
Output is correct |
26 |
Correct |
209 ms |
19348 KB |
Output is correct |
27 |
Correct |
276 ms |
20172 KB |
Output is correct |
28 |
Correct |
258 ms |
20856 KB |
Output is correct |
29 |
Correct |
278 ms |
20728 KB |
Output is correct |
30 |
Correct |
113 ms |
10608 KB |
Output is correct |
31 |
Correct |
187 ms |
20168 KB |
Output is correct |
32 |
Correct |
179 ms |
20640 KB |
Output is correct |
33 |
Correct |
175 ms |
20292 KB |
Output is correct |
34 |
Correct |
192 ms |
20816 KB |
Output is correct |
35 |
Correct |
178 ms |
20296 KB |
Output is correct |
36 |
Correct |
195 ms |
20380 KB |
Output is correct |
37 |
Correct |
171 ms |
19112 KB |
Output is correct |