QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#774989#5439. Meet in the MiddleWaterSunWA 39ms60508kbC++145.1kb2024-11-23 14:25:252024-11-23 14:25:25

Judging History

你现在查看的是最新测评结果

  • [2024-11-23 14:25:25]
  • 评测
  • 测评结果:WA
  • 用时:39ms
  • 内存:60508kb
  • [2024-11-23 14:25:25]
  • 提交

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] = 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] = 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++){
                int a = dp[i][j - 1],b = dp[i + pot(j - 1)][j - 1];
                if (dep[a] < dep[b]) dp[i][j] = a;
                else dp[i][j] = b;
            }
        }
    }

    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 * dep[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: 0ms
memory: 50920kb

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: 0ms
memory: 52920kb

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: 51068kb

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: 60508kb

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:

647838384844
620313646592
508047795120
637066393138
466320233012
645842915960
641537859258
567378358372
637855428886
797649304882
656925021390
728537900332
730063093987
748978672062
553499885160
421608593349
595573998060
665244139546
444026071412
720585291576
638334522666
660535845378
685091533589
6...

result:

wrong answer 2nd numbers differ - expected: '626539793176', found: '620313646592'