QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#76090 | #5148. Tree Distance | aoui | WA | 1569ms | 130180kb | C++20 | 2.1kb | 2023-02-07 16:39:35 | 2023-02-07 16:39:38 |
Judging History
answer
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+5,M=1e6+5;
int n,m,Q,Si,rt,si[N],tp[N],bz[N],s[N];
long long ans[M],t[N],dep[N];
#include<vector>
vector<pair<int,int> > to[N];
void rfs(int x,int ff)
{
si[x]=1;
int ma=0;
for(auto i:to[x])
{
int y=i.first;
if(y==ff||bz[y])continue;
rfs(y,x);
si[x]+=si[y];
ma=max(ma,si[y]);
}
ma=max(ma,m-si[x]);
if(ma<Si)rt=x,Si=ma;
}
vector<pair<long long,int> > p;
void ifs(int x,int ff,int tt)
{
tp[x]=tt;
p.push_back({dep[x],-x});
for(auto i:to[x])
{
int y=i.first,z=i.second;
if(y==ff||bz[y])continue;
dep[y]=dep[x]+z;
ifs(y,x,tt);
}
}
struct node{int l,r,id;long long x;};
bool cmp(node x,node y){return x.l==y.l?(x.r==y.r?x.id<y.id:x.r<y.r):x.l>y.l;}
vector<node> q;
void sfs(int x)
{
p.clear();
p.push_back({dep[x]=0,-x});
tp[x]=x;
for(auto i:to[x])
{
int y=i.first,z=i.second;
if(bz[y])continue;
dep[y]=dep[x]+z;
ifs(y,x,y);
}
sort(p.begin(),p.end());
s[0]=0;
for(auto i:p)
{
int y=-i.second;
for(;s[0]&&y<s[s[0]];s[0]--)
if(tp[s[s[0]]]!=tp[y])q.push_back((node){y,s[s[0]],0,dep[y]+dep[s[s[0]]]});
if(s[0]&&tp[s[s[0]]]!=tp[y])q.push_back((node){s[s[0]],y,0,dep[y]+dep[s[s[0]]]});
s[++s[0]]=y;
}
bz[x]=1;
int res=m;
for(auto i:to[x])
{
int y=i.first;
if(bz[y])continue;
Si=m=si[y]>si[x]?res-si[x]:si[y];
rfs(rt=y,0);
sfs(rt);
}
}
void add(int x,long long y)
{
for(;x<=n;x+=x&-x)t[x]=min(t[x],y);
}
long long ask(int x)
{
long long res=1e18;
for(;x;x-=x&-x)res=min(res,t[x]);
return res;
}
int main()
{
scanf("%d",&n);
for(int x,y,z,i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
to[x].push_back({y,z});
to[y].push_back({x,z});
}
Si=m=n;
rfs(rt=1,0);
sfs(rt);
scanf("%d",&Q);
for(int l,r,i=1;i<=Q;i++)
{
scanf("%d%d",&l,&r);
q.push_back((node){l,r,i,0});
}
sort(q.begin(),q.end(),cmp);
for(int i=1;i<=n;i++)t[i]=1e18;
for(auto i:q)
{
int l=i.l,r=i.r;
if(i.id)ans[i.id]=ask(r);
else add(r,i.x);
}
for(int i=1;i<=Q;i++)
if(ans[i]==1e18)puts("-1");
else printf("%lld\n",ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 14744kb
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: 1569ms
memory: 130180kb
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:
29573323 -1 4088 65959 4366 -1 760172089 4867978 328055210 -1 35487219223 339719 92126 92126 4366 138216269 8212187 -1 2681285 4366 710854 114886 65959 3099557351 92126 91087 241621631057 26776689 5718199 710854 92126 -1 525277383 92126 710854 92126 710854 4366 -1 -1 16211870 21085 1350361 1350361 4...
result:
wrong answer 2nd numbers differ - expected: '1178569098929', found: '-1'