QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#773887 | #5439. Meet in the Middle | WaterSun | WA | 39ms | 63704kb | C++14 | 5.0kb | 2024-11-23 10:43:32 | 2024-11-23 10:43:32 |
Judging History
answer
#include <bits/stdc++.h>
#define re register
#define fst first
#define snd second
#define int long long
#define chmax(a,b) (a = max(a,(b)))
using namespace std;
typedef pair<int,int> pii;
const int N = 5e5 + 10;
int n,q,ans[N];
int tim,sz[N],dfn[N],pid[N],dep[N];
vector<pii> g1[N],g2[N],Q[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
namespace LCA{
#define pot(x) (1 << (x))
const int M = 24;
int tim,lg[N],dfn[N],dep[N],dp[N][M];
inline void dfs(int u,int fa){
dp[dfn[u] = ++tim][0] = dep[u];
for (pii v:g2[u]){
if (v.fst == fa) continue;
dep[v.fst] = dep[u] + v.snd;
dfs(v.fst,u);
}
dp[++tim][0] = dep[u];
}
inline void init(){
dfs(1,0);
for (re int i = 2;i <= tim;i++) lg[i] = lg[i >> 1] + 1;
for (re int j = 1;j <= lg[tim];j++){
for (re int i = 1;i + pot(j) - 1 <= tim;i++) dp[i][j] = min(dp[i][j - 1],dp[i + pot(j - 1)][j - 1]);
}
}
inline int lca(int x,int y){
int sx = dfn[x],sy = dfn[y];
if (sx > sy) swap(sx,sy);
int t = lg[sy - sx + 1];
return min(dp[sx][t],dp[sy - pot(t) + 1][t]);
}
inline int dist(int x,int y){
return (dep[x] + dep[y] - 2 * lca(x,y));
}
#undef pot
}
using LCA::dist;
struct{
#define ls(u) (u << 1)
#define rs(u) (u << 1 | 1)
#define val(u) (tr[u].val)
struct value{
int a,b,vala,valb;
bool friend operator <(const value &a,const value &b){
#define calc(x) (x.a == x.b ? 0 : (dist(x.a,x.b) + x.vala + x.valb))
return calc(a) < calc(b);
#undef calc
}
value friend operator +(const value &a,const value &b){
value res = max(a,b);
chmax(res,((value){a.a,b.a,a.vala,b.vala}));
chmax(res,((value){a.a,b.b,a.vala,b.valb}));
chmax(res,((value){a.b,b.a,a.valb,b.vala}));
chmax(res,((value){a.b,b.b,a.valb,b.valb}));
return res;
}
};
struct node{
int l,r,tag;
value val;
}tr[N << 2];
inline void calc(int u,int k){
tr[u].tag += k;
val(u).vala += k; val(u).valb += k;
}
inline void pushup(int u){
val(u) = val(ls(u)) + val(rs(u));
}
inline void pushdown(int u){
if (tr[u].tag){
calc(ls(u),tr[u].tag); calc(rs(u),tr[u].tag);
tr[u].tag = 0;
}
}
inline void build(int u,int l,int r){
tr[u] = {l,r};
if (l == r) return (val(u) = {pid[l],pid[l],dep[pid[l]],dep[pid[l]]}),void();
int mid = l + r >> 1;
build(ls(u),l,mid); build(rs(u),mid + 1,r);
pushup(u);
}
inline void modify(int u,int l,int r,int k){
// cerr << u << " " << tr[u].l << " " << tr[u].r << " " << l << " " << r << " modify\n";
if (l <= tr[u].l && tr[u].r <= r) return calc(u,k);
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(ls(u),l,r,k);
if (r > mid) modify(rs(u),l,r,k);
pushup(u);
}
#undef ls
#undef rs
#undef val
}T;
inline void dfs1(int u,int fa){
sz[u] = 1;
pid[dfn[u] = ++tim] = u;
for (pii v:g1[u]){
if (v.fst == fa) continue;
dep[v.fst] = dep[u] + v.snd;
dfs1(v.fst,u); sz[u] += sz[v.fst];
}
}
inline void dfs2(int u,int fa){
auto val = T.tr[1].val;
// cerr << u << " ????\n";
for (pii p:Q[u]) ans[p.snd] = max(dist(p.fst,val.a) + val.vala,dist(p.fst,val.b) + val.valb);
for (pii v:g1[u]){
if (v.fst == fa) continue;
T.modify(1,1,n,v.snd);
// cerr << v.fst << " " << dfn[v.fst] << " " << sz[v.fst] << " sbsb\n";
T.modify(1,dfn[v.fst],dfn[v.fst] + sz[v.fst] - 1,-2 * v.snd);
dfs2(v.fst,u);
T.modify(1,1,n,-v.snd);
T.modify(1,dfn[v.fst],dfn[v.fst] + sz[v.fst] - 1,2 * v.snd);
}
}
signed main(){
n = read(),q = read();
for (re int i = 1,a,b,c;i < n;i++){
a = read(),b = read(),c = read();
g1[a].push_back({b,c});
g1[b].push_back({a,c});
}
for (re int i = 1,a,b,c;i < n;i++){
a = read(),b = read(),c = read();
g2[a].push_back({b,c});
g2[b].push_back({a,c});
}
for (re int i = 1,x,y;i <= q;i++){
x = read(),y = read();
// cerr << x << " " << y << " ???\n";
Q[x].push_back({y,i});
}
dfs1(1,0); LCA::init(); T.build(1,1,n); dfs2(1,0);
// for (re int i = 1;i <= n;i++) cerr << dfn[i] << " "; cerr << "\n";
// for (re int i = 1;i <= n;i++) cerr << sz[i] << " "; cerr << "\n";
for (re int i = 1;i <= q;i++) printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 50940kb
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: 50924kb
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: 0ms
memory: 50892kb
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: 39ms
memory: 63704kb
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:
647795615748 626497024080 514231172608 637023624042 472503610500 645800146864 641495090162 573561735860 644038806374 803832682370 674327780890 734721277820 744820046345 763735624420 553457116064 526701055663 610330950418 689507477946 549118533726 726768669064 653091475024 666719222866 701575393972 6...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '647795615748'