#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
const int INF=0x3f3f3f3f;
const int OFF=(1<<18);
const int N=2e5+5;
int k, n;
ll sol=0;
vector <int> vec, saz;
vector <pii> v;
pli T[2*OFF];
ll desaz[N];
ll pref[N], suff[N];
int cmp(pii a, pii b) {
return a.X+a.Y<b.X+b.Y;
}
int sazmi(int x) {
int ret=lower_bound(saz.begin(), saz.end(), x)-saz.begin();
desaz[ret]=x;
return ret;
}
pli mrg(pli a, pli b) {
return mp(a.X+b.X, a.Y+b.Y);
}
pli query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return mp(0, 0);
int mid=(lo+hi)/2;
return mrg(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}
int nadji(int val, int pos=1) {
if (pos>=OFF) return pos-OFF;
if (T[pos*2].Y>=val) return nadji(val, pos*2);
return nadji(val-T[pos*2].Y, pos*2+1);
}
void update(int pos) {
pos+=OFF;
T[pos].X+=desaz[pos-OFF]; T[pos].Y++;
pos/=2;
while (pos) {
T[pos]=mrg(T[pos*2], T[pos*2+1]);
pos/=2;
}
}
void dva() {
n=v.size();
sort(v.begin(), v.end(), cmp);
for (int i=0; i<n; ++i) saz.pb(v[i].X), saz.pb(v[i].Y);
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for (int i=0; i<n; ++i) v[i].X=sazmi(v[i].X), v[i].Y=sazmi(v[i].Y);
for (int i=0; i<n; ++i) {
update(v[i].X); update(v[i].Y);
int med=nadji(i+1);
pli curr1=query(0, med), curr2=query(med+1, OFF);
pref[i]=desaz[med]*curr1.Y-curr1.X+curr2.X-desaz[med]*curr2.Y;
}
fill(T, T+2*OFF, mp(0, 0));
for (int i=n-1; i>=0; --i) {
update(v[i].X); update(v[i].Y);
int med=nadji(n-i);
pli curr1=query(0, med), curr2=query(med+1, OFF);
suff[i]=desaz[med]*curr1.Y-curr1.X + curr2.X-desaz[med]*curr2.Y;
}
ll res=suff[0];
for (int i=0; i<n; ++i) res=min(res, pref[i]+suff[i+1]);
printf("%lld\n", res+sol+n);
}
void jedinica() {
for (pii pp:v) vec.pb(pp.X), vec.pb(pp.Y);
sort(vec.begin(), vec.end());
int len=vec.size();
for (int i=0; i<len; ++i) sol+=abs(vec[i]-vec[len/2]);
printf("%lld\n", sol+len/2);
}
void load() {
scanf("%d %d", &k, &n);
for (int i=0; i<n; ++i) {
char ca, cb; int a, b;
scanf(" %c %d %c %d", &ca, &a, &cb, &b);
if (ca==cb) sol+=abs(a-b);
else v.pb(mp(min(a, b), max(a, b)));
}
}
int main() {
load();
if (k==1) jedinica();
else dva();
return 0;
}
Compilation message
bridge.cpp: In function 'void load()':
bridge.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d %d", &k, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
105 | scanf(" %c %d %c %d", &ca, &a, &cb, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
5 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
34 ms |
2656 KB |
Output is correct |
13 |
Correct |
57 ms |
2656 KB |
Output is correct |
14 |
Correct |
44 ms |
2528 KB |
Output is correct |
15 |
Correct |
53 ms |
1508 KB |
Output is correct |
16 |
Correct |
39 ms |
2656 KB |
Output is correct |
17 |
Correct |
43 ms |
2656 KB |
Output is correct |
18 |
Correct |
47 ms |
2656 KB |
Output is correct |
19 |
Correct |
54 ms |
2804 KB |
Output is correct |
20 |
Correct |
43 ms |
2656 KB |
Output is correct |
21 |
Correct |
56 ms |
2656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8556 KB |
Output is correct |
2 |
Correct |
5 ms |
8556 KB |
Output is correct |
3 |
Correct |
6 ms |
8556 KB |
Output is correct |
4 |
Correct |
6 ms |
8556 KB |
Output is correct |
5 |
Correct |
6 ms |
8556 KB |
Output is correct |
6 |
Correct |
6 ms |
8556 KB |
Output is correct |
7 |
Correct |
6 ms |
8556 KB |
Output is correct |
8 |
Correct |
6 ms |
8556 KB |
Output is correct |
9 |
Correct |
6 ms |
8556 KB |
Output is correct |
10 |
Correct |
6 ms |
8556 KB |
Output is correct |
11 |
Correct |
6 ms |
8556 KB |
Output is correct |
12 |
Correct |
5 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8556 KB |
Output is correct |
2 |
Correct |
5 ms |
8556 KB |
Output is correct |
3 |
Correct |
5 ms |
8556 KB |
Output is correct |
4 |
Correct |
6 ms |
8556 KB |
Output is correct |
5 |
Correct |
7 ms |
8556 KB |
Output is correct |
6 |
Correct |
6 ms |
8556 KB |
Output is correct |
7 |
Correct |
7 ms |
8556 KB |
Output is correct |
8 |
Correct |
5 ms |
8556 KB |
Output is correct |
9 |
Correct |
5 ms |
8556 KB |
Output is correct |
10 |
Correct |
5 ms |
8556 KB |
Output is correct |
11 |
Correct |
5 ms |
8556 KB |
Output is correct |
12 |
Correct |
5 ms |
8556 KB |
Output is correct |
13 |
Correct |
7 ms |
8556 KB |
Output is correct |
14 |
Correct |
7 ms |
8556 KB |
Output is correct |
15 |
Correct |
7 ms |
8556 KB |
Output is correct |
16 |
Correct |
6 ms |
8556 KB |
Output is correct |
17 |
Correct |
6 ms |
8556 KB |
Output is correct |
18 |
Correct |
6 ms |
8556 KB |
Output is correct |
19 |
Correct |
8 ms |
8556 KB |
Output is correct |
20 |
Correct |
7 ms |
8556 KB |
Output is correct |
21 |
Correct |
7 ms |
8556 KB |
Output is correct |
22 |
Correct |
7 ms |
8556 KB |
Output is correct |
23 |
Correct |
7 ms |
8556 KB |
Output is correct |
24 |
Correct |
7 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8556 KB |
Output is correct |
2 |
Correct |
5 ms |
8556 KB |
Output is correct |
3 |
Correct |
5 ms |
8556 KB |
Output is correct |
4 |
Correct |
5 ms |
8556 KB |
Output is correct |
5 |
Correct |
35 ms |
8556 KB |
Output is correct |
6 |
Correct |
5 ms |
8556 KB |
Output is correct |
7 |
Correct |
30 ms |
8556 KB |
Output is correct |
8 |
Correct |
5 ms |
8556 KB |
Output is correct |
9 |
Correct |
5 ms |
8556 KB |
Output is correct |
10 |
Correct |
5 ms |
8556 KB |
Output is correct |
11 |
Correct |
5 ms |
8556 KB |
Output is correct |
12 |
Correct |
5 ms |
8556 KB |
Output is correct |
13 |
Correct |
6 ms |
8556 KB |
Output is correct |
14 |
Correct |
7 ms |
8556 KB |
Output is correct |
15 |
Correct |
7 ms |
8556 KB |
Output is correct |
16 |
Correct |
6 ms |
8556 KB |
Output is correct |
17 |
Correct |
7 ms |
8556 KB |
Output is correct |
18 |
Correct |
6 ms |
8556 KB |
Output is correct |
19 |
Correct |
6 ms |
8556 KB |
Output is correct |
20 |
Correct |
7 ms |
8576 KB |
Output is correct |
21 |
Correct |
7 ms |
8556 KB |
Output is correct |
22 |
Correct |
7 ms |
8556 KB |
Output is correct |
23 |
Correct |
7 ms |
8556 KB |
Output is correct |
24 |
Correct |
7 ms |
8556 KB |
Output is correct |
25 |
Correct |
137 ms |
11748 KB |
Output is correct |
26 |
Correct |
163 ms |
11748 KB |
Output is correct |
27 |
Correct |
257 ms |
13284 KB |
Output is correct |
28 |
Correct |
317 ms |
15948 KB |
Output is correct |
29 |
Correct |
256 ms |
15644 KB |
Output is correct |
30 |
Correct |
133 ms |
12392 KB |
Output is correct |
31 |
Correct |
136 ms |
13412 KB |
Output is correct |
32 |
Correct |
187 ms |
15716 KB |
Output is correct |
33 |
Correct |
173 ms |
15332 KB |
Output is correct |
34 |
Correct |
222 ms |
15588 KB |
Output is correct |
35 |
Correct |
151 ms |
13668 KB |
Output is correct |
36 |
Correct |
190 ms |
15460 KB |
Output is correct |
37 |
Correct |
126 ms |
12592 KB |
Output is correct |