# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
936497 | gaga999 | Treasure Hunt (CEOI11_tre) | C++17 | 0 ms | 0 KiB |
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 <cstdio>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <sstream>
#include <assert.h>
#include <climits>
#include <sstream>
#include <numeric>
#include <time.h>
#include <limits.h>
#include <list>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <iomanip>
#include <complex>
#include <chrono>
#include <fstream>
#include <functional>
#include <unistd.h>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define initi(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define pb emplace_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define NIF 0xc0c0c0c0
#define eps 1e-9
#define F first
#define S second
#define int long long
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;
// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);
void db() { cerr << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cerr << a << " ", db(b...); }
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
void rd() {}
template <typename T, typename... U>
void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (!isdigit(c))
f ^= !(c ^ 45), c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
void prt(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
prt(x / 10);
putchar((x % 10) ^ 48);
}
const int N = 4e5 + 7;
int pt[N], dep[N], dst[N], n = 1, m = 1; // head to head
pii ac[20][N]; // po num
inline int gid(int x)
{
return lower_bound(pt, pt + n + 1, x) - pt;
}
inline int gdst(const pii &p1, const pii &p2) // up down
{
return dst[p2.F] - dst[p1.F] + (p2.S - pt[p2.F - 1]) - (p1.S - pt[p1.F - 1]);
// -1 +1
}
inline pii lca(pii p1, pii p2)
{
if (dep[p1.F] < dep[p2.F])
swap(p1, p2);
for (int i = 19; i >= 0; i--)
if (ac[i][p1.F].F && dep[ac[i][p1.F].F] >= dep[p2.F])
p1 = ac[i][p1.F];
if (p1.F != p2.F)
{
for (int i = 19; i >= 0; i--)
if (ac[i][p1.F].F && ac[i][p2.F].F && ac[i][p1.F].F != ac[i][p2.F].F)
p1 = ac[i][p1.F], p2 = ac[i][p2.F];
p1 = ac[0][p1.F], p2 = ac[0][p2.F];
}
return pii(p1.F, min(p1.S, p2.S));
}
inline int qy(pii p, int d)
{
if (p.S - pt[p.F - 1] > d)
return p.S - d;
pii cr = p;
for (int i = 19; i >= 0; i--)
if (ac[i][cr.F].F && gdst(ac[i][cr.F], p) < d)
cr = ac[i][cr.F];
cr = ac[0][cr.F];
d = gdst(mpr(cr.F, pt[cr.F - 1] + 1), p) - d;
return pt[cr.F - 1] + d + 1;
}
void init() {}
void path(int x, int y)
{
pt[++n] = m += y;
pii pr = mpr(gid(x), x);
dep[n] = dep[pr.F] + 1;
dst[n] = dst[pr.F] + (pr.S - pt[pr.F - 1]);
ac[0][n] = pr;
for (int i = 1; i < 20; i++)
ac[i][n] = ac[i - 1][ac[i - 1][n].F];
}
int32_t dig(int x, int y)
{
pii p1 = mpr(gid(x), x), p2 = mpr(gid(y), y);
pii p = lca(p1, p2);
int d1 = gdst(p, p1), d2 = gdst(p, p2);
// db(p2.F, p2.S, p1.F, p1.S);
if (d1 > d2)
return qy(p1, (d1 + d2) >> 1);
else
return qy(p2, (d1 + d2 + 1) >> 1);
}
// signed main()
// {
// pt[1] = 1;
// int q, x, y;
// char c[10];
// scanf("%d", &q);
// while (q-- && scanf("%s%d%d", c, &x, &y))
// {
// if (c[0] == 'P')
// path(x, y);
// else
// printf("%d\n", dig(x, y)), fflush(stdout);
// }
// }