#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define int ll
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int mxn = 2e5+10;
int N,K;
struct BIT{
ll bit[mxn];
BIT(){}
void modify(int p,int v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
ll getval(int s,int e){
ll re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
}
};
namespace S1{
vector<ll> v;
void GO(){
ll ans = 0;
for(int i = 0;i<N;i++){
char s1,s2;
int p1,p2;
cin>>s1>>p1>>s2>>p2;
if(s1 == s2)ans += abs(p2-p1);
else{
v.push_back(p1);
v.push_back(p2);
}
}
sort(v.begin(),v.end());
ll cen = 0;
if(!v.empty())cen = v[v.size()/2];
for(auto &i:v)ans += abs(cen-i);
ans += v.size()>>1;
cout<<ans<<'\n';
return;
}
}
namespace S2{
vector<int> all;
struct DS{
DS(){}
BIT cnt,sum;
ordered_set<pii> st;
int ptr = 0;
void add(int k){
st.insert(pii(k,ptr++));
cnt.modify(k,1);
sum.modify(k,all[k]);
}
void del(int k){
auto it = st.lower_bound(pii(k,-1));
st.erase(st.find(*it));
cnt.modify(k,-1);
sum.modify(k,-all[k]);
}
ll getval(){
if(st.empty())return 0;
ll cen = st.find_by_order(st.size()/2)->fs;
ll lsum = all[cen]*cnt.getval(0,cen)-sum.getval(0,cen);
ll rsum = sum.getval(cen+1,mxn-1)-all[cen]*cnt.getval(cen+1,mxn-1);
return lsum+rsum;
}
};
DS dl,dr;
vector<pll> v;
void GO(){
all.push_back(-1);
ll ans = 0;
for(int i = 0;i<N;i++){
char s1,s2;
int p1,p2;
cin>>s1>>p1>>s2>>p2;
if(s1 == s2){
ans += abs(p1-p2);
}
else{
if(p1>p2)swap(p1,p2);
all.push_back(p1);
all.push_back(p2);
v.push_back(pll(p1,p2));
}
}
sort(all.begin(),all.end());
all.erase(unique(all.begin(),all.end()),all.end());
sort(v.begin(),v.end(),[](pll a,pll b){return a.fs+a.sc<b.fs+b.sc;});
ans += v.size();
for(auto &i:v){
i.fs = lower_bound(all.begin(),all.end(),i.fs)-all.begin();
i.sc = lower_bound(all.begin(),all.end(),i.sc)-all.begin();
dr.add(i.fs);
dr.add(i.sc);
}
ll tans = 1e18;
tans = dl.getval()+dr.getval();
for(auto &i:v){
dr.del(i.fs);
dr.del(i.sc);
dl.add(i.fs);
dl.add(i.sc);
tans = min(tans,dl.getval()+dr.getval());
}
cout<<ans+tans<<'\n';
return;
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>K>>N;
if(K == 1)S1::GO();
else S2::GO();
}
Compilation message
bridge.cpp:129:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
129 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2408 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
20 ms |
4568 KB |
Output is correct |
13 |
Correct |
31 ms |
4536 KB |
Output is correct |
14 |
Correct |
23 ms |
4564 KB |
Output is correct |
15 |
Correct |
18 ms |
3548 KB |
Output is correct |
16 |
Correct |
19 ms |
4568 KB |
Output is correct |
17 |
Correct |
22 ms |
4564 KB |
Output is correct |
18 |
Correct |
27 ms |
4584 KB |
Output is correct |
19 |
Correct |
30 ms |
4568 KB |
Output is correct |
20 |
Correct |
21 ms |
4568 KB |
Output is correct |
21 |
Correct |
27 ms |
4568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4564 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
3 ms |
4700 KB |
Output is correct |
15 |
Correct |
3 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4588 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
3 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4608 KB |
Output is correct |
24 |
Correct |
3 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4564 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
3 ms |
4952 KB |
Output is correct |
15 |
Correct |
3 ms |
4696 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
3 ms |
4700 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4700 KB |
Output is correct |
25 |
Correct |
445 ms |
21088 KB |
Output is correct |
26 |
Correct |
289 ms |
21412 KB |
Output is correct |
27 |
Correct |
510 ms |
23888 KB |
Output is correct |
28 |
Correct |
485 ms |
24612 KB |
Output is correct |
29 |
Correct |
506 ms |
24688 KB |
Output is correct |
30 |
Correct |
208 ms |
14628 KB |
Output is correct |
31 |
Correct |
483 ms |
21392 KB |
Output is correct |
32 |
Correct |
500 ms |
24176 KB |
Output is correct |
33 |
Correct |
211 ms |
23836 KB |
Output is correct |
34 |
Correct |
553 ms |
24092 KB |
Output is correct |
35 |
Correct |
523 ms |
21784 KB |
Output is correct |
36 |
Correct |
540 ms |
24148 KB |
Output is correct |
37 |
Correct |
531 ms |
20912 KB |
Output is correct |