#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;
vector<ll> cp;
struct segtree {
ll val[888888], lim;
void init () {
for(lim=1;lim<=m;lim<<=1);
for(ll i=2*lim;--i;) val[i] = 0;
}
void upd (ll P, ll V) {
P += lim;
while(P) {
val[P] += V;
P >>= 1;
}
}
ll get (ll S, ll E) {
S += lim; E += lim;
ll ret = 0;
while(S<=E) {
if(S%2==1) ret += val[S++];
if(E%2==0) ret += val[E--];
S>>=1; E>>=1;
}
return ret;
}
ll kth (ll K) {
ll P = 1;
while(P < lim) {
if(val[2*P] >= K) P = 2*P;
else {K -= val[2*P]; P = 2*P+1;}
}
return P - lim;
}
} cs, vs;
void solve1() {
vector<ll> a;
ll ans = 0;
for(int 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]) ans += abs(A-B);
else {a.push_back(A); a.push_back(B);}
}
sort(a.begin(), a.end());
ans += a.size()/2;
for(ll i=0;i<a.size();i++) {
ans += abs(a[i] - a[a.size()/2]);
}
printf("%lld\n",ans);
}
ll cpr (ll X) {
return lower_bound(cp.begin(), cp.end(), X) - cp.begin();
}
void getdp (vector<ll> &D, vector<tp> &V) {
cs.init(); vs.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);
cs.upd(A, 1); vs.upd(A, cp[A]);
cs.upd(B, 1); vs.upd(B, cp[B]);
ll M = cs.kth(++I);
D.push_back(vs.get(M+1, m-1)-vs.get(0, M));
}
}
void solve2() {
vector<tp> a;
ll sub = 0;
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(cp.begin(), cp.end());
cp.erase(unique(cp.begin(), cp.end()), cp.end());
m = cp.size();
n = a.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",ans+sub);
}
int main()
{
scanf("%lld%lld",&x,&n);
if(x == 1) solve1();
else solve2();
}
Compilation message
bridge.cpp: In function 'void solve1()':
bridge.cpp:54:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i=0;i<a.size();i++) {
^
bridge.cpp:48: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);
^
bridge.cpp: In function 'void solve2()':
bridge.cpp:86: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);
^
bridge.cpp: In function 'int main()':
bridge.cpp:117:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&x,&n);
^
# |
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 |
# |
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 |
33 ms |
19068 KB |
Output is correct |
13 |
Correct |
69 ms |
19068 KB |
Output is correct |
14 |
Correct |
49 ms |
19068 KB |
Output is correct |
15 |
Correct |
36 ms |
17532 KB |
Output is correct |
16 |
Correct |
53 ms |
19068 KB |
Output is correct |
17 |
Correct |
53 ms |
19068 KB |
Output is correct |
18 |
Correct |
66 ms |
19068 KB |
Output is correct |
19 |
Correct |
76 ms |
19068 KB |
Output is correct |
20 |
Correct |
69 ms |
19068 KB |
Output is correct |
21 |
Correct |
66 ms |
19068 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 |
Incorrect |
0 ms |
15912 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
15912 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
15912 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |