QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#596964 | #5148. Tree Distance | louhao088# | WA | 1527ms | 130284kb | C++23 | 3.4kb | 2024-09-28 16:46:42 | 2024-09-28 16:46:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pr;
typedef long long ll;
const int MAXN=2e5,MAXQ=1e6,LOGN=20,SIZE=2*MAXN*LOGN;
const ll INF=1e18;
inline int Read()
{
int res;char c;
while(1) {c=getchar();if('0'<=c && c<='9') {res=c-'0';break;}}
while(1) {c=getchar();if('0'<=c && c<='9') res=res*10+c-'0';else break;}
return res;
}
int n,q,QL[MAXQ+5],QR[MAXQ+5];
vector<pr> Tree[MAXN+5];
int Size[MAXN+5];
int CL[SIZE+5],CR[SIZE+5],val[SIZE+5],tot;
int A[MAXQ+5],B[SIZE+5];
ll ans[MAXQ+5];
inline bool cmp0(int a,int b) {return QR[a]<QR[b];}
inline bool cmp1(int a,int b) {return CR[a]<CR[b];}
bool V[MAXN+5];
void CalSize(int now,int fa)
{
Size[now]=1;
for(int i=0,rear;i<Tree[now].size();i++)
{
rear=Tree[now][i].first;
if(rear==fa || V[rear]) continue;
CalSize(rear,now);
Size[now]+=Size[rear];
}
}
int FindGC(int now,int fa,int ALL)
{
bool OK=1;
for(int i=0,rear;i<Tree[now].size();i++)
{
rear=Tree[now][i].first;
if(rear==fa || V[rear]) continue;
if(2*Size[rear]>ALL) {OK=0;break;}
}
if(2*Size[now]>=ALL && OK) return now;
for(int i=0,rear,res;i<Tree[now].size();i++)
{
rear=Tree[now][i].first;
if(rear==fa || V[rear]) continue;
res=FindGC(rear,now,ALL);
if(res) return res;
}
return 0;
}
int Q[MAXN+5],Tail;ll dis[MAXN+5];
void CalMsg(int now,int fa)
{
Q[++Tail]=now;
for(int i=0,rear;i<Tree[now].size();i++)
{
rear=Tree[now][i].first;
if(rear==fa || V[rear]) continue;
dis[rear]=dis[now]+Tree[now][i].second;
CalMsg(rear,now);
}
}
int stk[MAXN+5],tp;
void Solve(int now)
{
CalSize(now,0),now=FindGC(now,0,Size[now]);
Tail=dis[now]=0,CalMsg(now,0);
sort(Q+1,Q+Tail+1);
tp=0;
for(int i=1;i<=Tail;i++)
{
while(tp)
if(dis[Q[i]]<=dis[stk[tp]]) {++tot,CL[tot]=stk[tp],CR[tot]=Q[i],val[tot]=dis[stk[tp--]]+dis[Q[i]];}
else break;
stk[++tp]=Q[i];
}
tp=0;
for(int i=Tail;i;i--)
{
while(tp)
if(dis[Q[i]]<=dis[stk[tp]]) {++tot,CL[tot]=Q[i],CR[tot]=stk[tp],val[tot]=dis[stk[tp--]]+dis[Q[i]];}
else break;
stk[++tp]=Q[i];
}
V[now]=1;
for(int i=0,rear;i<Tree[now].size();i++)
{
rear=Tree[now][i].first;
if(V[rear]) continue;
Solve(rear);
}
}
ll minn[MAXN+5];
inline void Init() {for(int i=1;i<=n;i++) minn[i]=INF;}
inline int lowbit(int x) {return x&-x;}
inline void Modify(int x,ll v) {for(;x;x-=lowbit(x)) minn[x]=min(minn[x],v);}
inline ll GetMin(int x) {ll res=INF;for(;x<=n;x+=lowbit(x)) res=min(res,minn[x]);return res;}
int main()
{
n=Read();
for(int i=1,a,b,c;i<n;i++)
{
a=Read(),b=Read(),c=Read();
Tree[a].push_back(make_pair(b,c));
Tree[b].push_back(make_pair(a,c));
}
q=Read();
for(int i=1;i<=q;i++) QL[i]=Read(),QR[i]=Read();
Solve(1);
for(int i=1;i<=q;i++) A[i]=i;
for(int i=1;i<=tot;i++) B[i]=i;
sort(A+1,A+q+1,cmp0),sort(B+1,B+tot+1,cmp1);
Init();
for(int i=1,j=1;i<=q;i++)
{
while(j<=tot && CR[B[j]]<=QR[A[i]]) Modify(CL[B[j]],val[B[j]]),++j;
ans[A[i]]=GetMin(QL[A[i]]);
}
for(int i=1;i<=q;i++)
if(ans[i]<INF) printf("%lld\n",ans[i]);
else printf("-1\n");
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18156kb
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: 1527ms
memory: 130284kb
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:
-2147437829 -1677423355 -2147483271 -2147475012 -2147483152 -2146130731 -2147481721 -2147409703 -2146326742 -2087166198 -2146599269 -2147483152 -2147481721 -2147476385 -2147483152 -2147415440 -2147416357 -2144090692 -2147479455 -2147483152 -2147481721 -2147483152 -2147481721 -2146842961 -2147468741 ...
result:
wrong answer 1st numbers differ - expected: '29573323', found: '-2147437829'