QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#692975 | #5439. Meet in the Middle | ningago | TL | 0ms | 0kb | C++14 | 8.1kb | 2024-10-31 15:18:58 | 2024-10-31 15:19:09 |
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <cctype>
#include <set>
namespace uvu
{
#define LOCAL ____________DONT_DEFINE_ME____________
#define ll long long
// #define inf 0x3f3f3f3f
// #define int long long
// #define inf 0x3f3f3f3f3f3f3f3fll
#define infll 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = (1 << 20);
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
return getchar();
#else
if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return iS == iT ? EOF : *iS++;
#endif
#define getchar ERR
}
void Flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; }
struct Flusher { ~Flusher(){ Flush(); } }iamflusher;
void putc(char c)
{
#ifdef LOCAL
putchar(c);
#else
*oS++ = c;
if(oS == oT) Flush();
#endif
#define putchar ERR
}
template <typename T = int> T read()
{
T x = 0, f = 1; char c = getc();
for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
for(; isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
}
template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
top = 0;
if(x < 0) putc('-'), x = -x;
if(!x) sta[top = 1] = 0;
for(; x; x /= 10) sta[++top] = x % 10;
for(; top; ) putc(sta[top--] ^ 48);
if(c) putc(c);
}
int readstr(char *s, int base)
{
int idx = base - 1; char c = getc();
for(; !(isdigit(c) || isalpha(c) || c == '#' || c == '.'); c = getc());
for(; isdigit(c) || isalpha(c) || c == '#' || c == '.' ; c = getc()) s[++idx] = c;
return idx - base + 1;
}
void printf(const char *s) { for(; *s; s++) putc(*s); }
template <typename T, typename ... Args>
void printf(const char *s, T x, Args ... rest)
{
for(; *s; s++)
{
if(*s != '%') { putc(*s); continue; }
s++; if(*s == 'd') print(x, 0);
else if(*s == 'c') putc(x);
printf(s + 1, rest ...);
return;
}
}
template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
#define mod 998244353
// #define mod 1000000007
int sm(int x) { return x >= mod ? x - mod : x; }
void plus_(int &x, int y) { x = sm(x + y); }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }
#define N 100010
#define M 500010
int n, m, x_[M], y_[M];
int xx[N], yy[N];
namespace T1 { ll dis[N]; int dfn[N], fdfn[N], out[N]; }
namespace T2
{
int h[N], e[N << 1], ne[N << 1], v[N << 1], idx = -1;
void add_edge(int x, int y, int z) { ne[++idx] = h[x], h[x] = idx, e[idx] = y, v[idx] = z; }
void add(int x, int y, int z) { add_edge(x, y, z), add_edge(y, x, z); }
int sz[N], hson[N], top[N], fa[N], dep[N];
ll dis[N];
void dfs0(int k, int fa)
{
T2::fa[k] = fa; sz[k] = 1; hson[k] = 0; dep[k] = dep[fa] + 1;
for(int i = h[k]; ~i; i = ne[i]) if(e[i] != fa) dis[e[i]] = dis[k] + v[i], dfs0(e[i], k), sz[k] += sz[e[i]];
}
void dfs1(int k, int tp)
{
top[k] = tp; if(hson[k]) dfs1(hson[k], tp);
for(int i = h[k]; ~i; i = ne[i]) if(e[i] != fa[k] && e[i] != hson[k]) dfs1(e[i], e[i]);
}
int LCA(int x, int y)
{
for(; top[x] != top[y]; x = fa[top[x]]) if(dep[top[x]] < dep[top[y]]) x ^= y ^= x ^= y;
return dep[x] < dep[y] ? x : y;
}
ll Dis(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
void init()
{
for(int i = 1; i <= n; i++) h[i] = idx = -1;
for(int i = 1, x, y, z; i < n; i++) x = read(), y = read(), z = read(), add(x, y, z);
dfs0(1, 0); dfs1(1, 1);
}
struct Tree
{
int x, y; ll vx, vy, lazy;
void push(ll z) { vx += z, vy += z, lazy += z; }
}tr[N << 2];
#define root 1, 1, n
#define lson k << 1
#define rson k << 1 | 1
#define ls lson, l, mid
#define rs rson, mid + 1, r
bool ckmax(ll &x, ll y) { if(x <= y) { x = y; return 1; } return 0; }
void pushup(int k)
{
ll mx = -1;
if(tr[lson].x != tr[lson].y && ckmax(mx, tr[lson].vx + tr[lson].vy + Dis(tr[lson].x, tr[lson].y))) tr[k].x = tr[lson].x, tr[k].y = tr[lson].y, tr[k].vx = tr[lson].vx, tr[k].vy = tr[lson].vy;
if(tr[rson].x != tr[rson].y && ckmax(mx, tr[rson].vx + tr[rson].vy + Dis(tr[rson].x, tr[rson].y))) tr[k].x = tr[rson].x, tr[k].y = tr[rson].y, tr[k].vx = tr[rson].vx, tr[k].vy = tr[rson].vy;
if(ckmax(mx, tr[lson].vx + tr[rson].vx + Dis(tr[lson].x, tr[rson].x))) tr[k].x = tr[lson].x, tr[k].y = tr[rson].x, tr[k].vx = tr[lson].vx, tr[k].vy = tr[rson].vx;
if(ckmax(mx, tr[lson].vx + tr[rson].vy + Dis(tr[lson].x, tr[rson].y))) tr[k].x = tr[lson].x, tr[k].y = tr[rson].y, tr[k].vx = tr[lson].vx, tr[k].vy = tr[rson].vy;
if(ckmax(mx, tr[lson].vy + tr[rson].vx + Dis(tr[lson].y, tr[rson].x))) tr[k].x = tr[lson].y, tr[k].y = tr[rson].x, tr[k].vx = tr[lson].vy, tr[k].vy = tr[rson].vx;
if(ckmax(mx, tr[lson].vy + tr[rson].vy + Dis(tr[lson].y, tr[rson].y))) tr[k].x = tr[lson].y, tr[k].y = tr[rson].y, tr[k].vx = tr[lson].vy, tr[k].vy = tr[rson].vy;
}
void pushdown(int k)
{
if(tr[k].lazy) tr[lson].push(tr[k].lazy), tr[rson].push(tr[k].lazy), tr[k].lazy = 0;
}
void build(int k, int l, int r)
{
tr[k].lazy = 0;
if(l == r)
{
int p = T1::fdfn[l];
tr[k].x = tr[k].y = p;
tr[k].vx = tr[k].vy = T1::dis[p];
return;
}
int mid = (l + r) >> 1; build(ls); build(rs); pushup(k);
}
void change(int k, int l, int r, int ql, int qr, ll z)
{
if(ql > qr) return;
if(ql <= l && r <= qr) return tr[k].push(z);
int mid = (l + r) >> 1; pushdown(k);
if(ql <= mid) change(ls, ql, qr, z);
if(mid < qr) change(rs, ql, qr, z);
pushup(k);
}
}
namespace T1
{
int h[N], e[N << 1], ne[N << 1], v[N << 1], idx = -1;
void add_edge(int x, int y, int z) { ne[++idx] = h[x], h[x] = idx, e[idx] = y, v[idx] = z; }
void add(int x, int y, int z) { add_edge(x, y, z), add_edge(y, x, z); }
int sz[N], hson[N], top[N], fa[N], dep[N];
void dfs0(int k, int fa)
{
T1::fa[k] = fa; sz[k] = 1; hson[k] = 0; dep[k] = dep[fa] + 1;
for(int i = h[k]; ~i; i = ne[i]) if(e[i] != fa) dis[e[i]] = dis[k] + v[i], dfs0(e[i], k), sz[k] += sz[e[i]];
}
void dfs1(int k, int tp)
{
fdfn[dfn[k] = ++dfn[0]] = k;
top[k] = tp; if(hson[k]) dfs1(hson[k], tp);
for(int i = h[k]; ~i; i = ne[i]) if(e[i] != fa[k] && e[i] != hson[k]) dfs1(e[i], e[i]);
out[k] = dfn[0];
}
int LCA(int x, int y)
{
for(; top[x] != top[y]; x = fa[top[x]]) if(dep[top[x]] < dep[top[y]]) x ^= y ^= x ^= y;
return dep[x] < dep[y] ? x : y;
}
ll Dis(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
void init()
{
dfn[0] = 0;
for(int i = 1; i <= n; i++) h[i] = idx = -1;
for(int i = 1, x, y, z; i < n; i++) x = read(), y = read(), z = read(), add(x, y, z);
dfs0(1, 0); dfs1(1, 1);
}
void dfs2(int k, int fa)
{
xx[k] = T2::tr[1].x, yy[k] = T2::tr[1].y;
for(int i = h[k]; ~i; i = ne[i]) if(e[i] != fa)
{
T2::change(root, dfn[e[i]], out[e[i]], -v[i]);
T2::change(root, 1, dfn[e[i]] - 1, v[i]);
T2::change(root, out[e[i]] + 1, n, v[i]);
dfs2(e[i], k);
T2::change(root, dfn[e[i]], out[e[i]], v[i]);
T2::change(root, 1, dfn[e[i]] - 1, -v[i]);
T2::change(root, out[e[i]] + 1, n, -v[i]);
}
}
void solve()
{
dfs2(1, 0); dfs0(1, 0);
}
}
void solve()
{
// memset(h, idx = -1, sizeof(h));
n = read();
T1::init(); T2::init();
m = read(); for(int i = 1; i <= m; i++) x_[i] = read(), y_[i] = read();
// static int Case;
// Case++; if(Case != 3) { for(int i = 1; i <= m; i++) print(-1, '\n'); return; };
T2::build(root); T1::solve();
for(int i = 1; i <= m; i++) print(std::max(T2::Dis(xx[x_[i]], y_[i]) + T1::Dis(xx[x_[i]], x_[i]), T2::Dis(yy[x_[i]], y_[i]) + T1::Dis(yy[x_[i]], x_[i])), '\n');
}
void init()
{
}
char _ED_;
void mian()
{
debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
init();
for(int T = 1; T; solve(), T--);
}
#ifdef int
#undef int
#endif
}
int main()
{
uvu::mian(); return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2