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 dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=2e5+99,inf=1e9;
int k,n,sum,ans,Sum[2][N],Cnt[2][N];
vector<pair<int,int>> a,b;
void addsum(int x,int val,int s){
for(x++;x<N;x+=x&-x) Sum[s][x]+=val;
}
void addcnt(int x,int val,int s){
for(x++;x<N;x+=x&-x) Cnt[s][x]+=val;
}
int getsum(int x,int s){
int res=0;
for(x++;x;x-=x&-x) res+=Sum[s][x];
return res;
}
int getcnt(int x,int s){
int res=0;
for(x++;x;x-=x&-x) res+=Cnt[s][x];
return res;
}
bool cmp(pair<int,int> i,pair<int,int> j){
return i.F+i.S<j.F+j.S;
}
vector<int> solve(vector<pair<int,int>> a){
f(s,0,2) f(i,0,N) Sum[s][i]=Cnt[s][i]=0;
vector<int> res,vec;
f(i,0,a.size()) vec.pb(a[i].F),vec.pb(a[i].S);
sort(all(vec));
f(i,0,a.size()){
int posl=lower_bound(all(vec),a[i].F)-vec.begin(),posr=lower_bound(all(vec),a[i].S)-vec.begin();
addcnt(posl,1,1);
addcnt(posr,1,0);
addsum(posl,a[i].F,1);
addsum(posr,a[i].S,0);
int l=-1,r=vec.size();
while(l+1<r){
int mid=(l+r)>>1;
if(getcnt(mid,0)<getcnt(vec.size()-1,1)-getcnt(mid,1)){
l=mid;
}
else{
r=mid;
}
}
l=r;
int pos=vec[l];
res.pb(pos*getcnt(l,0)-getsum(l,0)+(getsum(vec.size()-1,1)-getsum(l,1))-pos*(getcnt(vec.size()-1,1)-getcnt(l,1)));
}
return res;
}
main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
cin>>k>>n;
f(i,0,n){
char c1,c2;
int l,r;
cin>>c1>>l>>c2>>r;
if(l>r) swap(l,r);
sum+=r-l;
if(c1==c2) continue ;
sum++;
a.pb({l,r});
}
if(a.size()==0) cout<<sum,exit(0);
sort(all(a),cmp); b=a;
reverse(all(b));
f(i,0,b.size()) b[i].F=inf-b[i].F,b[i].S=inf-b[i].S,swap(b[i].F,b[i].S);
vector<int> A=solve(a),B=solve(b);
ans=A.back();
f(i,0,A.size()-1){
minm(ans,A[i]+B[B.size()-i-2]);
}
if(k==1) cout<<A.back()+A.back()+sum;
else cout<<ans+ans+sum;
}
Compilation message (stderr)
bridge.cpp: In function 'std::vector<long long int> solve(std::vector<std::pair<long long int, long long int> >)':
bridge.cpp:8:31: 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]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
51 | f(i,0,a.size()) vec.pb(a[i].F),vec.pb(a[i].S);
| ~~~~~~~~~~~~
bridge.cpp:51:2: note: in expansion of macro 'f'
51 | f(i,0,a.size()) vec.pb(a[i].F),vec.pb(a[i].S);
| ^
bridge.cpp:8:31: 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]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
54 | f(i,0,a.size()){
| ~~~~~~~~~~~~
bridge.cpp:54:2: note: in expansion of macro 'f'
54 | f(i,0,a.size()){
| ^
bridge.cpp: At global scope:
bridge.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main(){
| ^~~~
bridge.cpp: In function 'int main()':
bridge.cpp:8:31: 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]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
94 | f(i,0,b.size()) b[i].F=inf-b[i].F,b[i].S=inf-b[i].S,swap(b[i].F,b[i].S);
| ~~~~~~~~~~~~
bridge.cpp:94:2: note: in expansion of macro 'f'
94 | f(i,0,b.size()) b[i].F=inf-b[i].F,b[i].S=inf-b[i].S,swap(b[i].F,b[i].S);
| ^
bridge.cpp:8:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
98 | f(i,0,A.size()-1){
| ~~~~~~~~~~~~~~
bridge.cpp:98:2: note: in expansion of macro 'f'
98 | f(i,0,A.size()-1){
| ^
# | 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... |