#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 ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define vii vector<vi>
#define ld long double
#define pii pair<int, int>
mt19937 mt(time(0));
namespace io
{
template <typename T, typename F>
istream &operator>>(istream &cin, pair<T, F> &pr)
{
cin >> pr.first >> pr.second;
return cin;
}
template <typename T, typename F>
ostream &operator<<(ostream &cout, pair<T, F> &pr)
{
cout << pr.first << ' ' << pr.second;
return cout;
}
template <typename T>
istream &operator>>(istream &cin, vector<T> &vec)
{
for (T &i : vec)
cin >> i;
return cin;
}
template <typename T>
ostream &operator<<(ostream &cout, vector<T> vec)
{
for (T i : vec)
cout << i << ' ';
return cout;
}
}
using namespace io;
int M = 1e9 + 7;
int pw(int x, int y)
{
x %= M;
int res = 1;
while (y)
{
if (y & 1)
res = res * x % M;
x = x * x % M, y /= 2;
}
return res;
}
int inv(int x) { return pw(x, M - 2); }
void solve()
{
int k, n, x, y, ans = 0;
char A, B;
cin >> k >> n;
vector<pii> a;
while (n--)
{
cin >> A >> x >> B >> y;
if (x > y)
swap(x, y);
if (A != B)
a.push_back({x, y}), ans++;
else
ans += y - x;
}
n = a.size();
if (n == 0)
return cout << ans, void();
sort(all(a));
int mn = 1e18, mx = 0;
for (auto &[x, y] : a)
mn = min(mn, y), mx = max(mx, x);
int m = (mx + mn) / 2;
for (auto &[x, y] : a)
{
if (x <= m && m <= y)
ans += y - x;
else
ans += abs(x - m) + abs(y - m);
}
cout << ans;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve(), cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |