QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#87445 | #5439. Meet in the Middle | Acestar | WA | 55ms | 35576kb | C++14 | 3.3kb | 2023-03-12 22:39:23 | 2023-03-12 22:39:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int, ll> P;
#define fi first
#define se second
const int N = 1e5 + 5;
const int M = 5e5 + 5;
int n, m;
vector <P> q[N];
int ans[M];
struct Tree
{
vector <P> G[N];
int fa[N], siz[N], son[N], dep[N];
ll dis[N];
int top[N], tim, dfn[N], id[N];
void dfs1(int u, int p)
{
fa[u] = p, siz[u] = 1, dep[u] = dep[p] + 1;
for(auto v : G[u])
{
if(v.fi == p) continue;
dis[v.fi] = dis[u] + v.se;
dfs1(v.fi, u); siz[u] += siz[v.fi];
if(siz[v.fi] > siz[son[u]]) son[u] = v.fi;
}
return;
}
void dfs2(int u, int tp)
{
top[u] = tp, dfn[u] = ++tim, id[tim] = u;
if(son[u]) dfs2(son[u], tp);
for(auto v : G[u])
if(v.fi != fa[u] && v.fi != son[u]) dfs2(v.fi, v.fi);
return;
}
int LCA(int x, int y)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
ll Dis(int x, int y)
{
if(!x || !y) return 0;
return dis[x] + dis[y] - dis[LCA(x, y)] * 2;
}
} A, B;
struct Path
{
P s, t; ll dis;
Path(P _s = {0, 0}, P _t = {0, 0}) {s = _s, t = _t, dis = B.Dis(s.fi, t.fi) + s.se + t.se;}
bool operator < (const Path &rhs) const {return dis < rhs.dis;}
friend Path operator + (Path x, Path y)
{
Path z = max(x, y);
z = max({z, Path(x.s, y.s), Path(x.s, y.t)});
z = max({z, Path(x.t, y.s), Path(x.t, y.t)});
return z;
}
};
#define ls (rt << 1)
#define rs (rt << 1 | 1)
#define mid ((l + r) >> 1)
Path t[N << 2];
ll tag[N << 2];
void pushtag(int rt, ll v)
{
t[rt].s.se += v, t[rt].t.se += v;
if(t[rt].t.fi) t[rt].dis += 2 * v;
tag[rt] += v;
}
void pushdown(int rt)
{
if(!tag[rt]) return;
pushtag(ls, tag[rt]), pushtag(rs, tag[rt]);
tag[rt] = 0;
}
void build(int l, int r, int rt)
{
if(l == r) return t[rt] = Path({A.id[l], B.dis[A.id[l]]}, {0, 0}), void();
build(l, mid, ls), build(mid + 1, r, rs);
t[rt] = t[ls] + t[rs];
}
void upd(int L, int R, int v, int l = 1, int r = n, int rt = 1)
{
if(L > R || l > R || r < L) return;
if(L <= l && r <= R) return pushtag(rt, v);
pushdown(rt);
upd(L, R, v, l, mid, ls), upd(L, R, v, mid + 1, r, rs);
t[rt] = t[ls] + t[rs];
}
void update(int l, int r, int v)
{
upd(1, n, v), upd(l, r, -2 * v);
}
int qry(int x)
{
Path p = t[1];
return max(B.Dis(x, p.s.fi) + p.s.se, B.Dis(x, p.t.fi) + p.t.se);
}
void dfs3(int u)
{
for(auto p : q[u]) ans[p.se] = qry(p.fi);
for(auto v : A.G[u])
{
if(v.fi == A.fa[u]) continue;
update(A.dfn[v.fi], A.dfn[v.fi] + A.siz[v.fi] - 1, v.se);
dfs3(v.fi);
update(A.dfn[v.fi], A.dfn[v.fi] + A.siz[v.fi] + 1, -v.se);
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m;
for(int i = 1, u, v, w; i < n; i++)
cin >> u >> v >> w, A.G[u].push_back({v, w}), A.G[v].push_back({u, w});
for(int i = 1, u, v, w; i < n; i++)
cin >> u >> v >> w, B.G[u].push_back({v, w}), B.G[v].push_back({u, w});
for(int i = 1, a, b; i <= m; i++)
cin >> a >> b, q[a].push_back({b, i});
A.dfs1(1, 0), A.dfs2(1, 1);
B.dfs1(1, 0), B.dfs2(1, 1);
build(1, n, 1); dfs3(1);
for(int i = 1; i <= m; i++) cout << ans[i] << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 31308kb
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: 3ms
memory: 31380kb
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: 1ms
memory: 32016kb
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: 55ms
memory: 35576kb
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:
-691706494 -752283121 1233171373 1355465610 279888841 1542053840 -1325564236 685418583 2041110191 -373100617 1442355420 -765028431 249807983 1985516874 830774882 -155481743 -1095301768 -557816708 787159840 -127702595 -1284450122 1825391115 1439957004 1292996216 1020792661 -600414153 1543196635 -1815...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '-691706494'