#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<ll, ll, ll> tp;
const ll inf = 1e18;
ll x, n, m, ans = inf, sub;
vector<ll> cp;
vector<tp> a;
struct segtree {
ll val[888888], cnt[888888], lim;
void init () {
for(lim=1;lim<=m;lim<<=1);
for(ll i=2*lim;--i;) {val[i] = 0; cnt[i] = 0;}
}
void upd (ll P, ll V) {
P += lim;
while(P) {val[P]+=V; cnt[P]++; P>>=1;}
}
ll get (ll K) {
ll P = 1, R = 0;
while(P < lim) {
if(cnt[2*P] >= K) P = 2*P;
else {K-=cnt[2*P]; R+=val[2*P]; P=2*P+1;}
}
return K*cp[P-lim] + R;
}
} seg;
ll cpr (ll X) {
return lower_bound(cp.begin(), cp.end(), X) - cp.begin();
}
void getdp (vector<ll> &D, vector<tp> &V) {
seg.init();
D.push_back(0);
ll I = 0;
for(auto &T : V) {
ll _, A, B; tie(_, A, B) = T;
A = cpr(A); B = cpr(B); ++I;
seg.upd(A, cp[A]);
seg.upd(B, cp[B]);
D.push_back(seg.get(2*I)-2*seg.get(I));
}
}
int main()
{
scanf("%lld%lld",&x,&n);
for(ll i=1;i<=n;i++) {
ll A, B; char C[2], D[2];
scanf("%s%lld%s%lld",C,&A,D,&B);
if(C[0] == D[0]) sub += abs(A-B);
else {
a.push_back(make_tuple(A+B, A, B));
cp.push_back(A); cp.push_back(B);
sub++;
}
}
sort(a.begin(), a.end());
n = a.size();
sort(cp.begin(), cp.end());
cp.erase(unique(cp.begin(), cp.end()), cp.end());
m = cp.size();
vector<ll> pre, suf;
getdp(pre, a);
reverse(a.begin(), a.end());
getdp(suf, a);
ll ans = inf;
for(ll i=0;i<=n;i++) {
ans = min(ans, pre[i]+suf[n-i]);
}
printf("%lld\n",(x==1?pre[n]:ans)+sub);
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:50:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&x,&n);
^
bridge.cpp:53:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",C,&A,D,&B);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15912 KB |
Output is correct |
2 |
Correct |
0 ms |
15912 KB |
Output is correct |
3 |
Correct |
0 ms |
16056 KB |
Output is correct |
4 |
Correct |
0 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
16056 KB |
Output is correct |
6 |
Correct |
0 ms |
16056 KB |
Output is correct |
7 |
Correct |
0 ms |
16056 KB |
Output is correct |
8 |
Correct |
0 ms |
16056 KB |
Output is correct |
9 |
Correct |
0 ms |
16056 KB |
Output is correct |
10 |
Correct |
0 ms |
16056 KB |
Output is correct |
11 |
Correct |
0 ms |
16056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15912 KB |
Output is correct |
2 |
Correct |
0 ms |
15912 KB |
Output is correct |
3 |
Correct |
0 ms |
16056 KB |
Output is correct |
4 |
Correct |
0 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
16056 KB |
Output is correct |
6 |
Correct |
0 ms |
16056 KB |
Output is correct |
7 |
Correct |
0 ms |
16056 KB |
Output is correct |
8 |
Correct |
0 ms |
16056 KB |
Output is correct |
9 |
Correct |
0 ms |
16056 KB |
Output is correct |
10 |
Correct |
0 ms |
16056 KB |
Output is correct |
11 |
Correct |
0 ms |
16056 KB |
Output is correct |
12 |
Correct |
56 ms |
24188 KB |
Output is correct |
13 |
Correct |
286 ms |
24188 KB |
Output is correct |
14 |
Correct |
139 ms |
24188 KB |
Output is correct |
15 |
Correct |
126 ms |
20092 KB |
Output is correct |
16 |
Correct |
43 ms |
24188 KB |
Output is correct |
17 |
Correct |
99 ms |
24188 KB |
Output is correct |
18 |
Correct |
156 ms |
24188 KB |
Output is correct |
19 |
Correct |
159 ms |
24188 KB |
Output is correct |
20 |
Correct |
66 ms |
24188 KB |
Output is correct |
21 |
Correct |
139 ms |
24188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15912 KB |
Output is correct |
2 |
Correct |
0 ms |
15912 KB |
Output is correct |
3 |
Correct |
0 ms |
15912 KB |
Output is correct |
4 |
Correct |
0 ms |
15912 KB |
Output is correct |
5 |
Correct |
0 ms |
15912 KB |
Output is correct |
6 |
Correct |
0 ms |
15912 KB |
Output is correct |
7 |
Correct |
0 ms |
15912 KB |
Output is correct |
8 |
Correct |
0 ms |
15912 KB |
Output is correct |
9 |
Correct |
0 ms |
15912 KB |
Output is correct |
10 |
Correct |
0 ms |
15912 KB |
Output is correct |
11 |
Correct |
0 ms |
15912 KB |
Output is correct |
12 |
Correct |
0 ms |
15912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15912 KB |
Output is correct |
2 |
Correct |
0 ms |
15912 KB |
Output is correct |
3 |
Correct |
0 ms |
15912 KB |
Output is correct |
4 |
Correct |
0 ms |
15912 KB |
Output is correct |
5 |
Correct |
0 ms |
15912 KB |
Output is correct |
6 |
Correct |
0 ms |
15912 KB |
Output is correct |
7 |
Correct |
0 ms |
15912 KB |
Output is correct |
8 |
Correct |
0 ms |
15912 KB |
Output is correct |
9 |
Correct |
0 ms |
15912 KB |
Output is correct |
10 |
Correct |
0 ms |
15912 KB |
Output is correct |
11 |
Correct |
0 ms |
15912 KB |
Output is correct |
12 |
Correct |
0 ms |
15912 KB |
Output is correct |
13 |
Correct |
0 ms |
16056 KB |
Output is correct |
14 |
Correct |
0 ms |
16056 KB |
Output is correct |
15 |
Correct |
0 ms |
16056 KB |
Output is correct |
16 |
Correct |
0 ms |
15912 KB |
Output is correct |
17 |
Correct |
0 ms |
16056 KB |
Output is correct |
18 |
Correct |
0 ms |
15912 KB |
Output is correct |
19 |
Correct |
0 ms |
16056 KB |
Output is correct |
20 |
Correct |
0 ms |
16056 KB |
Output is correct |
21 |
Correct |
0 ms |
16056 KB |
Output is correct |
22 |
Correct |
0 ms |
16056 KB |
Output is correct |
23 |
Correct |
0 ms |
16056 KB |
Output is correct |
24 |
Correct |
0 ms |
16056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
15912 KB |
Output is correct |
2 |
Correct |
0 ms |
15912 KB |
Output is correct |
3 |
Correct |
0 ms |
15912 KB |
Output is correct |
4 |
Correct |
0 ms |
15912 KB |
Output is correct |
5 |
Correct |
0 ms |
15912 KB |
Output is correct |
6 |
Correct |
0 ms |
15912 KB |
Output is correct |
7 |
Correct |
0 ms |
15912 KB |
Output is correct |
8 |
Correct |
0 ms |
15912 KB |
Output is correct |
9 |
Correct |
0 ms |
15912 KB |
Output is correct |
10 |
Correct |
0 ms |
15912 KB |
Output is correct |
11 |
Correct |
0 ms |
15912 KB |
Output is correct |
12 |
Correct |
0 ms |
15912 KB |
Output is correct |
13 |
Correct |
0 ms |
16056 KB |
Output is correct |
14 |
Correct |
0 ms |
16056 KB |
Output is correct |
15 |
Correct |
0 ms |
16056 KB |
Output is correct |
16 |
Correct |
0 ms |
15912 KB |
Output is correct |
17 |
Correct |
0 ms |
16056 KB |
Output is correct |
18 |
Correct |
0 ms |
15912 KB |
Output is correct |
19 |
Correct |
0 ms |
16056 KB |
Output is correct |
20 |
Correct |
0 ms |
16056 KB |
Output is correct |
21 |
Correct |
0 ms |
16056 KB |
Output is correct |
22 |
Correct |
0 ms |
16056 KB |
Output is correct |
23 |
Correct |
0 ms |
16056 KB |
Output is correct |
24 |
Correct |
0 ms |
16056 KB |
Output is correct |
25 |
Correct |
49 ms |
24188 KB |
Output is correct |
26 |
Correct |
73 ms |
24188 KB |
Output is correct |
27 |
Correct |
249 ms |
24188 KB |
Output is correct |
28 |
Correct |
269 ms |
24188 KB |
Output is correct |
29 |
Correct |
266 ms |
24188 KB |
Output is correct |
30 |
Correct |
126 ms |
20092 KB |
Output is correct |
31 |
Correct |
43 ms |
24188 KB |
Output is correct |
32 |
Correct |
143 ms |
24188 KB |
Output is correct |
33 |
Correct |
146 ms |
24188 KB |
Output is correct |
34 |
Correct |
166 ms |
24188 KB |
Output is correct |
35 |
Correct |
73 ms |
24188 KB |
Output is correct |
36 |
Correct |
149 ms |
24188 KB |
Output is correct |
37 |
Correct |
46 ms |
24188 KB |
Output is correct |