QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1006#645777#9416. Intersection of Pathscyz2010msk_samaFailed.2024-10-16 22:16:182024-10-16 22:16:19

Details

Extra Test:

Invalid Input

input:

500000 500000
1 2 725437625
1 3 184841067
2 4 139090798
3 5 886470346
4 6 164199002
5 7 763725905
2 8 683809569
8 9 724583922
1 10 383924149
3 11 803996877
5 12 853999394
4 13 218779738
9 14 950897108
2 15 897086552
3 16 108192394
6 17 98436126
7 18 565522308
15 19 44289059
1 20 6...

output:


result:

FAIL Expected EOLN (stdin, line 1)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#645777#9416. Intersection of Pathsmsk_samaAC ✓1963ms232192kbC++143.9kb2024-10-16 19:48:422024-10-16 19:48:46

answer

#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <ctime>
#include <cstdio>
#include <random>
#include <vector>
#include <bitset>
#include <cassert>
#include <cstring>
#include <algorithm>
#define MISAKA main
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define _rep(i,a,b) for(int i=a;i>=b;--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
const int IN=1e8;
inline char _getch(){
    static char buf[IN],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,IN,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
   char ch=_getch();int f=1,x=0;
   while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=_getch();}
   while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=_getch();}
   return x*f;
}
const double eps=1e-9;
const int N=5e5+10,mod=998244353;
int n,m,st[N<<1][20],tag[N],F[N],lg[N],k=0,dep[N],dfn[N],id[N],pos[N],siz[N],vis[N],tim=0;ll ans[N],D[N];
struct Edge{int v,w;};vector<Edge> g[N];
struct edge{int u,v,w,id;}e[N];vector<edge> E[N];
struct node{int a,b,id;};vector<node> q[N];
void dfs(int u,int fa){
    st[pos[u]=++k][0]=u;id[dfn[u]=++tim]=u;siz[u]=1;dep[u]=dep[fa]+1;F[u]=fa;
    for(auto [v,w]:g[u])if(v!=fa) D[v]=D[u]+w,dfs(v,u),st[++k][0]=u,siz[u]+=siz[v];
}
int _min(int a,int b){return dep[a]<dep[b]?a:b;}
int lca(int x,int y){
    x=pos[x],y=pos[y];if(x>y) swap(x,y);
    int k=lg[y-x+1];return _min(st[x][k],st[y-(1<<k)+1][k]);
}
ll dis(int x,int y){return D[x]+D[y]-(D[lca(x,y)]<<1);}
struct tnode{int l,r,p[2];}t[N<<2];
tnode mg(tnode X,tnode Y){
    if(!X.p[0]) return {X.l,Y.r,Y.p[0],Y.p[1]};
    if(!Y.p[0]) return {X.l,Y.r,X.p[0],X.p[1]};
    int nx=X.p[0],ny=X.p[1];
    rep(i,0,1)rep(j,0,1){
        int x=X.p[i],y=Y.p[j];
        if(dis(x,y)>dis(nx,ny)) nx=x,ny=y;
    }
    int x=Y.p[0],y=Y.p[1];
    if(dis(x,y)>dis(nx,ny)) nx=x,ny=y;
    return {X.l,Y.r,nx,ny};
}
void pushup(int now){t[now]=mg(t[2*now],t[2*now+1]);}
void build(int now,int l,int r){
    t[now].l=l,t[now].r=r;int mid=l+r>>1;
    t[now].p[0]=t[now].p[1]=0;
    if(t[now].l==t[now].r) return;
    build(2*now,l,mid);build(2*now+1,mid+1,r);
}
void upd(int now,int x){
    if(t[now].l==t[now].r){t[now].p[0]=t[now].p[1]=id[x];return;}
    upd(2*now+(x>t[2*now].r),x);pushup(now);
}   
tnode qry(int now,int l,int r){
    if(t[now].l>r||t[now].r<l) return {0,0,0,0};
    if(l<=t[now].l&&t[now].r<=r) return t[now];
    return mg(qry(2*now,l,r),qry(2*now+1,l,r));
}
signed MISAKA(){
    // freopen("data.in","r",stdin);
    n=read(),m=read();lg[0]=-1;
    rep(i,1,n-1){
        int u=read(),v=read(),w=read();
        g[u].push_back({v,w});
        g[v].push_back({u,w});
        e[i]={u,v,w,i};
    }
    dfs(1,0);
    rep(i,1,n-1){
        int &x=e[i].u,&y=e[i].v;
        if(dep[x]>dep[y]) swap(x,y);
        int k=min(siz[y],n-siz[y]);
        E[k].push_back({x,y,e[i].w,i});
    }
    rep(i,1,k) lg[i]=lg[i>>1]+1;
    rep(j,1,19)rep(i,1,k-(1<<j)+1)
        st[i][j]=_min(st[i][j-1],st[i+(1<<j-1)][j-1]);
    build(1,1,n);
    rep(i,1,m){
        int a=read(),b=read(),k=read();
        q[k].push_back({a,b,i});
    }
    _rep(i,n/2,1){
        for(auto [x,y,w,id]:E[i]){
            vis[id]=1;
            if(!tag[dfn[x]]) upd(1,dfn[x]),tag[dfn[x]]=1;
            if(!tag[dfn[y]]) upd(1,dfn[y]),tag[dfn[y]]=1;
        }
        for(auto [a,b,id]:q[i]){
            if(vis[a]){
                int y=e[a].v,w=b-e[a].w;
                tnode X=qry(1,dfn[y],dfn[y]+siz[y]-1);
                tnode Y=mg(qry(1,1,dfn[y]-1),qry(1,dfn[y]+siz[y],n));
                ans[id]=max(dis(X.p[0],X.p[1]),dis(Y.p[0],Y.p[1]));
                rep(j,0,1)rep(k,0,1) ans[id]=max(ans[id],dis(X.p[j],Y.p[k])+w);
            }
            else ans[id]=dis(t[1].p[0],t[1].p[1]);
        }
    }
    // printf("%d",clock());
    rep(i,1,m) printf("%lld\n",ans[i]);
    return 0;
}