#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;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 1e5 + 5;
struct Citizen{
char P; int S; char Q; int T;
Citizen(char P = '?', int S = 0, char Q = '?', int T = 0): P(P), S(S), Q(Q), T(T){
}
};
int k, n;
Citizen citizens[N];
ll ans;
pii a[N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> k >> n;
ForE(i, 1, n){
cin >> citizens[i].P >> citizens[i].S >> citizens[i].Q >> citizens[i].T;
}
{
int tn = 0;
ForE(i, 1, n){
if (citizens[i].P == citizens[i].Q){
ans += abs(citizens[i].S - citizens[i].T);
continue;
}
a[++tn] = make_pair(min(citizens[i].S, citizens[i].T), max(citizens[i].S, citizens[i].T));
ans += a[tn].se - a[tn].fi + 1;
}
n = tn;
}
if (k == 1){
vi cac;
ForE(i, 1, n){
cac.emplace_back(a[i].fi); cac.emplace_back(a[i].se);
}
sort(bend(cac));
int pos = cac[n];
ForE(i, 1, n){
ans += 2 * max(0, max(a[i].fi - pos, pos - a[i].se));
}
cout << ans << endl;
}
}
/*
is this slope trick?
==================================================+
INPUT: |
--------------------------------------------------|
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
2 ms |
1868 KB |
Output is correct |
3 |
Correct |
2 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
6 |
Correct |
3 ms |
1936 KB |
Output is correct |
7 |
Correct |
2 ms |
1868 KB |
Output is correct |
8 |
Correct |
3 ms |
1868 KB |
Output is correct |
9 |
Correct |
3 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
2 ms |
1868 KB |
Output is correct |
3 |
Correct |
2 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1868 KB |
Output is correct |
8 |
Correct |
2 ms |
1868 KB |
Output is correct |
9 |
Correct |
2 ms |
1912 KB |
Output is correct |
10 |
Correct |
3 ms |
1844 KB |
Output is correct |
11 |
Correct |
3 ms |
1876 KB |
Output is correct |
12 |
Correct |
29 ms |
4580 KB |
Output is correct |
13 |
Correct |
53 ms |
6080 KB |
Output is correct |
14 |
Correct |
37 ms |
4876 KB |
Output is correct |
15 |
Correct |
30 ms |
4288 KB |
Output is correct |
16 |
Correct |
37 ms |
5576 KB |
Output is correct |
17 |
Correct |
43 ms |
6092 KB |
Output is correct |
18 |
Correct |
44 ms |
5692 KB |
Output is correct |
19 |
Correct |
48 ms |
6124 KB |
Output is correct |
20 |
Correct |
36 ms |
5704 KB |
Output is correct |
21 |
Correct |
46 ms |
5832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |