QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#562816#5439. Meet in the MiddleWuyanruWA 25ms31168kbC++145.0kb2024-09-13 21:13:362024-09-13 21:13:36

Judging History

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

  • [2024-09-13 21:13:36]
  • 评测
  • 测评结果:WA
  • 用时:25ms
  • 内存:31168kb
  • [2024-09-13 21:13:36]
  • 提交

answer

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
inline int read()
{
    int s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
inline ll lread()
{
    ll s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
template<const int N,const int M>
struct graph
{
    int head[N+5];
    int ww[M+5];
    int t[M+5];
    int x[M+5];
    int cntm;
    graph(){ cntm=1;}
    inline void clear(int n=N)
    {
        cntm=1;
        for(int i=1;i<=n;i++) head[i]=0;
    }
    inline void ad(int u,int v,int w=0)
    {
        cntm++;
        t[cntm]=v;
        x[cntm]=head[u];
        ww[cntm]=w;
        head[u]=cntm;
    }
    inline void add(int u,int v,int w=0)
    {
        ad(u,v,w);
        ad(v,u,w);
    }
    inline int st(int num){ return head[num];}
    inline int to(int num){ return t[num];}
    inline int nx(int num){ return x[num];}
    inline int w(int num){ return ww[num];}
};
graph<100000,200000>g1,g2;
int numa[400001];
ll da[400001];
int numb[400001];
ll db[400001];
ll tag[400001];
int st[100001][21];
int dfn[100001];
ll qans[500001];
int b[500001];
vc<int>id[100001];
ll depa[100001];
ll depb[100001];
int wtf[100001];
int rk[100001];
int siz[100001];
int n,m,tim,c;
void DEP(int num,int fa)
{
    wtf[num]=++c,rk[c]=num,siz[num]=1;
    for(int i=g1.st(num);i;i=g1.nx(i))
    {
        int p=g1.to(i);
        if(p==fa) continue;
        depa[p]=depa[num]+g1.w(i);
        DEP(p,num);siz[num]+=siz[p];
    }
}
void init(int num,int fa)
{
    dfn[num]=++tim,st[tim][0]=fa;
    for(int i=g2.st(num);i;i=g2.nx(i))
    {
        int p=g2.to(i);
        if(p==fa) continue;
        depb[p]=depb[num]+g2.w(i);
        init(p,num);
    }
}
inline int lca(int u,int v)
{
    if(u==v) return u;
    if(dfn[u]>dfn[v]) swap(u,v);
    int num=31-__builtin_clz(dfn[v]-dfn[u]);
    int p=st[dfn[u]+1][num],q=st[dfn[v]-(1<<num)+1][num];
    return depb[p]<depb[q]?p:q;
}
inline ll disb(int u,int v)
{
    int p=lca(u,v);
    return depb[u]+depb[v]-2*depb[p];
}
inline void update(int &v1,ll &v2,int &v3,ll &v4,ll &w,int a,ll da,int b,ll db)
{
    ll dis=disb(a,b)+da+db;
    if(dis>w)
    {
        w=dis;
        v1=a,v2=da;
        v3=b,v4=db;
    }
}
inline void push_up(int p)
{
    ll w=-1;int u=p*2,v=p*2|1;
    update(numa[p],da[p],numb[p],db[p],w,numa[u],da[u],numb[u],db[u]);
    update(numa[p],da[p],numb[p],db[p],w,numa[v],da[v],numb[v],db[v]);
    update(numa[p],da[p],numb[p],db[p],w,numa[u],da[u],numb[v],db[v]);
    update(numa[p],da[p],numb[p],db[p],w,numa[v],da[v],numb[u],db[u]);
    update(numa[p],da[p],numb[p],db[p],w,numa[u],da[u],numa[v],da[v]);
    update(numa[p],da[p],numb[p],db[p],w,numb[u],db[u],numb[v],db[v]);
assert(w>-1);
}
void build(int p,int pl,int pr)
{
    if(pl==pr)
    {
        int nod=rk[pl];
        tag[p]=depa[nod];
        numa[p]=numb[p]=nod;
        da[p]=db[p]=depa[nod];
        return ;
    }
    int mid=(pl+pr)>>1;
    build(p*2,pl,mid);
    build(p*2|1,mid+1,pr);
    push_up(p);
}
inline void T(int p,ll y)
{
    tag[p]+=y;
    da[p]+=y;
    db[p]+=y;
}
inline void push_down(int p)
{
    T(p*2,tag[p]);
    T(p*2|1,tag[p]);
    tag[p]=0;
}
void add(int p,int pl,int pr,int l,int r,ll y)
{
    if(l<=pl&&pr<=r){ T(p,y);return ;}
    int mid=(pl+pr)>>1;push_down(p);
    if(l<=mid) add(p*2,pl,mid,l,r,y);
    if(mid<r) add(p*2|1,mid+1,pr,l,r,y);
    push_up(p);
}
void run(int num,int fa)
{
    for(int i:id[num])
    {
        ll w1=disb(b[i],numa[1])+da[1];
        ll w2=disb(b[i],numb[1])+db[1];
        qans[i]=max(w1,w2);
    }
    for(int i=g1.st(num);i;i=g1.nx(i))
    {
        int p=g1.to(i);if(p==fa) continue;
        add(1,1,n,1,n,g1.w(i));
        add(1,1,n,wtf[p],wtf[p]+siz[p]-1,-2*g1.w(i));
        run(p,num);
        add(1,1,n,wtf[p],wtf[p]+siz[p]-1,2*g1.w(i));
        add(1,1,n,1,n,-g1.w(i));
    }
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read(),w=read();
        g1.add(u,v,w);
    }
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read(),w=read();
        g2.ad(u,v,w);
    }

    DEP(1,1),init(1,1);
    // printf("???\n");
    for(int i=1;i<=16;i++) for(int j=1;j+(1<<i)-1<=n;j++)
    {
        // printf("i=%d j=%d\n",i,j);
        int u=st[j][i-1],v=st[j+(1<<(i-1))][i-1];
        st[j][i]=depb[u]<depb[v]?u:v;
    }
    // printf("???\n");
    build(1,1,n);
    // printf("???\n");

    for(int i=1;i<=m;i++)
    {
        int a=read();b[i]=read();
        id[a].push_back(i);
    }
    run(1,1);

    for(int i=1;i<=m;i++) printf("%lld\n",qans[i]);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 26320kb

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

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

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: 25ms
memory: 31168kb

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:

370054055625
279829783432
269996704995
382721987783
280267764264
393774249978
350649923217
310673281256
370836138066
504574892847
324234672982
402375941187
410686842659
508172830360
280351613083
318850252908
364825700024
361824603132
320120511640
436019586833
346546265072
355166054838
521869834235
3...

result:

wrong answer 1st numbers differ - expected: '647838384844', found: '370054055625'