/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int k, n;
ll ans = 0;
array<ll, 2> a[N];
pair<ll, ll> pref[N], suf[N];
array<ll, 2> TL[N], TR[N];
vector<pair<ll, int>> v;
void build(int m){
for(int i = 0; i <= 4*m; ++i) TL[i] = {0, 0}, TR[i] = {0, 0};
}
void updateL(int l, int r, int p, int k, ll val){
if(l == r){
TL[k] = {val, 1};
return;
}
int m = l+r>>1;
if(p <= m) updateL(l, m, p, k<<1, val);
else updateL(m+1, r, p, k<<1|1, val);
TL[k][0] = TL[k<<1][0] + TL[k<<1|1][0];
TL[k][1] = TL[k<<1][1] + TL[k<<1|1][1];
}
void updateR(int l, int r, int p, int k, ll val){
if(l == r){
TR[k] = {val, 1};
return;
}
int m = l+r>>1;
if(p <= m) updateR(l, m, p, k<<1, val);
else updateR(m+1, r, p, k<<1|1, val);
TR[k][0] = TR[k<<1][0] + TR[k<<1|1][0];
TR[k][1] = TR[k<<1][1] + TR[k<<1|1][1];
}
array<ll, 2> getL(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return {0, 0};
if(ql <= l && r <= qr) return TL[k];
int m = l+r>>1;
auto p = getL(l, m, ql, qr, k<<1);
auto v = getL(m+1, r, ql, qr, k<<1);
return {p[0] + v[0], p[1] + v[1]};
}
array<ll, 2> getR(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return {0, 0};
if(ql <= l && r <= qr) return TR[k];
int m = l+r>>1;
auto p = getR(l, m, ql, qr, k<<1);
auto v = getR(m+1, r, ql, qr, k<<1);
return {p[0] + v[0], p[1] + v[1]};
}
ll get(int l, int r, int p, int k, int tot, ll sum){
if(l == r){
tot += TR[k][1];
sum -= TR[k][0];
return sum + tot * v[l - 1].first;
}
int m = l+r>>1;
if(TR[k<<1][1] + TL[k<<1][1] >= p){
return get(l, m, p, k<<1, tot - TL[k<<1|1][1], sum + TL[k<<1|1][0]);
}
return get(m+1, r, p - (TR[k<<1][1] + TL[k<<1][1]), k<<1|1, tot + TR[k<<1][1], sum - TR[k<<1][0]);
}
void solve(){
cin >> k >> n;
int c = 0;
for(int i = 1; i <= n; ++i){
char c1, c2; ll x1, x2; cin >> c1 >> x1 >> c2 >> x2;
if(c1 == c2){
ans += abs(x1-x2);
}else{
++c;
a[c][0] = min(x1, x2);
a[c][1] = max(x1, x2);
ans += abs(x1-x2);
}
}
n = c;
if(n==0){
cout << ans;
return;
}
sort(a+1, a+1+n);
if(k == 1){
vector<pair<ll, ll>> v;
for(int i = 1; i <= n; ++i) v.pb({a[i][0], 0}), v.pb({a[i][1], 1});
sort(all(v));
pref[0] = {0, 0};
for(int i = 1; i < v.size(); ++i){
pref[i] = pref[i - 1];
if(v[i].second == 1) pref[i].first += v[i].first, pref[i].second++;
}
suf[v.size() - 1] = {0, 0};
for(int i = v.size() - 2; i >= 0; --i){
suf[i] = suf[i + 1];
if(v[i].second == 0) suf[i].first += v[i].first, suf[i].second++;
}
ll best = 1e18;
for(int i = 0; i < v.size(); ++i){
if(abs(suf[i].second - pref[i].second) <= 1)
best = min(best, suf[i].first - pref[i].first + (v[i].first * pref[i].second) - (v[i].first * suf[i].second));
}
cout << ans + best*2 + n;
}else{
ll best = 1e18;
sort(a+1, a+1+n, [&](const array<ll, 2> &x, const array<ll, 2> &y){
return x[0] + x[1] < y[0] + y[1];
});
for(int i = 1; i <= n; ++i) v.pb({a[i][0], i*2}), v.pb({a[i][1], i*2+1});
sort(all(v));
// for(int i= 1; i<= n; ++i) cout << a[i][0] << ' ' << a[i][1] << " : ";
// en;
vector<array<int, 2>> pos(2*n+5);
for(int i = 0; i < n*2; ++i){
pos[v[i].second / 2][v[i].second % 2] = i + 1;
}
// for(auto x: v) cout << x.first << ' ' << x.second << '\n';
// en;
// for(int i = 1; i <= n; ++i){
// cout << pos[i][0] << ' ' << pos[i][1] << '\n';
// }
vector<ll> PREF(n+1, 0);
build(2*n);
for(int i = 1; i <= n; ++i){
updateL(1, 2*n, pos[i][0], 1, a[i][0]);
updateR(1, 2*n, pos[i][1], 1, a[i][1]);
PREF[i] = get(1, 2*n, i, 1, 0, 0);
// cout << PREF[i] << ' ' << a[i][0] << ' ' << a[i][1] << " | ";
}
// en;
vector<ll> SUF(n+1, 0);
build(2*n);
for(int i = n; i >= 1; --i){
updateL(1, 2*n, pos[i][0], 1, a[i][0]);
updateR(1, 2*n, pos[i][1], 1, a[i][1]);
SUF[i] = get(1, 2*n, n-i+1, 1, 0, 0);
}
for(int i = 0; i < n; ++i) best=min(best,PREF[i]+SUF[i+1]);
cout << ans + 2*best + n;
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
bridge.cpp: In function 'void updateL(int, int, int, int, long long int)':
bridge.cpp:28:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'void updateR(int, int, int, int, long long int)':
bridge.cpp:39:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'std::array<long long int, 2> getL(int, int, int, int, int)':
bridge.cpp:49:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'std::array<long long int, 2> getR(int, int, int, int, int)':
bridge.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'long long int get(int, int, int, int, int, long long int)':
bridge.cpp:69:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'void solve()':
bridge.cpp:102:22: 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]
102 | for(int i = 1; i < v.size(); ++i){
| ~~^~~~~~~~~~
bridge.cpp:113:22: 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]
113 | for(int i = 0; i < v.size(); ++i){
| ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:166:15: warning: unused variable 'aa' [-Wunused-variable]
166 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
26 ms |
18628 KB |
Output is correct |
13 |
Correct |
45 ms |
17984 KB |
Output is correct |
14 |
Correct |
38 ms |
18756 KB |
Output is correct |
15 |
Correct |
26 ms |
12500 KB |
Output is correct |
16 |
Correct |
27 ms |
18896 KB |
Output is correct |
17 |
Correct |
29 ms |
18432 KB |
Output is correct |
18 |
Correct |
34 ms |
18084 KB |
Output is correct |
19 |
Correct |
34 ms |
17864 KB |
Output is correct |
20 |
Correct |
30 ms |
18384 KB |
Output is correct |
21 |
Correct |
33 ms |
17872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4580 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4864 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4576 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4568 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4636 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
91 ms |
39744 KB |
Output is correct |
26 |
Correct |
101 ms |
41144 KB |
Output is correct |
27 |
Correct |
137 ms |
41976 KB |
Output is correct |
28 |
Correct |
135 ms |
42044 KB |
Output is correct |
29 |
Correct |
134 ms |
42068 KB |
Output is correct |
30 |
Correct |
63 ms |
23492 KB |
Output is correct |
31 |
Correct |
86 ms |
41148 KB |
Output is correct |
32 |
Correct |
84 ms |
42048 KB |
Output is correct |
33 |
Correct |
88 ms |
42616 KB |
Output is correct |
34 |
Correct |
107 ms |
42040 KB |
Output is correct |
35 |
Correct |
87 ms |
41404 KB |
Output is correct |
36 |
Correct |
89 ms |
41664 KB |
Output is correct |
37 |
Correct |
83 ms |
40748 KB |
Output is correct |