/***Farhan132***/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm;
typedef tuple < int, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
//const ll mod = (ll) 998244353;
const ll mod = (ll) 1e9 + 7;
const ll maxn = (ll)1e8 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)1e18;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
bool USACO = 0;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 2e5 + 5;
struct fenwick{
ll ft[N];
void t(){
mem(ft, 0);
return;
}
void up(ll p, ll x) {
for(; p < N; p += p & -p)
ft[p] += x;
}
ll sum(ll l, ll r) {
ll ret = 0;
for(--l; l > 0; l -= l & -l) ret -= ft[l];
for(; r > 0; r -= r & -r) ret += ft[r];
return ret;
}
}sum1,sum2,c1,c2;
ll pref[N], suf[N];
ll pos(vector < ll >& v, ll key){
return lower_bound(v.begin(), v.end(), key) - v.begin();
}
ll cost(ll x, ll w){
return w * c1.sum(1, x-1) - sum1.sum(1, x-1) + sum2.sum(x + 1, N - 2) - w * c2.sum(x + 1, N - 2);
}
bool comp(ii x, ii y){
auto [l1, r1] = x;
auto [l2, r2] = y;
if(x == y) return 0;
if(r1 <= l2) return 1;
if(r2 <= l1) return 0;
if(l1 <= l2 && r2 > r1) return 1;
if(l2 <= l1 && r1 > r2) return 0;
if(l1 <= l2 && r2 <= r1){
return (l2 - l1) > (r1 - r2);
}else{
assert(l2 <= l1 && r1 <= r2);
swap(l1, l2);
swap(r1, r2);
return !((l2 - l1) > (r1 - r2));
}
return 0;
}
void solve(){
ll k , n;
cin >> k >> n;
ll ans = 0;
vector < ii > v;
vector < ll > L , R;
vector < ll > t;
for(ll i = 1; i <= n; i++){
char A , B; ll a , b;
cin >> A >> a >> B >> b;
ans += abs(a - b) + (A != B);
if(A != B){
if(a > b) swap(a, b);
v.pb({a, b});
L.pb(a);
R.pb(b);
t.pb(a); t.pb(b);
}
}
if(!t.size()){
cout << ans << '\n';
return;
}
sort(v.begin(), v.end(), comp);
sort(L.begin() , L.end());
L.erase(unique(L.begin(), L.end()),L.end());
sort(R.begin() , R.end());
R.erase(unique(R.begin(), R.end()),R.end());
sort(t.begin(), t.end());
t.erase(unique(t.begin(), t.end()),t.end());
for(ll i = 1; i < v.size(); i++) pref[i] = 1e18;
ll cur = 1;
pref[0] = 0;
for(ll i = 0; i < v.size(); i++){
auto [l , r] = v[i];
sum2.up(pos(t, l)+1, l);
c2.up(pos(t, l)+1, 1);
sum1.up(pos(t, r)+1 , r);
c1.up(pos(t, r)+1, 1);
while(cur + 1 <= t.size() && cost(cur, t[cur - 1]) > cost(cur + 1, t[cur])) cur++;
pref[i + 1] = cost(cur, t[cur - 1]);
}
c1.t();
c2.t();
sum1.t();
sum2.t();
ll sz = v.size();
cur = t.size();
suf[sz + 1] = 0;
for(ll i = sz - 1; i >= 0; i--){
auto [l , r] = v[i];
sum2.up(pos(t, l)+1, l);
c2.up(pos(t, l)+1, 1);
sum1.up(pos(t, r)+1 , r);
c1.up(pos(t, r)+1, 1);
while(cur - 1 >= 1 && cost(cur, t[cur - 1]) > cost(cur - 1, t[cur - 2])) cur--;
suf[i + 1] = cost(cur, t[cur - 1]);
}
ll mn = 1e18;
for(ll i = 0; i <= sz; i++){
mn = min(mn , pref[i] + suf[i + 1]);
}
if(k == 1){
cout << ans + 2 * suf[1] << '\n';
return;
}
cout << ans + 2 * mn << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
auto start_time = clock();
#else
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//pre();
ll T = 1, CT = 0; // cin >> T;
while(T--){
// cout << "Case #" << ++CT <<": " ;
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:159:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for(ll i = 1; i < v.size(); i++) pref[i] = 1e18;
| ~~^~~~~~~~~~
bridge.cpp:164:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | for(ll i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
bridge.cpp:172:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
172 | while(cur + 1 <= t.size() && cost(cur, t[cur - 1]) > cost(cur + 1, t[cur])) cur++;
| ~~~~~~~~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:228:15: warning: unused variable 'CT' [-Wunused-variable]
228 | ll T = 1, CT = 0; // cin >> T;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
6564 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
5 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
6 ms |
6612 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6612 KB |
Output is correct |
11 |
Correct |
5 ms |
6692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
5 ms |
6652 KB |
Output is correct |
5 |
Correct |
5 ms |
6580 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
5 ms |
6612 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6612 KB |
Output is correct |
12 |
Correct |
74 ms |
14268 KB |
Output is correct |
13 |
Correct |
251 ms |
14148 KB |
Output is correct |
14 |
Correct |
143 ms |
13524 KB |
Output is correct |
15 |
Correct |
130 ms |
10572 KB |
Output is correct |
16 |
Correct |
74 ms |
14204 KB |
Output is correct |
17 |
Correct |
149 ms |
14112 KB |
Output is correct |
18 |
Correct |
133 ms |
14104 KB |
Output is correct |
19 |
Correct |
163 ms |
14220 KB |
Output is correct |
20 |
Correct |
86 ms |
14168 KB |
Output is correct |
21 |
Correct |
192 ms |
14192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6600 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
6576 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
8 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6612 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
5 ms |
6608 KB |
Output is correct |
11 |
Correct |
5 ms |
6600 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
5 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
5 ms |
6612 KB |
Output is correct |
16 |
Correct |
3 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6672 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
5 ms |
6608 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6600 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6608 KB |
Output is correct |
10 |
Correct |
5 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
4 ms |
6608 KB |
Output is correct |
14 |
Correct |
5 ms |
6612 KB |
Output is correct |
15 |
Correct |
6 ms |
6632 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6664 KB |
Output is correct |
20 |
Correct |
5 ms |
6664 KB |
Output is correct |
21 |
Correct |
6 ms |
6616 KB |
Output is correct |
22 |
Correct |
6 ms |
6632 KB |
Output is correct |
23 |
Correct |
4 ms |
6584 KB |
Output is correct |
24 |
Correct |
5 ms |
6628 KB |
Output is correct |
25 |
Correct |
84 ms |
14916 KB |
Output is correct |
26 |
Correct |
111 ms |
15176 KB |
Output is correct |
27 |
Correct |
255 ms |
15944 KB |
Output is correct |
28 |
Correct |
334 ms |
16512 KB |
Output is correct |
29 |
Correct |
297 ms |
16524 KB |
Output is correct |
30 |
Correct |
136 ms |
11532 KB |
Output is correct |
31 |
Correct |
80 ms |
15792 KB |
Output is correct |
32 |
Correct |
159 ms |
16604 KB |
Output is correct |
33 |
Correct |
142 ms |
16116 KB |
Output is correct |
34 |
Correct |
183 ms |
16476 KB |
Output is correct |
35 |
Correct |
89 ms |
16016 KB |
Output is correct |
36 |
Correct |
171 ms |
16292 KB |
Output is correct |
37 |
Correct |
67 ms |
14984 KB |
Output is correct |