#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const ll oo = 1e18;
const int mod = 1e9 + 7;
const int K = 1e5 + 5;
char P[N], Q[N];
int S[N], T[N], k, n;
ll ans, s[N];
vector<ll> vr;
vector<pii> h;
int cnt = 0;
namespace sub1{
void solve(){
ll ret = oo;
for(int i = 1; i <= cnt << 1; i ++){
for(int j = i + 1; j <= cnt << 1; j ++){
ll kq = 0;
for(auto [l, r] : h){
if(l <= vr[i - 1] && vr[i - 1] <= r) kq += 1 + vr[i - 1] - l + r - vr[i - 1];
else if(l <= vr[j - 1] && vr[j - 1] <= r) kq += 1 + vr[j - 1] - l + r - vr[j - 1];
else if(vr[i - 1] <= l && r <= vr[j - 1]) kq += 1 + min(l - vr[i - 1] + r - vr[i - 1], vr[j - 1] - l + vr[j - 1] - r);
else if(r <= vr[i - 1]) kq += 1 + vr[i - 1] - l + vr[i - 1] - r;
else kq += 1 + l - vr[j - 1] + r - vr[j - 1];
}
ret = min(ret, kq + ans);
}
}
ans = ret;
}
}
namespace sub2{
ll cal(int i,int j){
ll kq = 0;
for(auto [l, r] : h){
if(l <= i && i <= r) kq += 1 + i - l + r - i;
else if(l <= j && j <= r) kq += 1 + j - l + r - j;
else if(i <= l && r <= j) kq += 1 + min(l - i + r - i, j - l + j - r);
else if(r <= i) kq += 1 + i - l + i - r;
else kq += 1 + l - j + r - j;
}
return kq;
}
ll ck(int i){
ll ret = oo;
int l = vr[i - 1] + 1;
int r = min((ll)1e9, vr[cnt * 2 - 1] + 1);
while(l < r - 3 && 1.0 * clock() / CLOCKS_PER_SEC <= 1.8){
int m1 = l + (r - l)/3, m2 = r - (r - l)/3;
ll u = cal(vr[i - 1], m1);
ll v = cal(vr[i - 1], m2);
if(u > v) l = m1; else r = m2;
ret = min({ret, u, v});
}
for(int j = l; j <= r; j ++) ret = min(ret, cal(vr[i - 1], j));
return ret;
}
void solve(){
ll ret = oo;
int le = 1, ri = 2 * cnt, mid1, mid2;
// cerr << 1.0 * clock() / CLOCKS_PER_SEC << " j\n";
while(le < ri - 3 && 1.0 * clock() / CLOCKS_PER_SEC <= 1.8){
mid1 = le + (ri - le)/3, mid2 = ri - (ri - le)/3;
// cerr << le << " " << ri << "f\n";
ll u = ck(mid1);
ll v = ck(mid2);
if(u > v) le = mid1; else ri = mid2;
ret = min({ret, u, v});
}
for(int i = le; i <= ri; i ++) ret = min(ret, ck(i));
ans += ret;
// for(int i = 1; i <= cnt * 2; i ++){
// int l = vr[i - 1] + 1;
// int r = min((int)1e9, vr[cnt * 2 - 1] + 1);
// while(l < r - 2){
// int m1 = l + (r - l)/3, m2 = r - (r - l)/3;
// int u = cal(vr[i - 1], m1);
// int v = cal(vr[i - 1], m2);
// if(u > v) l = m1; else r = m2;
// ret = min({ret, u, v});
// }
// for(int j = l; j <= r; j ++) ret = min(ret, cal(vr[i - 1], j));
//// cerr << i << " " << ret << "f\n";
// }
// ans = ret + ans;
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> k >> n;
for(int i = 1; i <= n; i ++){
cin >> P[i] >> S[i] >> Q[i] >> T[i];
if(P[i] == Q[i]){
ans += abs(S[i] - T[i]);
continue;
}
vr.pb(S[i]);
vr.pb(T[i]);
cnt++;
if(S[i] > T[i]) swap(S[i], T[i]);
h.pb({S[i], T[i]});
}
if(!vr.empty()){
sort(vr.begin(), vr.end());
for(int i = 1; i <= cnt * 2; i ++) s[i] = s[i - 1] + vr[i - 1];
if(k == 1){
ll kq = oo;
ll lim = vr[cnt - 1];
for(int j = max(vr[0], lim - (ll)1e5); j <= min(vr[2 * cnt - 1], lim + (ll)1e5); j ++){
int i = lower_bound(all(vr), j) - vr.begin();
if(i < 2 * cnt){
i++;
}
kq = min(kq, cnt + 1ll * j * (i - 1) - 1ll * j * (1ll * 2 * cnt - i + 1) - s[i - 1] + s[cnt * 2] - s[i - 1]);
}
ans = ans + kq;
}else{
// if(n <= 100) sub1 :: solve();
// else
sub2 :: solve();
}
}
cout << ans;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2392 KB |
Output is correct |
5 |
Correct |
3 ms |
2396 KB |
Output is correct |
6 |
Correct |
3 ms |
2400 KB |
Output is correct |
7 |
Correct |
3 ms |
2832 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
3 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
4 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
3 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
3 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2576 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
12 |
Correct |
18 ms |
6480 KB |
Output is correct |
13 |
Correct |
37 ms |
6468 KB |
Output is correct |
14 |
Correct |
24 ms |
6216 KB |
Output is correct |
15 |
Correct |
22 ms |
4688 KB |
Output is correct |
16 |
Correct |
20 ms |
6480 KB |
Output is correct |
17 |
Correct |
26 ms |
6580 KB |
Output is correct |
18 |
Correct |
34 ms |
6480 KB |
Output is correct |
19 |
Correct |
34 ms |
6480 KB |
Output is correct |
20 |
Correct |
25 ms |
6480 KB |
Output is correct |
21 |
Correct |
41 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
3 ms |
2392 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
5 ms |
2396 KB |
Output is correct |
15 |
Correct |
11 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
3 ms |
2396 KB |
Output is correct |
19 |
Correct |
5 ms |
2396 KB |
Output is correct |
20 |
Correct |
9 ms |
2396 KB |
Output is correct |
21 |
Correct |
5 ms |
2396 KB |
Output is correct |
22 |
Correct |
14 ms |
2576 KB |
Output is correct |
23 |
Correct |
11 ms |
2396 KB |
Output is correct |
24 |
Correct |
13 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2580 KB |
Output is correct |
15 |
Correct |
11 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
3 ms |
2396 KB |
Output is correct |
19 |
Correct |
5 ms |
2396 KB |
Output is correct |
20 |
Correct |
9 ms |
2576 KB |
Output is correct |
21 |
Correct |
5 ms |
2396 KB |
Output is correct |
22 |
Correct |
13 ms |
2396 KB |
Output is correct |
23 |
Correct |
11 ms |
2396 KB |
Output is correct |
24 |
Correct |
12 ms |
2392 KB |
Output is correct |
25 |
Correct |
48 ms |
6744 KB |
Output is correct |
26 |
Correct |
953 ms |
6648 KB |
Output is correct |
27 |
Execution timed out |
2058 ms |
6480 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |