#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() (rand() >> 3)*rand()
int k, n, x[100005], y[100005], t, c1, c2, p=0, d;
ll ans, curr=0, ans2;
char ch, ch2;
vector<int> v;
multiset<pii> M1, R1, L2, M2, R2;
int32_t main(){
cin >> k >> n;
fox(l, n){
cin >> ch >> x[d] >> ch2 >> y[d];
if (x[d]>y[d]){t=x[d]; x[d]=y[d]; y[d]=t;}
ans+=y[d]-x[d];
if (ch!=ch2){
v.pb(x[d]);
v.pb(y[d]);
curr+=x[d];
--c2;
ans++;
R2.insert(mp(x[d], y[d]));
++d;
}
}
//cout << d << endl;
if (n<=100 && k==2){
ans2=(1LL << 60);
if (v.size()==0) ans2=0;
fox(l, v.size()){
for(int l2=l; l2<v.size(); ++l2){
curr=0;
fox(l3, d){
curr+=max(0, min(max(v[l]-y[l3], x[l3]-v[l]), max(v[l2]-y[l3], x[l3]-v[l2])));
}
ans2=min(ans2, curr);
}
}
cout << ans+ans2*2;
return 0;
}
ans2=curr;
v.pb(0);
sort(v.begin(), v.end());
fox1(l, v.size()-1){
curr+=1LL*c2*(v[l]-v[l-1]);
//cout << v[l] << ' ' << curr << endl;
while(R2.size() && R2.begin()->x<=v[l]){
M2.insert(mp(R2.begin()->y, R2.begin()->x));
R2.erase(R2.begin());
++c2;
}
while(M2.size() && M2.begin()->x<=v[l]){
++c2;
L2.insert(mp(M2.begin()->y+M2.begin()->x, M2.begin()->y));
M2.erase(M2.begin());
}
if (k==1){
ans2=min(ans2, curr);
continue;
}
while(L2.size() && v[p]>=L2.begin()->x-v[l]){
--c1; --c2;
if (!(v[p]>=L2.begin()->y))
curr+=L2.begin()->x-v[l]-v[p];
R1.insert(mp(L2.begin()->y, L2.begin()->x-L2.begin()->y));
L2.erase(L2.begin());
}
while(R1.size() && R1.begin()->x<=v[p]){
M1.insert(mp(R1.begin()->y, R1.begin()->x));
R1.erase(R1.begin());
++c1;
}
while(M1.size() && M1.begin()->x<=v[p]){
++c1;
M1.erase(M1.begin());
}
while(c1<=0 && p<l-1){
curr+=1LL*(v[p+1]-v[p])*c1; ++p;
while(L2.size() && v[p]>=L2.begin()->x-v[l]){
--c1; --c2;
if (!(v[p]>=L2.begin()->y))
curr+=L2.begin()->x-v[l]-v[p];
R1.insert(mp(L2.begin()->y, L2.begin()->x-L2.begin()->y));
L2.erase(L2.begin());
}
while(R1.size() && R1.begin()->x<=v[p]){
M1.insert(mp(R1.begin()->y, R1.begin()->x));
R1.erase(R1.begin());
++c1;
}
while(M1.size() && M1.begin()->x<=v[p]){
++c1;
M1.erase(M1.begin());
}
}
/*if (curr<ans2){
cout << v[p] << ' ' << v[l] << ' ' << c1 << ' ' << c2 << ' ' << curr << endl;
}*/
ans2=min(ans2, curr);
}
//cout << ans << ' ' << ans2 << endl;
cout << ans+ans2*2;
return 0;
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
^
bridge.cpp:52:9: note: in expansion of macro 'fox'
fox(l, v.size()){
^
bridge.cpp:53:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int l2=l; l2<v.size(); ++l2){
^
bridge.cpp:19:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox1(k, x) for (int k=1; k<=x; ++k)
^
bridge.cpp:67:5: note: in expansion of macro 'fox1'
fox1(l, v.size()-1){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2940 KB |
Output is correct |
4 |
Correct |
2 ms |
2940 KB |
Output is correct |
5 |
Correct |
0 ms |
2940 KB |
Output is correct |
6 |
Correct |
2 ms |
2940 KB |
Output is correct |
7 |
Correct |
0 ms |
2940 KB |
Output is correct |
8 |
Correct |
2 ms |
2940 KB |
Output is correct |
9 |
Correct |
2 ms |
2940 KB |
Output is correct |
10 |
Correct |
2 ms |
2940 KB |
Output is correct |
11 |
Correct |
2 ms |
2940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2940 KB |
Output is correct |
4 |
Correct |
2 ms |
2940 KB |
Output is correct |
5 |
Correct |
2 ms |
2940 KB |
Output is correct |
6 |
Correct |
2 ms |
2940 KB |
Output is correct |
7 |
Correct |
2 ms |
2940 KB |
Output is correct |
8 |
Correct |
3 ms |
2940 KB |
Output is correct |
9 |
Correct |
0 ms |
2940 KB |
Output is correct |
10 |
Correct |
2 ms |
2940 KB |
Output is correct |
11 |
Correct |
0 ms |
2940 KB |
Output is correct |
12 |
Correct |
122 ms |
8588 KB |
Output is correct |
13 |
Correct |
337 ms |
8588 KB |
Output is correct |
14 |
Correct |
245 ms |
8060 KB |
Output is correct |
15 |
Correct |
169 ms |
6096 KB |
Output is correct |
16 |
Correct |
221 ms |
8588 KB |
Output is correct |
17 |
Correct |
208 ms |
8588 KB |
Output is correct |
18 |
Correct |
196 ms |
8588 KB |
Output is correct |
19 |
Correct |
248 ms |
8588 KB |
Output is correct |
20 |
Correct |
167 ms |
8588 KB |
Output is correct |
21 |
Correct |
171 ms |
8588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2808 KB |
Output is correct |
3 |
Correct |
8 ms |
2808 KB |
Output is correct |
4 |
Correct |
8 ms |
2808 KB |
Output is correct |
5 |
Correct |
2 ms |
2808 KB |
Output is correct |
6 |
Correct |
0 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
9 ms |
2808 KB |
Output is correct |
9 |
Correct |
13 ms |
2808 KB |
Output is correct |
10 |
Correct |
14 ms |
2808 KB |
Output is correct |
11 |
Correct |
11 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2808 KB |
Output is correct |
4 |
Correct |
8 ms |
2808 KB |
Output is correct |
5 |
Correct |
2 ms |
2808 KB |
Output is correct |
6 |
Correct |
0 ms |
2808 KB |
Output is correct |
7 |
Correct |
6 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
10 ms |
2808 KB |
Output is correct |
10 |
Correct |
11 ms |
2808 KB |
Output is correct |
11 |
Correct |
8 ms |
2808 KB |
Output is correct |
12 |
Correct |
11 ms |
2808 KB |
Output is correct |
13 |
Correct |
1 ms |
2940 KB |
Output is correct |
14 |
Correct |
2 ms |
2940 KB |
Output is correct |
15 |
Correct |
3 ms |
2940 KB |
Output is correct |
16 |
Correct |
1 ms |
2808 KB |
Output is correct |
17 |
Correct |
1 ms |
2808 KB |
Output is correct |
18 |
Correct |
1 ms |
2808 KB |
Output is correct |
19 |
Correct |
2 ms |
2940 KB |
Output is correct |
20 |
Correct |
2 ms |
2940 KB |
Output is correct |
21 |
Correct |
2 ms |
2940 KB |
Output is correct |
22 |
Correct |
2 ms |
2940 KB |
Output is correct |
23 |
Correct |
2 ms |
2940 KB |
Output is correct |
24 |
Correct |
2 ms |
2940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2808 KB |
Output is correct |
4 |
Correct |
8 ms |
2808 KB |
Output is correct |
5 |
Correct |
2 ms |
2808 KB |
Output is correct |
6 |
Correct |
0 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
8 ms |
2808 KB |
Output is correct |
9 |
Correct |
8 ms |
2808 KB |
Output is correct |
10 |
Correct |
10 ms |
2808 KB |
Output is correct |
11 |
Correct |
8 ms |
2808 KB |
Output is correct |
12 |
Correct |
11 ms |
2808 KB |
Output is correct |
13 |
Correct |
1 ms |
2940 KB |
Output is correct |
14 |
Correct |
2 ms |
2940 KB |
Output is correct |
15 |
Correct |
2 ms |
2940 KB |
Output is correct |
16 |
Correct |
0 ms |
2808 KB |
Output is correct |
17 |
Correct |
1 ms |
2808 KB |
Output is correct |
18 |
Correct |
1 ms |
2808 KB |
Output is correct |
19 |
Correct |
1 ms |
2940 KB |
Output is correct |
20 |
Correct |
2 ms |
2940 KB |
Output is correct |
21 |
Correct |
2 ms |
2940 KB |
Output is correct |
22 |
Correct |
2 ms |
2940 KB |
Output is correct |
23 |
Correct |
2 ms |
2940 KB |
Output is correct |
24 |
Correct |
2 ms |
2940 KB |
Output is correct |
25 |
Correct |
151 ms |
8588 KB |
Output is correct |
26 |
Correct |
254 ms |
8588 KB |
Output is correct |
27 |
Correct |
320 ms |
8588 KB |
Output is correct |
28 |
Correct |
371 ms |
8588 KB |
Output is correct |
29 |
Correct |
368 ms |
8588 KB |
Output is correct |
30 |
Correct |
161 ms |
5832 KB |
Output is correct |
31 |
Correct |
243 ms |
8588 KB |
Output is correct |
32 |
Correct |
207 ms |
8588 KB |
Output is correct |
33 |
Correct |
208 ms |
8588 KB |
Output is correct |
34 |
Correct |
310 ms |
8588 KB |
Output is correct |
35 |
Correct |
179 ms |
8588 KB |
Output is correct |
36 |
Correct |
216 ms |
8588 KB |
Output is correct |
37 |
Correct |
214 ms |
8588 KB |
Output is correct |