#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int base = 31;
const ll mod = 1e9 + 7;
const ll oo = 1e16;
const int N = 2e5 + 5;
const int M = 2e5;
vector<pll> v;
vector<ll> nen;
bool cmp(pii a, pii b){
return (a.fi + a.se) < (b.fi + b.se);
}
struct node{
ll cnt, sum;
};
struct segtree{
node st[4 * N];
node combine(node a, node b){
return {a.cnt + b.cnt, a.sum + b.sum};
}
void update(int id, int l, int r, int i, int v){
if (i < l || r < i) return;
if (l == r){
st[id].cnt += v;
st[id].sum = nen[l - 1] * st[id].cnt;
return;
}
int mid = (l + r) >> 1;
update(2 * id, l, mid, i, v), update(2 * id + 1, mid + 1, r, i, v);
st[id] = combine(st[2 * id], st[2 * id + 1]);
}
node get(int id, int l, int r, int u, int v){
if (v < l || r < u) return {0, 0};
if (u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return combine(get(2 * id, l, mid, u, v), get(2 * id + 1, mid + 1, r, u, v));
}
int find_kth(int id, int l, int r, int k){
if (l == r) return l;
int mid = (l + r) >> 1;
if (st[2 * id].cnt >= k) return find_kth(2 * id, l, mid, k);
return find_kth(2 * id + 1, mid + 1, r, k - st[2 * id].cnt);
}
ll find_cost(int sz){
ll cmed = find_kth(1, 1, M, (1 + sz) >> 1);
ll med = nen[cmed - 1];
node lower_ans = get(1, 1, M, 1, cmed);
node higher_ans = get(1, 1, M, cmed + 1, M);
return med * lower_ans.cnt - lower_ans.sum + higher_ans.sum - med * higher_ans.cnt;
}
} Left, Right, All;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int K, n;
cin >> K >> n;
ll res = 0;
forr(i, 1, n) {
char b1, b2;
int x1, x2;
cin >> b1 >> x1 >> b2 >> x2;
if (b1 == b2) res += abs(x1 - x2);
else{
res++;
v.pb({x1, x2});
nen.pb(x1);
nen.pb(x2);
}
}
sort(all(v), cmp);
sort(all(nen));
nen.resize(unique(all(nen)) - nen.begin());
forr(i, 0, v.size() - 1){
v[i].fi = lower_bound(all(nen), v[i].fi) - nen.begin() + 1;
v[i].se = lower_bound(all(nen), v[i].se) - nen.begin() + 1;
}
// K = 1
ll cost1 = 0;
if (v.size() >= 1){
forf(i, 0, v.size()){
All.update(1, 1, M, v[i].fi, 1);
All.update(1, 1, M, v[i].se, 1);
}
cost1 = All.find_cost(2 * v.size());
}
// ll cost2 = oo;
// if (K == 2 && v.size() >= 2){
// forf(i, 0, v.size()) {
// Right.update(1, 1, M, v[i].fi, 1);
// Right.update(1, 1, M, v[i].se, 1);
// }
// int cntLeft = 0;
// forr(i, 0, v.size() - 2){
// Left.update(1, 1, M, v[i].fi, 1);
// Left.update(1, 1, M, v[i].se, 1);
// Right.update(1, 1, M, v[i].fi, -1);
// Right.update(1, 1, M, v[i].se, -1);
// cntLeft++;
// cost2 = min(cost2, Left.find_cost(2 * cntLeft) + Right.find_cost(2 * (v.size() - cntLeft)));
// }
// }
cout << res + cost1 << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:2:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
2 | #define forr(i, a, b) for (int i = (a); i <= (b); i++)
| ^
bridge.cpp:112:4: note: in expansion of macro 'forr'
112 | forr(i, 0, v.size() - 1){
| ^~~~
bridge.cpp:4:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define forf(i, a, b) for (int i = (a); i < (b); i++)
| ^
bridge.cpp:122:7: note: in expansion of macro 'forf'
122 | forf(i, 0, v.size()){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |