#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int n, k;
vector<int> st, en;
int stuf[200005], stuf2[200005];
int P[200005], S[200005], dp[1000005];
vector<int> pos;
vector<pi> brute;
int mini = 1e18;
void dnc(int l, int r, int optl, int optr){
if(l > r)return;
int mid = (l + r) >> 1;
int mn = 1e18, opt = optr;
for(int i=max(mid+1, optl);i<=optr;i++){
int cur = 0;
for(auto [a, b] : brute){
cur += min(abs(pos[mid]-a) + abs(pos[mid]-b), abs(pos[i]-a) + abs(pos[i]-b));
cur -= abs(a - b);
}
//cout << cur << " " << pos[mid] << " " << pos[i] << '\n';
if(mn > cur)mn = cur, opt = i;
}
dp[mid] = pos[opt];
mini = min(mini, mn);
dnc(l, mid - 1, optl, opt);
dnc(mid+1, r, opt, optr);
}
void solve(){
cin >> k >> n;
int ans = 0, tmp = 0;
for(int i=1;i<=n;i++){
char a, c; int b, d; cin >> a >> b >> c >> d;
if(a == c)ans += abs(b - d);
else st.push_back(max(b, d)), en.push_back(min(b, d)), tmp+= abs(b - d) + 1, pos.push_back(b), pos.push_back(d), brute.push_back({min(b, d), max(b, d)});
}
sort(st.begin(), st.end());
sort(en.begin(), en.end());
P[0] = (st.size() ? st[0] : 0);
int mn = (st.size() ? 1e18 : 0);
for(int i=1;i<(int)st.size();i++)P[i] = P[i-1] + st[i];
for(int i=(int)en.size()-1;i>=0;i--)S[i] = S[i+1] + en[i];
sort(pos.begin(), pos.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
int cnt = 0;
for(auto i : pos){
int cur = 0;
int x = upper_bound(st.begin(), st.end(), i) - st.begin();
if(x != 0){
x--;
cur += 2 * ((x + 1) * i - P[x]);
}
stuf[cnt] = cur;
x = upper_bound(en.begin(), en.end(), i) - en.begin();
if(x != (int)en.size()){
cur += 2 * (S[x] - i * ((int)en.size() - x));
stuf2[cnt] = 2 * (S[x] - i * ((int)en.size() - x));
}
cnt++;
//cout << i << " " << cur << '\n';
mn = min(mn, cur);
}
if(k == 1){
cout << ans + tmp + mn;
return;
}
mn = (st.size() ? 1e18 : 0);
cnt = 0;
for(auto i : pos){
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<pi> rem;
for(auto [a, b] : brute)if(a > i)rem.push_back({b, a});
sort(rem.begin(), rem.end());
int cur = 0, in = 0, cur2 = 0, grr = 0, grr2 = 0, id = cnt;
for(auto j : pos){
if(i >= j)continue;
id++;
while(in < (int)rem.size() && rem[in].fi <= j){
cur += rem[in].fi;
grr++;
pq.push({rem[in].fi + rem[in].se, {rem[in].fi, rem[in].se}});
in++;
}
while(!pq.empty() && pq.top().fi < i + j){
pi lol = pq.top().se;
pq.pop();
cur -= lol.fi;
cur2 += lol.se;
grr--;
grr2++;
}
mn = min(mn, stuf[cnt] + stuf2[id] + 2 * (grr * j - cur) + 2 * (cur2 - grr2 * i));
//cout << stuf[cnt] << " " << stuf2[id] << " " << 2 * (grr * j - cur) << " " << 2 * (cur2 - grr2 * i) << " " << i << " " << j << '\n';
}
cnt++;
}
cout << ans + mn + tmp;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
bridge.cpp:110:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
110 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
26 ms |
6748 KB |
Output is correct |
13 |
Correct |
79 ms |
9904 KB |
Output is correct |
14 |
Correct |
43 ms |
6228 KB |
Output is correct |
15 |
Correct |
44 ms |
6004 KB |
Output is correct |
16 |
Correct |
31 ms |
6736 KB |
Output is correct |
17 |
Correct |
49 ms |
9832 KB |
Output is correct |
18 |
Correct |
64 ms |
9152 KB |
Output is correct |
19 |
Correct |
68 ms |
9656 KB |
Output is correct |
20 |
Correct |
32 ms |
6668 KB |
Output is correct |
21 |
Correct |
64 ms |
9788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
468 KB |
Output is correct |
15 |
Correct |
91 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
13 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
81 ms |
496 KB |
Output is correct |
21 |
Correct |
60 ms |
504 KB |
Output is correct |
22 |
Correct |
116 ms |
488 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
90 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
2 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
468 KB |
Output is correct |
15 |
Correct |
91 ms |
488 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
13 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
93 ms |
492 KB |
Output is correct |
21 |
Correct |
62 ms |
468 KB |
Output is correct |
22 |
Correct |
106 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
88 ms |
488 KB |
Output is correct |
25 |
Correct |
27 ms |
8452 KB |
Output is correct |
26 |
Correct |
618 ms |
11308 KB |
Output is correct |
27 |
Execution timed out |
2083 ms |
14288 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |