#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#define int long long
using namespace std;
const int mx=200000;
struct T{
int cnt,sum;
}st[4*mx][2];
int k,n,s,t,res,id,val[mx+1],mn;
char p,q;
vector <pair <int, int> > v;
vector <int> v2;
map <int, int> mp;
bool cmp(pair <int, int> a, pair <int, int> b){
return a.first+a.second<b.first+b.second;
}
void update(int node, int l, int r, int x, int v, int idx){
if (l>x||r<x||l>r)
return;
if (l==r){
st[node][idx].cnt+=v;
st[node][idx].sum+=v*val[l];
return;
}
int mid=(l+r)>>1;
update(node<<1,l,mid,x,v,idx);
update(node<<1|1,mid+1,r,x,v,idx);
st[node][idx].sum=st[node<<1][idx].sum+st[node<<1|1][idx].sum;
st[node][idx].cnt=st[node<<1][idx].cnt+st[node<<1|1][idx].cnt;
}
int get(int node, int l, int r, int x, int idx){
if (l==r)
return (st[node][idx].cnt-x*2+2)*val[l];
int mid=(l+r)>>1;
if (st[node<<1][idx].cnt>=x)
return st[node<<1|1][idx].sum+get(node<<1,l,mid,x,idx);
return get(node<<1|1,mid+1,r,x-st[node<<1][idx].cnt,idx)-st[node<<1][idx].sum;
}
signed main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> k >> n;
for (int i=0;i<n;i++){
cin >> p >> s >> q >> t;
if (p==q){
res+=abs(s-t);
continue;
}
v.push_back(make_pair(min(s,t),max(s,t)));
}
res+=v.size();
for (auto i:v){
v2.push_back(i.first);
v2.push_back(i.second);
}
sort(v2.begin(),v2.end());
if (k==1){
for (int i:v2)
res+=abs(i-v2[v2.size()/2]);
cout << res;
return 0;
}
for (int i:v2)
if (!mp.count(i)){
mp[i]=++id;
val[id]=i;
}
sort(v.begin(),v.end(),cmp);
for (auto i:v){
update(1,1,mx,mp[i.first],1,1);
update(1,1,mx,mp[i.second],1,1);
}
mn=get(1,1,mx,st[1][1].cnt/2+1,1);
for (auto i:v){
update(1,1,mx,mp[i.first],1,0);
update(1,1,mx,mp[i.second],1,0);
update(1,1,mx,mp[i.first],-1,1);
update(1,1,mx,mp[i.second],-1,1);
mn=min(mn,get(1,1,mx,st[1][0].cnt/2+1,0)+get(1,1,mx,st[1][1].cnt/2+1,1));
}
cout << res+mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
23 ms |
5352 KB |
Output is correct |
13 |
Correct |
43 ms |
5484 KB |
Output is correct |
14 |
Correct |
31 ms |
5068 KB |
Output is correct |
15 |
Correct |
27 ms |
2928 KB |
Output is correct |
16 |
Correct |
30 ms |
5500 KB |
Output is correct |
17 |
Correct |
29 ms |
5464 KB |
Output is correct |
18 |
Correct |
41 ms |
5316 KB |
Output is correct |
19 |
Correct |
52 ms |
5404 KB |
Output is correct |
20 |
Correct |
29 ms |
5312 KB |
Output is correct |
21 |
Correct |
37 ms |
5432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
464 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
3 ms |
740 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
464 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
117 ms |
5068 KB |
Output is correct |
26 |
Correct |
145 ms |
5172 KB |
Output is correct |
27 |
Correct |
558 ms |
31332 KB |
Output is correct |
28 |
Correct |
564 ms |
35496 KB |
Output is correct |
29 |
Correct |
598 ms |
35520 KB |
Output is correct |
30 |
Correct |
275 ms |
19340 KB |
Output is correct |
31 |
Correct |
128 ms |
6440 KB |
Output is correct |
32 |
Correct |
279 ms |
35392 KB |
Output is correct |
33 |
Correct |
311 ms |
35436 KB |
Output is correct |
34 |
Correct |
299 ms |
34124 KB |
Output is correct |
35 |
Correct |
120 ms |
6420 KB |
Output is correct |
36 |
Correct |
298 ms |
35500 KB |
Output is correct |
37 |
Correct |
110 ms |
5708 KB |
Output is correct |