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;
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5;
int t[4*N],idx,sm[N],s[N],p[N],pos[N];
map<int,int>id;
void compress(vector<pii>v){
idx=0;
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
++idx;
id[v[i].s]=idx;pos[idx]=v[i].f;
}
}
void build(int u,int l,int r){
t[u]=0;sm[u]=0;
if(l==r)return;
int mid=(l+r)/2;
build(2*u,l,mid);build(2*u+1,mid+1,r);
}
void upd(int u,int id,int l,int r){
if(l==r){
t[u]++;
sm[u]+=pos[l];
return;
}
int mid=(l+r)/2;
if(id<=mid)upd(2*u,id,l,mid);
else upd(2*u+1,id,mid+1,r);
t[u]=t[2*u+1]+t[2*u];
sm[u]=sm[2*u+1]+sm[2*u];
}
int get(int u,int id,int l,int r){
if(l==r)return l;
int mid=(l+r)/2;
if(t[2*u]>=id)return get(2*u,id,l,mid);
return get(2*u+1,id-t[2*u],mid+1,r);
}
int sum(int u,int st,int en,int l,int r){
if(l>en||r<st)return 0;
if(st<=l&&r<=en)return sm[u];
int mid=(l+r)/2;
return sum(2*u,st,en,l,mid)+sum(2*u+1,st,en,mid+1,r);
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int k,n;
cin>>k>>n;
vector<pair<int,pii> > x;
vector<pii>v;
int X=0;
for(int i=1;i<=n;i++){
char c,cc;
int a,b;
cin>>c>>a>>cc>>b;
if(c==cc)X+=abs(b-a);
else{
idx+=2;
x.push_back({a+b,{idx-1,idx}});
v.push_back({a,idx-1});v.push_back({b,idx});
}
}
idx=0;
compress(v);
sort(x.begin(),x.end());
for(int i=0;i<x.size();i++){
upd(1,id[x[i].s.f],1,idx);
upd(1,id[x[i].s.s],1,idx);
int mid=get(1,(i+1),1,idx);
p[i]=-sum(1,1,mid,1,idx)+sum(1,mid+1,idx,1,idx);
}
int ans=p[x.size()-1];
if(k==1)cout<<p[x.size()-1]+X+(int)x.size();
else{
build(1,1,idx);
for(int i=(int)x.size()-1;i>=0;i--){
upd(1,id[x[i].s.f],1,idx);
upd(1,id[x[i].s.s],1,idx);
int mid=get(1,((int)x.size()-1-i+1),1,idx);
s[i]=-sum(1,1,mid,1,idx)+sum(1,mid+1,idx,1,idx);
if(i>0)ans=min(ans,s[i]+p[i-1]);
}
cout<<ans+X+(int)x.size();
}
}
Compilation message (stderr)
bridge.cpp: In function 'void compress(std::vector<std::pair<long long int, long long int> >)':
bridge.cpp:14:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
bridge.cpp: At global scope:
bridge.cpp:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main(){
| ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:70:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0;i<x.size();i++){
| ~^~~~~~~~~
# | 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... |