이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using orderedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T>
void readi(T &x)
{
T input = 0;
bool negative = false;
char c = ' ';
while (c < '-')
{
c = getchar();
}
if (c == '-')
{
negative = true;
c = getchar();
}
while (c >= '0')
{
input = input * 10 + (c - '0');
c = getchar();
}
if (negative)
{
input = -input;
}
x = input;
}
template<class T>
void printi(T output)
{
if (output == 0)
{
putchar('0');
return;
}
if (output < 0)
{
putchar('-');
output = -output;
}
int aout[20];
int ilen = 0;
while(output)
{
aout[ilen] = ((output % 10));
output /= 10;
ilen++;
}
for (int i = ilen - 1; i >= 0; i--)
{
putchar(aout[i] + '0');
}
return;
}
template<class T>
void ckmin(T &a, T b)
{
a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
a = max(a, b);
}
template<class T, class U>
T normalize(T x, U mod = 1000000007)
{
return (((x % mod) + mod) % mod);
}
long long randomizell(long long mod)
{
return ((1ll << 45) * rand() + (1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}
int randomize(int mod)
{
return ((1ll << 15) * rand() + rand()) % mod;
}
#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;
#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
int N, K, M;
ll ans = 0, best = LLINF;
vector<pll> ranges;
vector<ll> pos;
vector<pii> upds[MAXN];
ll pref[MAXN];
struct pseg
{
struct node
{
node *ch[2];
ll stor;
};
int T;
node *root[MAXN];
int ft[MAXN];
node* build(int L, int R)
{
node *t = new node();
t -> stor = 0;
if (L == R)
{
return t;
}
int mid = (L + R)/2;
t -> ch[0] = build(L, mid);
t -> ch[1] = build(mid + 1, R);
return t;
}
node* update(node* w, int L, int R, int a, int val)
{
if (a < L || R < a)
{
return w;
}
node *t = new node();
if (L == R)
{
t -> stor = w -> stor + val;
return t;
}
int mid = (L + R)/2;
t -> ch[0] = update(w -> ch[0], L, mid, a, val);
t -> ch[1] = update(w -> ch[1], mid + 1, R, a, val);
t -> stor = t -> ch[0] -> stor + t -> ch[1] -> stor;
return t;
}
ll query(node *w, int L, int R, int a, int b)
{
if (b < L || R < a)
{
return 0;
}
if (a <= L && R <= b)
{
return w -> stor;
}
int mid = (L + R)/2;
return query(w -> ch[0], L, mid, a, b) + query(w -> ch[1], mid + 1, R, a, b);
}
};
pseg seg[2];
bool cmp(pll a, pll b)
{
return a.fi + a.se < b.fi + b.se;
}
ll get(int a, int lt, int rt)
{
ll res = seg[0].query(seg[0].root[seg[0].ft[a + 1]], 0, N, lt, rt);
return res;
}
ll cget(int a, int lt, int rt)
{
ll res = seg[1].query(seg[1].root[seg[1].ft[a + 1]], 0, N, lt, rt);
// cerr << "at time "<< a + 1 << " query " << lt << ' ' << rt << " gives " << res << endl;
return res;
}
ll f(ll a, ll b)
{
ll res = 0;
ll x = pos[a], y = pos[b];
int lo = 0, hi = N;
while(hi > lo)
{
int mid = (hi + lo) / 2;
if (ranges[mid].fi + ranges[mid].se >= x + y)
{
hi = mid;
}
else
{
lo = mid + 1;
}
}
//at time x, query(0, lo)
//at time y, query(y, hi)
ll xl = get(a, 0, lo - 1), xg = pref[lo] - xl;
ll yl = get(b, lo, N), yg = pref[N] - pref[lo] - yl;
ll xlc = cget(a, 0, lo - 1), xgc = 2 * lo - xlc;
ll ylc = cget(b, lo, N), ygc = 2 * N - 2 * lo - ylc;
res = (xlc * x - xl) + (xg - xgc * x) + (ylc * y - yl) + (yg - ygc * y);
// cerr << "# of guys with pos < " << lo << " value <= " << x << " is " << xlc << endl;
// for (int i = 0; i < lo; i++)
// {
// res += abs(x - ranges[i].fi) + abs(x - ranges[i].se);
// }
// for (int i = lo; i < N; i++)
// {
// res += abs(y - ranges[i].fi) + abs(y - ranges[i].se);
// }
return res;
}
void solve(int L, int R, int optl, int optr)
{
if (L > R)
{
return;
}
ll cur = LLINF;
int mid = (L + R) / 2, opt = -1;
for (int i = max(mid + 1, optl); i <= optr; i++)
{
if (f(mid, i) <= cur)
{
opt = i;
cur = f(mid, i);
}
}
ckmin(best, cur);
solve(L, mid - 1, optl, opt);
solve(mid + 1, R, opt, optr);
}
int32_t main()
{
// ios_base::sync_with_stdio(0);
srand(time(0));
// cout << fixed << setprecision(10);
// cerr << fixed << setprecision(10);
if (fopen("input.in", "r"))
{
freopen ("input.in", "r", stdin);
freopen ("output.out", "w", stdout);
}
readi(K); readi(N);
for (int i = 0; i < N; i++)
{
char c1; int x; char c2; int y;
c1 = getchar(); readi(x); c2 = getchar(); readi(y);
if (c1 == 'B')
{
swap(c1, c2);
swap(x, y);
}
if (c1 == c2)
{
ans += abs(y - x);
}
else
{
ranges.PB({x, y});
}
}
N = ranges.size();
if (N == 0)
{
printi(ans); putchar('\n');
return 0;
}
ans += N;
pos.reserve(2 * N);
for (int i = 0; i < N; i++)
{
pos.PB(ranges[i].fi);
pos.PB(ranges[i].se);
}
sort(pos.begin(), pos.end());
if (K == 1 || N == 1)
{
for (pll x : ranges)
{
ans += abs(x.fi - pos[pos.size() / 2]);
ans += abs(x.se - pos[pos.size() / 2]);
}
printi(ans); putchar('\n');
return 0;
}
pos.erase(unique(pos.begin(), pos.end()), pos.end());
M = pos.size();
sort(ranges.begin(), ranges.end(), cmp);
for (int i = 0; i < N; i++)
{
pref[i + 1] = pref[i] + ranges[i].fi + ranges[i].se;
}
for (int i = 0; i < N; i++)
{
int x1 = LB(pos.begin(), pos.end(), ranges[i].fi) - pos.begin();
int x2 = LB(pos.begin(), pos.end(), ranges[i].se) - pos.begin();
upds[x1].PB({i, 0});
upds[x2].PB({i, 1});
// cerr << "guy " << i << " is " << x1 << "," << x2 << endl;
}
seg[0].root[0] = seg[0].build(0, N);
seg[1].root[0] = seg[1].build(0, N);
for (int i = 0; i <= M; i++)
{
seg[0].ft[i] = seg[0].T;
seg[1].ft[i] = seg[1].T;
for (pii p : upds[i])
{
ll val = (p.se ? ranges[p.fi].se : ranges[p.fi].fi);
// cerr << "at time " << i << " update " << p.fi << "," << p.se << endl;
seg[0].T++;
seg[1].T++;
seg[0].root[seg[0].T] = seg[0].update(seg[0].root[seg[0].T - 1], 0, N, p.fi, val);
seg[1].root[seg[1].T] = seg[1].update(seg[1].root[seg[1].T - 1], 0, N, p.fi, 1);
}
}
if (N > 1000) return 0;
solve(0, pos.size() - 2, 1, pos.size() - 1);
ans += best;
printi(ans); putchar('\n');
// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'int32_t main()':
bridge.cpp:263:25: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen ("input.in", "r", stdin);
~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:264:25: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen ("output.out", "w", stdout);
~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |