QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#87075 | #5439. Meet in the Middle | Booksnow | WA | 341ms | 15132kb | C++14 | 4.0kb | 2023-03-11 16:50:39 | 2023-03-11 16:50:43 |
Judging History
answer
/*
....
.##.
.##.
H.AKLESS
# #### #
# #### #
# #### #
# #### #
## ##
## ##
## ##
*/
#include <bits/stdc++.h>
#define st first
#define nd second
#define db double
#define re register
#define pb push_back
#define mk make_pair
#define int long long
#define ldb long double
#define pii pair<int, int>
#define ull unsigned long long
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 1e5 + 10, M = 5e5 + 10;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w *= -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
struct edge{ int v, w; };
struct Que{ int x, id; };
int n, Q, tim, ans[M];
int id[N], out[N], dfn[N], init[N];
int up[N], dep[N], dis[N], siz[N], son[N], top[N];
vector<Que> q[N];
vector<edge> G[2][N];
inline void Pre(int u, int fa)
{
up[u] = fa, siz[u] = 1, dep[u] = dep[fa] + 1;
for(re edge to : G[1][u]){
if(to.v == fa) continue;
dis[to.v] = dis[u] + to.w;
Pre(to.v, u), siz[u] += siz[to.v];
if(siz[to.v] > siz[son[u]]) son[u] = to.v;
}
}
inline void DFS(int u, int fa, int tp)
{
top[u] = tp;
if(son[u]) DFS(son[u], u, tp);
for(re edge to : G[1][u])
if(to.v != fa && to.v != son[u]) DFS(to.v, u, to.v);
}
inline int LCA(int x, int y)
{
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = up[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
inline int dist(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
inline void Run(int u, int fa)
{
dfn[u] = ++tim, id[tim] = u;
for(re edge to : G[0][u])
if(to.v != fa) init[to.v] = init[u] + to.w, Run(to.v, u);
out[u] = tim;
}
#define ls k << 1
#define rs k << 1 | 1
#define mid ((l + r) >> 1)
struct SegmentTree{
int tr[N << 2][2], tag[N << 2]; //修改标记
inline int ask(int k, int l, int r, int x){
if(l == r && l == x) return tag[k];
if(x <= mid) return tag[k] + ask(ls, l, mid, x);
else return tag[k] + ask(rs, mid + 1, r, x);
}
inline void pushup(int k){
int mx = 0, pos[4] = {tr[ls][0], tr[ls][1], tr[rs][0], tr[rs][1]};
for(re int i = 0; i < 4; i++){
for(re int j = i + 1; j < 4; j++){
int x = pos[i], y = pos[j], v = dist(x, y) + ask(1, 1, n, x) + ask(1, 1, n, y);
if(v > mx) mx = v, tr[k][0] = x, tr[k][1] = y;
}
}
}
inline void build(int k, int l, int r){
if(l == r) { tag[k] = init[id[l]], tr[k][0] = tr[k][1] = id[l]; return; }
build(ls, l, mid), build(rs, mid + 1, r);
pushup(k);
}
inline void update(int k, int l, int r, int x, int y, int v){
if(x > y) return;
if(l >= x && r <= y) { tag[k] += v; return; }
if(y <= mid) update(ls, l, mid, x, y, v);
else if(x > mid) update(rs, mid + 1, r, x, y, v);
else update(ls, l, mid, x, mid, v), update(rs, mid + 1, r, mid + 1, y, v);
pushup(k);
}
}T;
inline void Sol(int u, int fa)
{
int x = T.tr[1][0], y = T.tr[1][1]; //直径的两个端点
for(re Que it : q[u]) ans[it.id] = max(T.ask(1, 1, n, x) + dist(it.x, x), T.ask(1, 1, n, y) + dist(it.x, y));
for(re edge to : G[0][u]){
if(to.v == fa) continue;
T.update(1, 1, n, dfn[to.v], out[to.v], -to.w);
T.update(1, 1, n, 1, dfn[to.v] - 1, to.w), T.update(1, 1, n, out[to.v] + 1, n, to.w);
Sol(to.v, u);
T.update(1, 1, n, dfn[to.v], out[to.v], to.w);
T.update(1, 1, n, 1, dfn[to.v] - 1, -to.w), T.update(1, 1, n, out[to.v] + 1, n, -to.w);
}
}
signed main()
{
n = read(), Q = read();
for(re int i = 1, x, y, z; i < n; i++)
x = read(), y = read(), z = read(), G[0][x].pb((edge){y, z}), G[0][y].pb((edge){x, z});
for(re int i = 1, x, y, z; i < n; i++)
x = read(), y = read(), z = read(), G[1][x].pb((edge){y, z}), G[1][y].pb((edge){x, z});
for(re int i = 1, x, y; i <= Q; i++) x = read(), y = read(), q[x].pb((Que){y, i});
Pre(1, 0), DFS(1, 0, 1), Run(1, 0);
T.build(1, 1, n);
Sol(1, 0);
for(re int i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
return 0;
}
/*
1
5
1 5
2 5
2 7
3 3
4 10
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 10596kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 10552kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 3ms
memory: 10584kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 341ms
memory: 15132kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
650456004218 631165818938 518899967466 635444828098 477172405358 648460535334 647037354288 578230530718 648707601232 806612613619 679870045016 739390072678 724701827332 751942468536 558999380190 532243319789 590212731405 695049742072 554660797852 731437463922 632973256011 671388017724 704193013346 6...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '650456004218'