#include <bits/stdc++.h>
typedef long long ll;
#define F first
#define S second
#define mp make_pair
using namespace std;
using pii = pair<int,int>;
const int N = 1e5 + 5, M = 2e5 + 5;
int sz, m, med[2][N], bit[M], cp[M];
ll BIT[M], Ans[2][N];
pii A[N];
int pos(int x){ return upper_bound(cp, cp+m, x) - cp;}
void upd(int i, int v){ for(; i<M; i+=i&-i) bit[i] += v;}
void UPD(int i, ll v){ for(; i<M; i+=i&-i) BIT[i] += v;}
int qry(int i){ int res = 0; for(; i; i-=i&-i) res += bit[i]; return res;}
ll QRY(int i){ ll res = 0; for(; i; i-=i&-i) res += BIT[i]; return res;}
int _find(int k){
int i = 0, sum = 0;
for(int j = 18; j>=0; --j){
if(i+(1<<j) < M && sum + bit[i+(1<<j)] < k) sum += bit[i+=(1<<j)];
}
return i + 1;
}
signed main(){
int k, n; scanf("%d %d", &k, &n);
ll tot = 0;
for(int i = 0, p[2]; i<n; ++i){
char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]);
if(c[0] == c[1]) tot += p[1] - p[0];
else{
cp[m++] = p[0]; cp[m++] = p[1];
A[sz++] = mp(p[0], p[1]);
}
}
auto cmp = [&](pii x, pii y){ return x.F + x.S < y.F + y.S;};
sort(A, A+sz, cmp); sort(cp, cp+m); m = unique(cp, cp+m) - cp;
int st = 0, ed = sz;
for(int j = 0; j<2; ++j){
memset(bit, 0, sizeof(bit)); memset(BIT, 0, sizeof(BIT));
for(int i = st, cnt = 0; i != ed; !j ? ++i : --i){
pii idx = mp(pos(A[i].F), pos(A[i].S));
upd(idx.F, +1); upd(idx.S, +1);
UPD(idx.F, +A[i].F); UPD(idx.S, +A[i].S);
cnt += 2;
int I = _find((cnt+1)/2);
Ans[j][i] = (ll)qry(I) * (ll)cp[I-1] - QRY(I) + QRY(M-1) - QRY(I) - (ll)(qry(M-1) - qry(I)) * cp[I-1];
}
st = sz-1; ed = -1;
}
tot += sz;
if(k == 1 || !sz){ printf("%lld", tot + Ans[0][sz-1]); return 0;}
ll mn = 2e18;
for(int i = 0; i+1<n; ++i) mn = min(mn, Ans[0][i] + Ans[1][i+1]);
printf("%lld", tot + mn);
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:25:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | int k, n; scanf("%d %d", &k, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:28:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2844 KB |
Output is correct |
12 |
Correct |
53 ms |
6484 KB |
Output is correct |
13 |
Correct |
122 ms |
8016 KB |
Output is correct |
14 |
Correct |
81 ms |
6736 KB |
Output is correct |
15 |
Correct |
67 ms |
5896 KB |
Output is correct |
16 |
Correct |
60 ms |
7508 KB |
Output is correct |
17 |
Correct |
74 ms |
8020 KB |
Output is correct |
18 |
Correct |
75 ms |
7808 KB |
Output is correct |
19 |
Correct |
85 ms |
8020 KB |
Output is correct |
20 |
Correct |
59 ms |
7544 KB |
Output is correct |
21 |
Correct |
81 ms |
7760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |