#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename t>
using pbds = tree<t,null_type,less<t>,rb_tree_tag,tree_order_statistics_node_update>;
#define lc ind<<1
#define rc ind<<1|1
const int MN = 2e5 + 5;
vector<int> xs;
int getx (int x) {return lower_bound(xs.begin(),xs.end(),x) - xs.begin() + 1;}
long long bit[MN];
void update (int i, int v) {
for (;i<MN;i+=i&-i)
bit[i]+=v;
}
long long query (int i) {
long long ret = 0;
for (;i;i^=i&-i)
ret += bit[i];
return ret;
}
int main () {
int k,n; long long add = 0, ans = 8e18;
scanf ("%d %d",&k,&n); vector<pair<int,int>> v = {{-1,-1}};
for (int i = 1; i <= n; i++) {
char c1, c2; int a,b;
scanf (" %c %d %c %d",&c1,&a,&c2,&b);
if (a > b) swap(a,b);
if (c1 == c2) add += b - a;
else {
v.emplace_back(a,b);
xs.push_back(a); xs.push_back(b);
}
}
sort(xs.begin(),xs.end()); xs.erase(unique(xs.begin(),xs.end()),xs.end());
n = (int)v.size() - 1;
sort(v.begin()+1,v.end(),[](const auto &a, const auto &b){return a.first + a.second < b.first + b.second;});
//solve K = 1 for each prefix and suffix, merge
//slope starts at -N, each A/B adds 1, we want to find where it is 0 (because of convexity)
pbds<pair<int,int>> st; vector<long long> pre(n+1), suf(n+2); long long sum = 0;
for (int i = 1; i <= n; i++) {
st.insert({v[i].first,2*i}); st.insert({v[i].second,2*i+1});
update(getx(v[i].first),v[i].first); update(getx(v[i].second),v[i].second); sum += v[i].first + v[i].second;
int want = st.find_by_order(i)->first, cnt = st.order_of_key({want,-1});
long long lowSum = query(getx(want) - 1) + (long long)(i - cnt) * want;
long long highSum = sum - lowSum;
pre[i] = highSum - lowSum;
}
if (k == 1) return !printf ("%lld\n",pre[n]+add+n);
st.clear(); memset(bit,0,sizeof bit);sum = 0;
for (int i = n; i >= 1; i--) {
st.insert({v[i].first,2*i}); st.insert({v[i].second,2*i+1});
update(getx(v[i].first),v[i].first); update(getx(v[i].second),v[i].second); sum += v[i].first + v[i].second;
int want = st.find_by_order(n-i+1)->first, cnt = st.order_of_key({want,-1});
long long lowSum = query(getx(want) - 1) + (long long)(n - i + 1 - cnt) * want;
long long highSum = sum - lowSum;
suf[i] = highSum - lowSum;
}
for (int takePre = 0; takePre <= n; takePre++) {
ans = min(ans,add + pre[takePre] + suf[takePre+1] + n);
}
printf ("%lld\n",ans);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
26 | scanf ("%d %d",&k,&n); vector<pair<int,int>> v = {{-1,-1}};
| ~~~~~~^~~~~~~~~~~~~~~
bridge.cpp:29:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
29 | scanf (" %c %d %c %d",&c1,&a,&c2,&b);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
250 ms |
16820 KB |
Output is correct |
13 |
Correct |
413 ms |
19812 KB |
Output is correct |
14 |
Correct |
318 ms |
15776 KB |
Output is correct |
15 |
Correct |
220 ms |
11920 KB |
Output is correct |
16 |
Correct |
260 ms |
17632 KB |
Output is correct |
17 |
Correct |
285 ms |
19808 KB |
Output is correct |
18 |
Correct |
176 ms |
19488 KB |
Output is correct |
19 |
Correct |
297 ms |
19808 KB |
Output is correct |
20 |
Correct |
263 ms |
18020 KB |
Output is correct |
21 |
Correct |
284 ms |
19680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
1 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1920 KB |
Output is correct |
5 |
Correct |
1 ms |
1920 KB |
Output is correct |
6 |
Correct |
1 ms |
1920 KB |
Output is correct |
7 |
Correct |
2 ms |
1920 KB |
Output is correct |
8 |
Correct |
2 ms |
1920 KB |
Output is correct |
9 |
Correct |
2 ms |
1920 KB |
Output is correct |
10 |
Correct |
2 ms |
1920 KB |
Output is correct |
11 |
Correct |
1 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1920 KB |
Output is correct |
5 |
Correct |
2 ms |
1920 KB |
Output is correct |
6 |
Correct |
1 ms |
1920 KB |
Output is correct |
7 |
Correct |
2 ms |
1920 KB |
Output is correct |
8 |
Correct |
2 ms |
1920 KB |
Output is correct |
9 |
Correct |
2 ms |
1920 KB |
Output is correct |
10 |
Correct |
2 ms |
1920 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
3 ms |
2044 KB |
Output is correct |
14 |
Correct |
4 ms |
2048 KB |
Output is correct |
15 |
Correct |
4 ms |
2048 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
3 ms |
1912 KB |
Output is correct |
18 |
Correct |
3 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
2048 KB |
Output is correct |
20 |
Correct |
4 ms |
2048 KB |
Output is correct |
21 |
Correct |
3 ms |
2048 KB |
Output is correct |
22 |
Correct |
4 ms |
2176 KB |
Output is correct |
23 |
Correct |
4 ms |
2048 KB |
Output is correct |
24 |
Correct |
4 ms |
2048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1920 KB |
Output is correct |
5 |
Correct |
2 ms |
1920 KB |
Output is correct |
6 |
Correct |
1 ms |
1920 KB |
Output is correct |
7 |
Correct |
2 ms |
1920 KB |
Output is correct |
8 |
Correct |
2 ms |
1920 KB |
Output is correct |
9 |
Correct |
2 ms |
1920 KB |
Output is correct |
10 |
Correct |
2 ms |
1920 KB |
Output is correct |
11 |
Correct |
1 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
2048 KB |
Output is correct |
14 |
Correct |
4 ms |
2048 KB |
Output is correct |
15 |
Correct |
4 ms |
2048 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
3 ms |
1920 KB |
Output is correct |
18 |
Correct |
2 ms |
1920 KB |
Output is correct |
19 |
Correct |
3 ms |
2048 KB |
Output is correct |
20 |
Correct |
4 ms |
2048 KB |
Output is correct |
21 |
Correct |
4 ms |
2048 KB |
Output is correct |
22 |
Correct |
4 ms |
2048 KB |
Output is correct |
23 |
Correct |
3 ms |
2048 KB |
Output is correct |
24 |
Correct |
5 ms |
2048 KB |
Output is correct |
25 |
Correct |
365 ms |
18356 KB |
Output is correct |
26 |
Correct |
412 ms |
18580 KB |
Output is correct |
27 |
Correct |
673 ms |
19324 KB |
Output is correct |
28 |
Correct |
697 ms |
19984 KB |
Output is correct |
29 |
Correct |
618 ms |
20044 KB |
Output is correct |
30 |
Correct |
266 ms |
11500 KB |
Output is correct |
31 |
Correct |
375 ms |
19168 KB |
Output is correct |
32 |
Correct |
406 ms |
19888 KB |
Output is correct |
33 |
Correct |
288 ms |
19684 KB |
Output is correct |
34 |
Correct |
447 ms |
19888 KB |
Output is correct |
35 |
Correct |
363 ms |
19556 KB |
Output is correct |
36 |
Correct |
440 ms |
19684 KB |
Output is correct |
37 |
Correct |
362 ms |
18316 KB |
Output is correct |