QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282815 | #5148. Tree Distance | WanXiangYun | TL | 8ms | 73492kb | C++14 | 4.0kb | 2023-12-13 09:52:02 | 2023-12-13 09:52:03 |
Judging History
answer
#pragma GCC optimize(fast)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long
#define PII pair <int,int>
#define lowbit(x) (x&(-x))
#define N 1000010
const int INF=1e18;
int n,m,tot,head[N];
int idx,dfn[N],anc[21][N],dep[N],dist[N];
int root,MinSize,AllSize,siz[N],maxsiz[N],vis[N];
int cnt,buc[N],top,st[N],ans[N];
vector <int> vecT[N];
vector < PII > vecQ[N];
//注意,此处存的二元组(x,y)保证x>y,方便根据右端点排序
struct NodeA
{
int nxt,to,val;
}edge[N<<1];
struct NodeB
{
int minv[N];
void Clear ()
{
for (int i=1;i<=n;i++)
minv[i]=INF;
}
void Update (int x,int val)
{
while (x<=n)
{
minv[x]=min (minv[x],val);
x+=lowbit (x);
}
}
int Query (int x)
{
int res=INF;
while (x>0)
{
res=min (res,minv[x]);
x-=lowbit (x);
}
return res;
}
}BIT;
int read ()
{
int k=1,s=0;char ch=getchar ();
while (!isdigit (ch)) {if (ch=='-') k=-1;ch=getchar ();}
while (isdigit (ch)) {s=s*10+ch-'0';ch=getchar ();}
return k*s;
}
void AddEdge (int x,int y,int w)
{
edge[++tot]=(NodeA) {head[x],y,w};
head[x]=tot;
}
void FindRoot (int x,int from)
{
siz[x]=1;
maxsiz[x]=0;
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to;
if (y==from || vis[y]) continue;
FindRoot (y,x);
siz[x]+=siz[y];
maxsiz[x]=max (maxsiz[x],siz[y]);
}
maxsiz[x]=max (maxsiz[x],AllSize-siz[x]);
if (maxsiz[x]<MinSize)
{
MinSize=maxsiz[x];
root=x;
}
}
void FindSon (int x,int from)
{
buc[++cnt]=x;
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to;
if (y==from || vis[y]) continue;
FindSon (y,x);
}
}
void DFS1 (int x,int from)
{
dep[x]=dep[from]+1;
anc[0][dfn[x]=++idx]=from;
siz[x]=1;
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to,w=edge[i].val;
if (y==from) continue;
dist[y]=dist[x]+w;
DFS1 (y,x);
siz[x]+=siz[y];
}
}
int Getmin (int x,int y)
{
return dfn[x]<dfn[y]?x:y;
}
int GetLCA (int x,int y)
{
if (x==y) return x;
if (dfn[x]>dfn[y]) swap (x,y);
x=dfn[x]+1,y=dfn[y];
int k=log2 (y-x+1);
return Getmin (anc[k][x],anc[k][y-(1<<k)+1]);
}
void GetST ()
{
for (int k=1;k<=20;k++)
for (int i=1;i+(1<<k)-1<=n;i++)
anc[k][i]=Getmin (anc[k-1][i],anc[k-1][i+(1<<(k-1))]);
}
int GetDist (int x,int y)
{
int z=GetLCA (x,y);
return dist[x]+dist[y]-2*dist[z];
}
void DFS2 (int x)
{
vis[x]=1;
cnt=0;
buc[++cnt]=x;
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to;
if (vis[y]) continue;
FindSon (y,x);
}
sort (buc+1,buc+1+cnt);
top=0;
for (int i=1;i<=cnt;i++)
{
while (top>0 && GetDist (x,st[top])>GetDist (x,buc[i])) top--;
if (top>0) vecT[st[top]].push_back (buc[i]);
st[++top]=buc[i];
}
reverse (buc+1,buc+1+cnt);
top=0;
for (int i=1;i<=cnt;i++)
{
while (top>0 && GetDist (x,st[top])>GetDist (x,buc[i])) top--;
if (top>0) vecT[buc[i]].push_back (st[top]);
st[++top]=buc[i];
}
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to;
if (vis[y]) continue;
root=0,AllSize=siz[y],MinSize=siz[y];
FindRoot (root,0);
DFS2 (y);
}
}
void Init ()
{
n=read ();
for (int i=1;i<n;i++)
{
int x=read (),y=read (),w=read ();
AddEdge (x,y,w);
AddEdge (y,x,w);
}
m=read ();
for (int i=1;i<=m;i++)
{
int l=read (),r=read ();
vecQ[l].push_back (make_pair (r,i));
}
}
void Work ()
{
root=0,MinSize=n,AllSize=n;
FindRoot (1,0);
DFS1 (root,0);
GetST ();
DFS2 (root);
BIT.Clear ();
for (int i=n;i>=1;i--)
{
for (int j=0;j<vecT[i].size ();j++)
{
int k=vecT[i][j];
BIT.Update (k,GetDist (k,i));
}
for (int j=0;j<vecQ[i].size ();j++)
{
int k=vecQ[i][j].first,id=vecQ[i][j].second;
ans[id]=BIT.Query (k);
}
}
for (int i=1;i<=m;i++)
if (ans[i]!=INF) printf ("%lld\n",ans[i]);
else printf ("-1\n");
}
signed main ()
{
Init ();
Work ();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 73492kb
input:
5 1 2 5 1 3 3 1 4 4 3 5 2 5 1 1 1 4 2 4 3 4 2 5
output:
-1 3 7 7 2
result:
ok 5 number(s): "-1 3 7 7 2"
Test #2:
score: -100
Time Limit Exceeded
input:
199999 31581 23211 322548833 176307 196803 690953895 34430 82902 340232856 36716 77480 466375266 7512 88480 197594480 95680 61864 679567992 19572 14126 599247796 188006 110716 817477802 160165 184035 722372640 23173 188594 490365246 54801 56250 304741654 10103 45884 643490340 127469 154479 214399361...