This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <bitset>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <functional>
#include <numeric>
#include <chrono>
#include <random>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using pb = pair<bool, bool>;
using vpb = vector<pb>;
using vvpb = vector<vpb>;
using vi = vector<int>;
using vvi = vector<vi>;
using pi = pair<int, int>;
using vpi = vector<pi>;
const ll inf = (ll)2e18;
const ll mod = (ll)(1e9 + 7);
#define FAST ios_base::sync_with_stdio(0)
#define FASTIN cin.tie(0)
#define FASTOUT cout.tie(0)
#define upmin(a, b) (a) = min((a), (b))
#define upmax(a, b) (a) = max((a), (b))
#define pr(x) cout << x << endl
#define prv(v) for(auto it : v) cout << it << " "; cout << endl;
#define prvv(v) for(auto it : v) { for(auto it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define spr(x) cout << x << " "
//#define DBG_FLAG (1) // Toggle to switch between bebug mode and solution mode
#ifdef DBG_FLAG
#define wpr(x) cout << #x << " = " << (x) << endl;
#define wprv(v) cout << #v << ": " << endl; for(auto& it : v) cout << it << " "; cout << endl;
#define wprvv(v) cout << #v << ": " << endl; for(auto& it : v) { for(auto& it2 : it) cout << it2 << " "; cout << endl; } cout << endl;
#define wspr(x) cout << #x << ": " << x << " "
#endif
#ifndef DBG_FLAG
#define wpr(x)
#define wprv(v)
#define wprvv(v)
#define wspr(x)
#endif
#define x first
#define y second
#define rep(i, s, e) for(ll i = s; i < e; i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
/*
Solution:
Let's start with k == 1:
Make a presistent segment tree.
Now, for each one we try, we have to take all values that end before it,
and all values that start after it.
*/
ostream& operator<<(ostream & os, const pll & p)
{
os << "{" << p.x << ", " << p.y << "}";
return os;
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U>& l, const pair<T, U>& r) {
return { l.first + r.first, l.second + r.second };
}
struct tr {
int l, r, m;
int cnt;
ll v;
tr* lp = nullptr, * rp = nullptr;
void push()
{
if (l == r) return;
if (!lp) {
lp = new tr(l, m);
}
if (!rp) {
rp = new tr(m + 1, r);
}
}
void pull()
{
if (l == r) return;
cnt = lp->cnt + rp->cnt;
v = lp->v + rp->v;
}
tr() {}
tr(tr* other) : l(other->l), r(other->r), m(other->m), cnt(other->cnt), v(other->v), lp(other->lp), rp(other->rp) {}
//tr(ll l, ll r, ll v) : l(l), r(r), m((l+r)>>1), v(v) {}
tr(ll l, ll r) : l(l), r(r), m((l + r) >> 1), cnt(0), v(0)
{
/*
if (l == r) return;
lp = new tr(l, m);
rp = new tr(m + 1, r);
*/
}
tr* add(ll i)
{
push();
tr* t = new tr(this);
if (l == r) {
t->cnt++;
t->v += i;
return t;
//return new tr(l, r, this->v + add);
}
if (i <= m) {
t->lp = lp->add(i);
}
else {
t->rp = rp->add(i);
}
t->pull();
return t;
}
pll q(ll f, ll t) {
if (r < f || l > t) return { 0, 0 };
if (f <= l && r <= t) return { cnt, v };
push();
return lp->q(f, t) + rp->q(f, t);
}
};
const ll maxN = 1e5 + 5;
const ll maxV = 1e9 + 5;
ll k, n;
vpll segs;
vll check;
ll ans = 0;
tr* segR[maxN], *segL[maxN];
void construct_pers_segs()
{
wprv(segs);
segR[0] = new tr(0, maxV);
rep(i, 0, n) {
wpr(i);
segR[i + 1] = segR[i]->add(segs[i].y);
}
segL[0] = new tr(0, maxV);
rep(i, 0, n) {
segL[i + 1] = segL[i]->add(segs[i].x);
}
}
void solve_k1()
{
construct_pers_segs();
ll minAdd = inf;
for(const auto& i : check) {
wpr(i);
ll cost = 0;
pll l = segR[n]->q(0, i - 1);
cost += (l.x * i - l.y) * 2;
pll r = segL[n]->q(i + 1, maxV);
wpr(l); wpr(r);
cost += (r.y - r.x * i) * 2;
wpr(cost);
upmin(minAdd, cost);
}
ans += minAdd;
}
void solve_k2()
{
}
void solve()
{
cin >> k >> n;
check.push_back(0);
check.push_back(maxV);
rep(i, 0, n) {
char side1, side2;
ll v1, v2;
cin >> side1 >> v1 >> side2 >> v2;
ans += max(v1, v2) - min(v1, v2);
if (side1 == side2) continue;
ans++;
segs.push_back({ min(v1, v2), max(v1, v2) });
check.push_back(v1);
check.push_back(v2);
}
n = segs.size();
sort(segs.begin(), segs.end(), [&](const pll& p1, const pll& p2) {
return p1.x + p1.y < p2.x + p2.y;
});
if (k == 1) solve_k1();
else solve_k2();
pr(ans);
}
int main()
{
FAST;
FASTIN; FASTOUT;
solve();
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# | 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... |