#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
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){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
5 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Correct |
4 ms |
6604 KB |
Output is correct |
9 |
Correct |
4 ms |
6660 KB |
Output is correct |
10 |
Correct |
4 ms |
6604 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
5 ms |
6604 KB |
Output is correct |
5 |
Correct |
5 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Correct |
4 ms |
6604 KB |
Output is correct |
9 |
Correct |
5 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6604 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
12 |
Correct |
110 ms |
15620 KB |
Output is correct |
13 |
Correct |
229 ms |
15716 KB |
Output is correct |
14 |
Correct |
222 ms |
14920 KB |
Output is correct |
15 |
Correct |
129 ms |
11692 KB |
Output is correct |
16 |
Correct |
97 ms |
15764 KB |
Output is correct |
17 |
Correct |
159 ms |
15672 KB |
Output is correct |
18 |
Correct |
164 ms |
15784 KB |
Output is correct |
19 |
Correct |
167 ms |
15600 KB |
Output is correct |
20 |
Correct |
113 ms |
15684 KB |
Output is correct |
21 |
Correct |
170 ms |
15788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
4 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
4 ms |
6476 KB |
Output is correct |
7 |
Correct |
4 ms |
6576 KB |
Output is correct |
8 |
Correct |
3 ms |
6476 KB |
Output is correct |
9 |
Correct |
4 ms |
6476 KB |
Output is correct |
10 |
Correct |
4 ms |
6476 KB |
Output is correct |
11 |
Correct |
4 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
4 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
8 |
Correct |
3 ms |
6476 KB |
Output is correct |
9 |
Correct |
3 ms |
6476 KB |
Output is correct |
10 |
Correct |
4 ms |
6476 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
13 |
Correct |
4 ms |
6604 KB |
Output is correct |
14 |
Correct |
4 ms |
6604 KB |
Output is correct |
15 |
Correct |
5 ms |
6604 KB |
Output is correct |
16 |
Correct |
5 ms |
6604 KB |
Output is correct |
17 |
Correct |
5 ms |
6604 KB |
Output is correct |
18 |
Correct |
4 ms |
6604 KB |
Output is correct |
19 |
Correct |
5 ms |
6604 KB |
Output is correct |
20 |
Correct |
5 ms |
6604 KB |
Output is correct |
21 |
Correct |
4 ms |
6604 KB |
Output is correct |
22 |
Correct |
5 ms |
6604 KB |
Output is correct |
23 |
Correct |
4 ms |
6604 KB |
Output is correct |
24 |
Correct |
4 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6476 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
4 ms |
6476 KB |
Output is correct |
8 |
Correct |
4 ms |
6476 KB |
Output is correct |
9 |
Correct |
4 ms |
6476 KB |
Output is correct |
10 |
Correct |
3 ms |
6476 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6476 KB |
Output is correct |
13 |
Correct |
4 ms |
6604 KB |
Output is correct |
14 |
Correct |
5 ms |
6604 KB |
Output is correct |
15 |
Correct |
4 ms |
6604 KB |
Output is correct |
16 |
Correct |
4 ms |
6604 KB |
Output is correct |
17 |
Correct |
4 ms |
6620 KB |
Output is correct |
18 |
Correct |
4 ms |
6604 KB |
Output is correct |
19 |
Correct |
5 ms |
6604 KB |
Output is correct |
20 |
Correct |
4 ms |
6604 KB |
Output is correct |
21 |
Correct |
5 ms |
6604 KB |
Output is correct |
22 |
Correct |
5 ms |
6604 KB |
Output is correct |
23 |
Correct |
4 ms |
6664 KB |
Output is correct |
24 |
Correct |
5 ms |
6604 KB |
Output is correct |
25 |
Correct |
103 ms |
15664 KB |
Output is correct |
26 |
Correct |
175 ms |
15616 KB |
Output is correct |
27 |
Correct |
246 ms |
15768 KB |
Output is correct |
28 |
Correct |
240 ms |
15664 KB |
Output is correct |
29 |
Correct |
231 ms |
15668 KB |
Output is correct |
30 |
Correct |
122 ms |
11356 KB |
Output is correct |
31 |
Correct |
100 ms |
15588 KB |
Output is correct |
32 |
Correct |
159 ms |
15616 KB |
Output is correct |
33 |
Correct |
158 ms |
15616 KB |
Output is correct |
34 |
Correct |
173 ms |
15776 KB |
Output is correct |
35 |
Correct |
103 ms |
15616 KB |
Output is correct |
36 |
Correct |
163 ms |
15648 KB |
Output is correct |
37 |
Correct |
95 ms |
15592 KB |
Output is correct |