이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 };
}
vll vals;
struct tr {
//int l, r, m;
int cnt;
ll v;
tr* lp = nullptr, * rp = nullptr;
void push(ll l, ll r)
{
if (l == r) return;
ll m = (l + r) / 2;
if (!lp) {
lp = new tr(l, m);
}
if (!rp) {
rp = new tr(m + 1, r);
}
}
void pull(ll l, ll r)
{
if (l == r) return;
cnt = (lp?lp->cnt:0) + (rp?rp->cnt:0);
v = (lp?lp->v:0) + (rp?rp->v:0);
}
tr() {}
tr(tr* other) : 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) : cnt(0), v(0)
{
/*
if (l == r) return;
lp = new tr(l, m);
rp = new tr(m + 1, r);
*/
}
void update(ll i, ll l, ll r) {
if (l == r) {
cnt++;
v += i;
return;
}
//push(l, r);
ll m = (l + r) / 2;
if (i <= m) {
if (!lp) lp = new tr(l, m);
lp = lp->add(i, l, m);
}
else {
if (!rp) rp = new tr(m + 1, r);
rp = rp->add(i, m + 1, r);
}
pull(l, r);
}
tr* add(ll i, ll l, ll r)
{
//push(l, r);
tr* t = new tr(this);
/*
if (!lp&&!rp) {
t->update(i, l, r);
return t;
}*/
//if (!lp) return t;
if (l == r) {
t->cnt++;
t->v += vals[i];
return t;
//return new tr(l, r, this->v + add);
}
ll m = (l + r) / 2;
if (i <= m) {
if (!lp) lp = new tr(l, m);
t->lp = lp->add(i, l, m);
}
else {
if (!rp) rp = new tr(m + 1, r);
t->rp = rp->add(i, m + 1, r);
}
t->pull(l, r);
return t;
}
pll q(ll f, ll t, ll l, ll r) {
if (r < f || l > t) return { 0, 0 };
if (f <= l && r <= t) return { cnt, v };
push(l, r);
ll m = (l + r) / 2;
return lp->q(f, t, l, m) + rp->q(f, t, m + 1, r);
}
};
const ll maxN = 1e5 + 5;
const ll maxV = 1e9 + 5;
ll k, n;
vpll segs;
ll sz;
ll ans = 0;
tr* segR[maxN], * segL[maxN];
void construct_pers_segs()
{
sz = vals.size();
wprv(segs);
segR[0] = new tr(0, sz);
rep(i, 0, n) {
wpr(i);
ll ind = lower_bound(vals.begin(), vals.end(), segs[i].y) - vals.begin();
segR[i + 1] = segR[i]->add(ind, 0, sz);
}
if (n >= 1e4) return;
segL[0] = new tr(0, sz);
rep(i, 0, n) {
ll ind = lower_bound(vals.begin(), vals.end(), segs[i].x) - vals.begin();
segL[i + 1] = segL[i]->add(ind, 0, sz);
}
}
void solve_k1()
{
//if (n >= 1e4) return;
construct_pers_segs();
if (n >= 1e4) return;
ll minAdd = inf;
rep(i, 0, vals.size()) {
wpr(i);
ll cost = 0;
pll l = segR[n]->q(0, i - 1, 0, sz);
cost += (l.x * i - l.y) * 2;
pll r = segL[n]->q(i + 1, sz, 0, sz);
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;
vals.push_back(0);
vals.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) });
vals.push_back(v1);
vals.push_back(v2);
}
sort(vals.begin(), vals.end());
//vals.erase(unique((vals.begin(), vals.end()), vals.end()));
wprv(vals);
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
*/
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'void solve_k1()':
bridge.cpp:77:38: 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]
77 | #define rep(i, s, e) for(ll i = s; i < e; i++)
......
234 | rep(i, 0, vals.size()) {
| ~~~~~~~~~~~~~~~~~
bridge.cpp:234:2: note: in expansion of macro 'rep'
234 | rep(i, 0, vals.size()) {
| ^~~
# | 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... |