#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
#define pb push_back
#define ff first
#define ss second
const ll inf = numeric_limits<ll> :: max();
struct DS{
struct node{
int s1; ll s2;
node *l, *r;
node(){
s1 = s2 = 0;
l = r = 0;
}
node(int x, ll y){
s1 = x; s2 = y;
l = r = 0;
}
node(node *ls, node *rs){
l = ls; r = rs;
s1 = s2 = 0;
if (l){
s1 += l -> s1;
s2 += l -> s2;
}
if (r){
s1 += r -> s1;
s2 += r -> s2;
}
}
};
node *rt;
int n, cc;
void init(){
rt = new node();
n = 1e9;
cc = 0;
}
node* upd(node *v, int tl, int tr, int& p, int& x){
if (tl == tr){
return new node(v -> s1 + 1, v -> s2 + x);
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!(v -> l)) v -> l = new node();
return new node(upd(v -> l, tl, tm, p, x), v -> r);
}
else {
if (!(v -> r)) v -> r = new node();
return new node(v -> l, upd(v -> r, tm + 1, tr, p, x));
}
}
void upd(int p, int x){
rt = upd(rt, 1, n, p, x);
}
pil get(node *v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return {0, 0};
if (l <= tl && tr <= r){
return {v -> s1, v -> s2};
}
int tm = (tl + tr) / 2;
pil p1 = {0, 0}, p2 = {0, 0};
if (v -> l) p1 = get(v -> l, tl, tm, l, r);
if (v -> r) p2 = get(v -> r, tm + 1, tr, l, r);
return {p1.ff + p2.ff, p1.ss + p2.ss};
}
pair<int, ll> get(int l, int r){
return get(rt, 1, n, l, r);
}
multiset<int> s1, s2;
void add(int x){
cc++;
s1.insert(x);
if (s1.size() > s2.size()){
int y = *prev(s1.end());
s1.erase(s1.find(y));
s2.insert(y);
}
if (!s1.empty() && !s2.empty()){
int a = *prev(s1.end()), b = *s2.begin();
if (a > b){
s1.erase(s1.find(a));
s2.erase(s2.find(b));
s1.insert(b);
s2.insert(a);
}
}
upd(x, x);
}
ll get(){
if (s2.empty()) return 0;
int x = (cc % 2) ? *s2.begin() : *prev(s1.end());
pil p1 = get(1, x), p2 = get(x + 1, n);
return (1LL * x * p1.ff - p1.ss) + (p2.ss - 1LL * x * p2.ff);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k, n; cin>>k>>n;
vector<pii> all;
ll out = 0;
while (n--){
char t1, t2; int x1, y1; cin>>t1>>x1>>t2>>y1;
if (t1 == t2){
out += abs(x1 - y1);
continue;
}
if (x1 > y1) swap(x1, y1);
all.pb({x1, y1});
}
n = (int) all.size();
vector<int> imp;
for (auto [x, y]: all){
imp.pb(x); imp.pb(y);
}
ll mn = inf;
if (k == 1){
if (!all.empty()){
sort(imp.begin(), imp.end());
int x = imp[(int) (imp.size() - 1) / 2];
ll sum = 0;
for (int i: imp){
sum += abs(i - x);
}
mn = sum + n;
}
}
else {
vector<pii> f;
for (int i = 0; i < n; i++){
f.pb({all[i].ff + all[i].ss, i});
}
sort(f.begin(), f.end());
vector<ll> f1(n + 1);
DS T1; T1.init();
for (int i = 1; i <= n; i++){
auto [x, y] = all[f[i - 1].ss];
T1.add(x); T1.add(y);
f1[i] = T1.get();
}
vector<ll> f2(n + 2);
DS T2; T2.init();
for (int i = n; i > 0; i--){
auto [x, y] = all[f[i - 1].ss];
T2.add(x); T2.add(y);
f2[i] = T2.get();
}
for (int i = 0; i <= n; i++){
mn = min(mn, f1[i] + f2[i + 1] + n);
}
}
if (mn == inf) mn = 0;
cout<<out + mn<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
728 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
508 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
472 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
16 ms |
3556 KB |
Output is correct |
13 |
Correct |
32 ms |
5080 KB |
Output is correct |
14 |
Correct |
23 ms |
3788 KB |
Output is correct |
15 |
Correct |
19 ms |
3024 KB |
Output is correct |
16 |
Correct |
22 ms |
4300 KB |
Output is correct |
17 |
Correct |
22 ms |
2764 KB |
Output is correct |
18 |
Correct |
26 ms |
4624 KB |
Output is correct |
19 |
Correct |
32 ms |
5072 KB |
Output is correct |
20 |
Correct |
21 ms |
4724 KB |
Output is correct |
21 |
Correct |
30 ms |
4668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
848 KB |
Output is correct |
4 |
Correct |
2 ms |
1360 KB |
Output is correct |
5 |
Correct |
2 ms |
1020 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
1532 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
1276 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
848 KB |
Output is correct |
4 |
Correct |
2 ms |
1360 KB |
Output is correct |
5 |
Correct |
1 ms |
848 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
1392 KB |
Output is correct |
9 |
Correct |
1 ms |
1104 KB |
Output is correct |
10 |
Correct |
2 ms |
1104 KB |
Output is correct |
11 |
Correct |
2 ms |
1028 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Correct |
8 ms |
6224 KB |
Output is correct |
14 |
Correct |
10 ms |
6648 KB |
Output is correct |
15 |
Correct |
14 ms |
9972 KB |
Output is correct |
16 |
Correct |
3 ms |
2128 KB |
Output is correct |
17 |
Correct |
5 ms |
3528 KB |
Output is correct |
18 |
Correct |
5 ms |
3408 KB |
Output is correct |
19 |
Correct |
8 ms |
6224 KB |
Output is correct |
20 |
Correct |
13 ms |
10320 KB |
Output is correct |
21 |
Correct |
9 ms |
6736 KB |
Output is correct |
22 |
Correct |
12 ms |
8784 KB |
Output is correct |
23 |
Correct |
8 ms |
6224 KB |
Output is correct |
24 |
Correct |
9 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
508 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
848 KB |
Output is correct |
4 |
Correct |
2 ms |
1360 KB |
Output is correct |
5 |
Correct |
1 ms |
848 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
3 ms |
1104 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Correct |
9 ms |
6224 KB |
Output is correct |
14 |
Correct |
14 ms |
6480 KB |
Output is correct |
15 |
Correct |
14 ms |
10076 KB |
Output is correct |
16 |
Correct |
3 ms |
2128 KB |
Output is correct |
17 |
Correct |
6 ms |
3664 KB |
Output is correct |
18 |
Correct |
5 ms |
3408 KB |
Output is correct |
19 |
Correct |
9 ms |
6224 KB |
Output is correct |
20 |
Correct |
14 ms |
10320 KB |
Output is correct |
21 |
Correct |
9 ms |
6992 KB |
Output is correct |
22 |
Correct |
12 ms |
8784 KB |
Output is correct |
23 |
Correct |
9 ms |
6224 KB |
Output is correct |
24 |
Correct |
9 ms |
6736 KB |
Output is correct |
25 |
Runtime error |
442 ms |
262144 KB |
Execution killed with signal 9 |
26 |
Halted |
0 ms |
0 KB |
- |