QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188334 | #5148. Tree Distance | zhouhuanyi | WA | 394ms | 79704kb | C++14 | 2.9kb | 2023-09-25 19:07:56 | 2023-09-29 19:17:53 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 200000
using namespace std;
const int inf=(int)(1e9);
const long long INF=(long long)(1e18);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
struct reads
{
int num;
long long data;
bool operator < (const reads &t)const
{
return num<t.num;
}
};
reads tong[N+1];
int n,q,rt,leng,length,smz,sz[N+1],szt[N+1],maxp[N+1],dque[N+1],top;
long long dis[N+1],ans[N+1],c[N+1];
vector<reads>E[N+1];
vector<reads>p[N+1];
vector<reads>v[N+1];
bool used[N+1],vis[N+1];
void add(int x,int y,int z)
{
E[x].push_back((reads){y,z}),E[y].push_back((reads){x,z});
return;
}
void get_rt(int x)
{
vis[x]=sz[x]=1,maxp[x]=0;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num]&&!vis[E[x][i].num])
get_rt(E[x][i].num),sz[x]+=sz[E[x][i].num],maxp[x]=max(maxp[x],sz[E[x][i].num]);
maxp[x]=max(maxp[x],smz-sz[x]);
if (maxp[x]<maxp[rt]) rt=x;
vis[x]=0;
return;
}
void dfs(int x)
{
vis[x]=szt[x]=1,tong[++length]=(reads){x,dis[x]};
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num]&&!vis[E[x][i].num])
dis[E[x][i].num]=dis[x]+E[x][i].data,dfs(E[x][i].num),szt[x]+=szt[E[x][i].num];
return;
}
void solve(int x)
{
used[x]=1,dis[x]=0,tong[length=1]=(reads){x,0};
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num])
dis[E[x][i].num]=E[x][i].data,dfs(E[x][i].num);
sort(tong+1,tong+length+1),top=0;
for (int i=1;i<=length;++i)
{
while (top&&tong[dque[top]].data>=tong[i].data) top--;
if (top) p[tong[i].num].push_back((reads){tong[dque[top]].num,tong[dque[top]].data+tong[i].data});
dque[++top]=i;
}
top=0;
for (int i=length;i>=1;--i)
{
while (top&&tong[dque[top]].data>tong[i].data) top--;
if (top) p[tong[dque[top]].num].push_back((reads){tong[i].num,tong[i].data+tong[dque[top]].data});
dque[++top]=i;
}
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num])
smz=szt[E[x][i].num],rt=0,get_rt(E[x][i].num),solve(rt);
return;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int x,long long d)
{
for (;x<=n;x+=lowbit(x)) c[x]=min(c[x],d);
return;
}
long long query(int x)
{
long long res=INF;
for (;x>=1;x-=lowbit(x)) res=min(res,c[x]);
return res;
}
int main()
{
int x,y,z,l,r;
n=read(),maxp[0]=inf;
for (int i=1;i<=n;++i) c[i]=INF;
for (int i=1;i<=n-1;++i) x=read(),y=read(),z=read(),add(x,y,z);
q=read();
for (int i=1;i<=q;++i)
{
l=read(),r=read();
if (l==r) ans[i]=-1;
else v[r].push_back((reads){l,i});
}
smz=n,get_rt(1),solve(rt);
for (int i=1;i<=n;++i)
{
for (int j=0;j<p[i].size();++j) add(n-p[i][j].num+1,p[i][j].data);
for (int j=0;j<v[i].size();++j) ans[v[i][j].data]=query(n-v[i][j].num+1);
}
for (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: 25896kb
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
Wrong Answer
time: 394ms
memory: 79704kb
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...
output:
result:
wrong answer Answer contains longer sequence [length = 999999], but output contains 0 elements