#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
typedef pair<ll,ll> pii;
typedef pair<pii,pii> piiii;
const int maxn = 2e5 + 1;
int n,k, cnt[4*maxn];
ll bit[maxn];
pii a[maxn];
struct IT {
void update(int v, int tl, int tr, int pos, int w) {
if (tl == tr) {
cnt[v] += w;
return;
}
int tm = (tl + tr)/2;
if (pos <= tm) update(2*v,tl,tm,pos,w);
else update(2*v+1,tm+1,tr,pos,w);
cnt[v] = cnt[2*v] + cnt[2*v+1];
}
int getMedian(int v, int tl, int tr, int w) {
if (tl == tr) return tl;
int tm = (tl + tr)/2;
if (cnt[2*v] >= w) return getMedian(2*v,tl,tm,w);
else return getMedian(2*v+1,tm+1,tr,w - cnt[2*v]);
}
} seg;
struct BIT {
void update(int idx, ll val, int m) {
for (; idx <= m; idx += (idx & -idx)) bit[idx] += val;
}
ll getSum(int L, int R) {
if (L > R) return 0;
L--;
ll res = 0;
for (; L > 0; L -= (L & -L)) res -= bit[L];
for (; R > 0; R -= (R & -R)) res += bit[R];
return res;
}
} fen;
void solve1() {
vector<ll> arr;
ll res = 0;
for (int i = 1; i <= n; i++) {
char C1,C2;
ll x,y;
cin >> C1 >> x >> C2 >> y;
if (C1 == C2) res += abs(x-y);
else {
arr.push_back(x);
arr.push_back(y);
res++;
}
}
sort(arr.begin(),arr.end());
ll mid = arr[arr.size()/2];
for (ll x: arr) res += abs(mid-x);
cout << res;
}
bool cmp(piiii A, piiii B) {
return A.fi.fi + A.fi.se < B.fi.fi + B.fi.se;
}
ll sum[maxn];
void solve2() {
vector<piiii> arr;
vector<pii> roirac;
ll same = 0;
for (int i = 1; i <= n; i++) {
char C1,C2;
ll x,y;
cin >> C1 >> x >> C2 >> y;
if (C1 == C2) same += abs(x-y);
else {
arr.push_back(piiii(pii(x,y),pii(0,0)));
same++;
}
}
sort(arr.begin(),arr.end(),cmp);
int m = arr.size();
for (int i = 0; i < m; i++) {
roirac.push_back(pii(arr[i].fi.fi,i+1));
roirac.push_back(pii(arr[i].fi.se,i+1 + m));
}
sort(roirac.begin(),roirac.end());
for (int i = 0; i < m; i++) {
arr[i].se.fi = lower_bound(roirac.begin(),roirac.end(),pii(arr[i].fi.fi,i+1)) - roirac.begin() + 1;
arr[i].se.se = lower_bound(roirac.begin(),roirac.end(),pii(arr[i].fi.se,i+1 + m)) - roirac.begin() + 1;
}
for (int i = 0; i < m; i++) {
seg.update(1,1,2*m,arr[i].se.fi,1);
fen.update(arr[i].se.fi,arr[i].fi.fi,2*m);
seg.update(1,1,2*m,arr[i].se.se,1);
fen.update(arr[i].se.se,arr[i].fi.se,2*m);
int mid = seg.getMedian(1,1,2*m,(2*(i+1) + 1)/2);
sum[i+1] = fen.getSum(mid+1,2*m) - fen.getSum(1,mid);
}
for (int i = 0; i < m; i++) {
seg.update(1,1,2*m,arr[i].se.fi,-1);
fen.update(arr[i].se.fi,-arr[i].fi.fi,2*m);
seg.update(1,1,2*m,arr[i].se.se,-1);
fen.update(arr[i].se.se,-arr[i].fi.se,2*m);
}
for (int i = m-1; i >= 0; i--) {
seg.update(1,1,2*m,arr[i].se.fi,1);
fen.update(arr[i].se.fi,arr[i].fi.fi,2*m);
seg.update(1,1,2*m,arr[i].se.se,1);
fen.update(arr[i].se.se,arr[i].fi.se,2*m);
int mid = seg.getMedian(1,1,2*m,(2*(m-i)+1)/2);
sum[i] += fen.getSum(mid+1,2*m) - fen.getSum(1,mid);
}
ll bridge = 1e18;
for (int i = 0; i <= m; i++) bridge = min(bridge,sum[i]);
cout << same + bridge;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int type;
cin >> type >> n;
if (type == 1) solve1();
else solve2();
return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# |
Verdict |
Execution time |
Memory |
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 |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 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 |
336 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 |
376 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
22 ms |
3288 KB |
Output is correct |
13 |
Correct |
43 ms |
4284 KB |
Output is correct |
14 |
Correct |
31 ms |
3664 KB |
Output is correct |
15 |
Correct |
24 ms |
2664 KB |
Output is correct |
16 |
Correct |
33 ms |
3724 KB |
Output is correct |
17 |
Correct |
30 ms |
4240 KB |
Output is correct |
18 |
Correct |
38 ms |
3912 KB |
Output is correct |
19 |
Correct |
42 ms |
4308 KB |
Output is correct |
20 |
Correct |
29 ms |
3908 KB |
Output is correct |
21 |
Correct |
37 ms |
4092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
212 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 |
340 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
336 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 |
296 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 |
340 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 |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
136 ms |
12028 KB |
Output is correct |
26 |
Correct |
179 ms |
12032 KB |
Output is correct |
27 |
Correct |
231 ms |
12984 KB |
Output is correct |
28 |
Correct |
246 ms |
13452 KB |
Output is correct |
29 |
Correct |
236 ms |
13328 KB |
Output is correct |
30 |
Correct |
129 ms |
7364 KB |
Output is correct |
31 |
Correct |
154 ms |
12768 KB |
Output is correct |
32 |
Correct |
133 ms |
13400 KB |
Output is correct |
33 |
Correct |
153 ms |
13000 KB |
Output is correct |
34 |
Correct |
153 ms |
13404 KB |
Output is correct |
35 |
Correct |
147 ms |
13016 KB |
Output is correct |
36 |
Correct |
156 ms |
13100 KB |
Output is correct |
37 |
Correct |
127 ms |
11912 KB |
Output is correct |