This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |