#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 1e5 + 5;
struct segment
{
int x, y;
bool operator < (const segment &other) const
{
return x + y < other.x + other.y;
}
};
vector<segment> v;
map<int,int> mp;
ll dp1[Nmax], dp2[Nmax];
ll ans = 0;
int ord[2*Nmax];
struct AIB
{
int a[2*Nmax];
ll b[2*Nmax];
int ub(int x) { return (x&(-x)); }
void clear()
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
}
void upd(int pos, int A, int B)
{
for(; pos<=mp.size(); pos+=ub(pos))
a[pos] += A, b[pos] += B;
}
ll query(int Pos)
{
int A = 0, pos = Pos; ll B = 0;
for(; pos; pos-=ub(pos))
A += a[pos], B += b[pos];
return (ll) A * ord[Pos] + B;
}
void update(int X, int Y)
{
int x = mp[X], y = mp[Y];
upd(y, 1, -Y);
upd(1, -1, X);
upd(x, 1, -X);
}
} aib;
void init()
{
sort(v.begin(), v.end());
int cnt = 0;
for(auto &it : mp)
{
it.second = ++cnt;
ord[cnt] = it.first;
}
}
void prefix()
{
aib.clear();
int i, id = 1;
for(i=0; i<v.size(); ++i)
{
aib.update(v[i].x, v[i].y);
while(id<mp.size() && aib.query(id) > aib.query(id+1)) ++id;
dp1[i] = aib.query(id);
}
}
void suffix()
{
aib.clear();
int i, id = mp.size();
for(i=v.size()-1; i>=0; --i)
{
aib.update(v[i].x, v[i].y);
while(id>1 && aib.query(id) > aib.query(id-1)) --id;
dp2[i] = aib.query(id);
}
}
int main()
{
// freopen("input", "r", stdin);
int i, N, K, x, y;
char tip1, tip2;
cin >> K >> N;
for(i=1; i<=N; ++i)
{
cin >> tip1 >> x >> tip2 >> y;
if(x > y) swap(x, y);
ans += y - x;
if(tip1 != tip2)
{
++ans;
v.push_back({x, y});
mp[x] = 1; mp[y] = 1;
}
}
if(v.empty())
{
cout << ans << '\n';
return 0;
}
init();
prefix();
suffix();
K = min(K, (int) mp.size());
K = min(K, (int) v.size());
ll best;
best = dp1[v.size()-1];
if(K == 2)
for(i=0; i<(int) v.size()-1; ++i) best = min(best, dp1[i] + dp2[i+1]);
cout << 2 * best + ans << '\n';
return 0;
}
Compilation message
bridge.cpp: In member function 'void AIB::upd(int, int, int)':
bridge.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; pos<=mp.size(); pos+=ub(pos))
~~~^~~~~~~~~~~
bridge.cpp: In function 'void prefix()':
bridge.cpp:76:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<v.size(); ++i)
~^~~~~~~~~
bridge.cpp:79:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(id<mp.size() && aib.query(id) > aib.query(id+1)) ++id;
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
2840 KB |
Output is correct |
4 |
Correct |
6 ms |
2908 KB |
Output is correct |
5 |
Correct |
6 ms |
2908 KB |
Output is correct |
6 |
Correct |
5 ms |
2908 KB |
Output is correct |
7 |
Correct |
6 ms |
2924 KB |
Output is correct |
8 |
Correct |
7 ms |
2924 KB |
Output is correct |
9 |
Correct |
6 ms |
2924 KB |
Output is correct |
10 |
Correct |
5 ms |
2924 KB |
Output is correct |
11 |
Correct |
6 ms |
3112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3112 KB |
Output is correct |
2 |
Correct |
2 ms |
3112 KB |
Output is correct |
3 |
Correct |
4 ms |
3112 KB |
Output is correct |
4 |
Correct |
9 ms |
3136 KB |
Output is correct |
5 |
Correct |
6 ms |
3184 KB |
Output is correct |
6 |
Correct |
5 ms |
3184 KB |
Output is correct |
7 |
Correct |
6 ms |
3184 KB |
Output is correct |
8 |
Correct |
6 ms |
3184 KB |
Output is correct |
9 |
Correct |
6 ms |
3196 KB |
Output is correct |
10 |
Correct |
5 ms |
3196 KB |
Output is correct |
11 |
Correct |
6 ms |
3240 KB |
Output is correct |
12 |
Correct |
72 ms |
6444 KB |
Output is correct |
13 |
Correct |
525 ms |
18752 KB |
Output is correct |
14 |
Correct |
204 ms |
18752 KB |
Output is correct |
15 |
Correct |
300 ms |
18752 KB |
Output is correct |
16 |
Correct |
108 ms |
18752 KB |
Output is correct |
17 |
Correct |
325 ms |
25420 KB |
Output is correct |
18 |
Correct |
310 ms |
27216 KB |
Output is correct |
19 |
Correct |
364 ms |
29508 KB |
Output is correct |
20 |
Correct |
113 ms |
29508 KB |
Output is correct |
21 |
Correct |
304 ms |
33584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
33584 KB |
Output is correct |
2 |
Correct |
2 ms |
33584 KB |
Output is correct |
3 |
Correct |
4 ms |
33584 KB |
Output is correct |
4 |
Correct |
4 ms |
33584 KB |
Output is correct |
5 |
Correct |
4 ms |
33584 KB |
Output is correct |
6 |
Correct |
4 ms |
33584 KB |
Output is correct |
7 |
Correct |
4 ms |
33584 KB |
Output is correct |
8 |
Correct |
5 ms |
33584 KB |
Output is correct |
9 |
Correct |
4 ms |
33584 KB |
Output is correct |
10 |
Correct |
4 ms |
33584 KB |
Output is correct |
11 |
Correct |
4 ms |
33584 KB |
Output is correct |
12 |
Correct |
4 ms |
33584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
33584 KB |
Output is correct |
2 |
Correct |
2 ms |
33584 KB |
Output is correct |
3 |
Correct |
4 ms |
33584 KB |
Output is correct |
4 |
Correct |
4 ms |
33584 KB |
Output is correct |
5 |
Correct |
4 ms |
33584 KB |
Output is correct |
6 |
Correct |
4 ms |
33584 KB |
Output is correct |
7 |
Correct |
5 ms |
33584 KB |
Output is correct |
8 |
Correct |
5 ms |
33584 KB |
Output is correct |
9 |
Correct |
4 ms |
33584 KB |
Output is correct |
10 |
Correct |
4 ms |
33584 KB |
Output is correct |
11 |
Correct |
4 ms |
33584 KB |
Output is correct |
12 |
Correct |
4 ms |
33584 KB |
Output is correct |
13 |
Correct |
5 ms |
33584 KB |
Output is correct |
14 |
Correct |
5 ms |
33584 KB |
Output is correct |
15 |
Correct |
6 ms |
33584 KB |
Output is correct |
16 |
Correct |
4 ms |
33584 KB |
Output is correct |
17 |
Correct |
5 ms |
33584 KB |
Output is correct |
18 |
Correct |
5 ms |
33584 KB |
Output is correct |
19 |
Correct |
5 ms |
33584 KB |
Output is correct |
20 |
Correct |
6 ms |
33584 KB |
Output is correct |
21 |
Correct |
6 ms |
33584 KB |
Output is correct |
22 |
Correct |
7 ms |
33584 KB |
Output is correct |
23 |
Correct |
5 ms |
33584 KB |
Output is correct |
24 |
Correct |
6 ms |
33584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
33584 KB |
Output is correct |
2 |
Correct |
2 ms |
33584 KB |
Output is correct |
3 |
Correct |
4 ms |
33584 KB |
Output is correct |
4 |
Correct |
4 ms |
33584 KB |
Output is correct |
5 |
Correct |
4 ms |
33584 KB |
Output is correct |
6 |
Correct |
4 ms |
33584 KB |
Output is correct |
7 |
Correct |
5 ms |
33584 KB |
Output is correct |
8 |
Correct |
4 ms |
33584 KB |
Output is correct |
9 |
Correct |
4 ms |
33584 KB |
Output is correct |
10 |
Correct |
4 ms |
33584 KB |
Output is correct |
11 |
Correct |
4 ms |
33584 KB |
Output is correct |
12 |
Correct |
4 ms |
33584 KB |
Output is correct |
13 |
Correct |
5 ms |
33584 KB |
Output is correct |
14 |
Correct |
5 ms |
33584 KB |
Output is correct |
15 |
Correct |
7 ms |
33584 KB |
Output is correct |
16 |
Correct |
4 ms |
33584 KB |
Output is correct |
17 |
Correct |
5 ms |
33584 KB |
Output is correct |
18 |
Correct |
5 ms |
33584 KB |
Output is correct |
19 |
Correct |
5 ms |
33584 KB |
Output is correct |
20 |
Correct |
6 ms |
33584 KB |
Output is correct |
21 |
Correct |
7 ms |
33584 KB |
Output is correct |
22 |
Correct |
6 ms |
33584 KB |
Output is correct |
23 |
Correct |
5 ms |
33584 KB |
Output is correct |
24 |
Correct |
6 ms |
33584 KB |
Output is correct |
25 |
Correct |
73 ms |
33584 KB |
Output is correct |
26 |
Correct |
108 ms |
33584 KB |
Output is correct |
27 |
Correct |
492 ms |
36772 KB |
Output is correct |
28 |
Correct |
521 ms |
40088 KB |
Output is correct |
29 |
Correct |
518 ms |
42308 KB |
Output is correct |
30 |
Correct |
215 ms |
42308 KB |
Output is correct |
31 |
Correct |
160 ms |
42308 KB |
Output is correct |
32 |
Correct |
304 ms |
47604 KB |
Output is correct |
33 |
Correct |
302 ms |
49436 KB |
Output is correct |
34 |
Correct |
355 ms |
51336 KB |
Output is correct |
35 |
Correct |
116 ms |
51336 KB |
Output is correct |
36 |
Correct |
279 ms |
55720 KB |
Output is correct |
37 |
Correct |
116 ms |
55720 KB |
Output is correct |