QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#152686 | #4924. 蜘蛛爬树 | zhouhuanyi | 0 | 2567ms | 271584kb | C++14 | 6.5kb | 2023-08-28 17:04:01 | 2023-08-28 17:04:03 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 800000
#define M 30
using namespace std;
long long read()
{
char c=0;
long long sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
const int inf=(int)(1e9);
struct reads
{
int num,snum;
long long data;
};
struct points
{
int x;
long long y;
};
int n,m,q,maxn,leng,length,rt,SD[N+1],X[N+1],Y[N+1],L[N+1],lg[N+1],p[N+1],fa[N+1],ls[N+1],rs[N+1],a[N+1],depth[N+1],smz,sz[N+1],minn,sx,sy,sd;
long long ans[N+1],dis[N+1][M+1],sw;
vector<reads>E[N+1];
vector<reads>ES[N+1];
vector<points>v[N+1];
bool used[N+1],vis[N+1];
void add(int x,int y,int z,long long w)
{
E[x].push_back((reads){y,z,w}),E[y].push_back((reads){x,z,w});
return;
}
void add2(int x,int y,long long z)
{
++leng,ES[x].push_back((reads){y,leng,z}),ES[y].push_back((reads){x,leng,z});
return;
}
bool cmp(int x,int y)
{
return a[x]<a[y];
}
void dfs(int x)
{
int nw=0;
long long d=0;
used[x]=1;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num])
{
dfs(E[x][i].num);
if (!nw) nw=E[x][i].num,d=E[x][i].data;
else ++length,add2(length,nw,d),add2(length,E[x][i].num,E[x][i].data),nw=length,d=0;
}
if (nw) add2(x,nw,d);
return;
}
void get_rt(int x)
{
vis[x]=sz[x]=1;
for (int i=0;i<ES[x].size();++i)
if (!used[ES[x][i].snum]&&!vis[ES[x][i].num])
{
get_rt(ES[x][i].num),sz[x]+=sz[ES[x][i].num];
if (max(sz[ES[x][i].num],smz-sz[ES[x][i].num])<minn) minn=max(sz[ES[x][i].num],smz-sz[ES[x][i].num]),sx=x,sy=ES[x][i].num,sd=ES[x][i].snum,sw=ES[x][i].data;
}
vis[x]=0;
return;
}
void dfs2(int x,int d)
{
vis[x]=1;
for (int i=0;i<ES[x].size();++i)
if (!used[ES[x][i].snum]&&!vis[ES[x][i].num])
dis[ES[x][i].num][d]=dis[x][d]+ES[x][i].data,dfs2(ES[x][i].num,d);
vis[x]=0;
return;
}
int solve(int x,int szt,int d)
{
smz=szt,minn=inf,get_rt(x);
if (minn==inf)
{
depth[x]=d;
return x;
}
int nw=++length,tx=sx,ty=sy,dx=szt-sz[sy],dy=sz[sy];
depth[nw]=d,used[sd]=1,dfs2(sx,d),dis[sy][d]=sw,dfs2(sy,d),ls[nw]=solve(tx,dx,d+1),rs[nw]=solve(ty,dy,d+1),fa[ls[nw]]=fa[rs[nw]]=nw;
return nw;
}
points operator + (points a,points b)
{
return (points){a.x+b.x,a.y+b.y};
}
points operator - (points a,points b)
{
return (points){a.x-b.x,a.y-b.y};
}
__int128 operator * (points a,points b)
{
return (__int128)(a.x)*b.y-(__int128)(a.y)*b.x;
}
bool check(points a,points b,points c)
{
if (a.x==b.x&&a.x==c.x) return b.y>=min(a.y,c.y);
return (b-a)*(c-a)<=0;
}
int lca(int x,int y)
{
if (depth[x]<depth[y]) swap(x,y);
while (depth[x]!=depth[y]) x=fa[x];
while (x!=y) x=fa[x],y=fa[y];
return x;
}
int main()
{
int x,y,sx,tx,d1,d2,d,sd,ps,l;
points ds;
long long s,t,z;
for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
length=n=read(),m=read(),q=read();
for (int i=1;i<=n;++i) a[i]=read(),p[i]=i;
sort(p+1,p+n+1,cmp);
for (int i=1;i<=n-1;++i) x=read(),y=read(),z=read(),add(x,y,i,z);
dfs(1);
for (int i=1;i<=n;++i) used[i]=0;
rt=solve(1,length,1);
for (int i=1;i<=q;++i) s=read(),t=read(),d1=(s-1)/n+1,d2=(t-1)/n+1,sd=SD[i]=abs(d1-d2),x=X[i]=s-(d1-1)*n,y=Y[i]=t-(d2-1)*n,l=L[i]=lca(X[i],Y[i]),ans[i]=dis[x][depth[l]]+dis[y][depth[l]]+1ll*min(a[x],a[y])*sd;
for (int i=1;i<=n;++i) maxn=max(maxn,depth[i]);
for (int w=1;w<=maxn;++w)
{
for (int i=1;i<=length;++i) v[i].clear();
for (int i=1;i<=n;++i)
{
sx=p[i];
for (int j=depth[p[i]];j>=w+1;--j)
{
ds=(points){a[p[i]],dis[p[i]][j-1]+dis[p[i]][w]};
while (v[sx].size()>=2&&check(v[sx][(int)(v[sx].size())-2],v[sx].back(),ds)) v[sx].pop_back();
v[sx].push_back(ds),sx=fa[sx];
}
}
for (int i=1;i<=q;++i)
{
x=X[i],y=Y[i],sd=SD[i],sx=l=L[i];
if (depth[l]>=w+2)
{
for (int j=depth[l];j>=w+2;--j) sx=fa[sx];
if (ls[fa[sx]]==sx)
{
d=depth[fa[sx]],tx=rs[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int j=lg[v[tx].size()];j>=0;--j)
if (ps+(1<<j)<v[tx].size()&&1ll*v[tx][ps+(1<<j)].x*sd+v[tx][ps+(1<<j)].y<1ll*v[tx][ps+(1<<j)-1].x*sd+v[tx][ps+(1<<j)-1].y)
ps+=(1<<j);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][w]+dis[y][w]);
}
}
else
{
d=depth[fa[sx]],tx=ls[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int j=lg[v[tx].size()];j>=0;--j)
if (ps+(1<<j)<v[tx].size()&&1ll*v[tx][ps+(1<<j)].x*sd+v[tx][ps+(1<<j)].y<1ll*v[tx][ps+(1<<j)-1].x*sd+v[tx][ps+(1<<j)-1].y)
ps+=(1<<j);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][w]+dis[y][w]);
}
}
}
if (depth[l]==w)
{
sx=x;
for (int j=depth[x];j>=depth[l]+2;--j)
{
if (ls[fa[sx]]==sx)
{
d=depth[l],tx=rs[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int k=lg[v[tx].size()];k>=0;--k)
if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
ps+=(1<<k);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][depth[l]]);
}
}
else
{
d=depth[l],tx=ls[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int k=lg[v[tx].size()];k>=0;--k)
if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
ps+=(1<<k);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][depth[l]]);
}
}
sx=fa[sx];
}
sx=y;
for (int j=depth[y];j>=depth[l]+2;--j)
{
if (ls[fa[sx]]==sx)
{
d=depth[l],tx=rs[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int k=lg[v[tx].size()];k>=0;--k)
if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
ps+=(1<<k);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][depth[l]]+dis[y][j-1]);
}
}
else
{
d=depth[l],tx=ls[fa[sx]];
if (!v[tx].empty())
{
ps=0;
for (int k=lg[v[tx].size()];k>=0;--k)
if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
ps+=(1<<k);
ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][depth[l]]+dis[y][j-1]);
}
}
sx=fa[sx];
}
}
}
}
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
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 15ms
memory: 87472kb
input:
97 99 1000 763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...
output:
6130845802041 10806758605627 3440559556796 5426989115608 4458875959622 1566659300400 7908007295597 1846030561682 5112206409383 6968388472340 4706970599850 5270158948178 4633066810868 3176122148295 2331646415266 961435137842 14353365296423 9675072605938 4256954122467 7333255321628 8376795894537 12319...
result:
wrong answer 35th lines differ - expected: '423024055356', found: '430514891236'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 2188ms
memory: 215116kb
input:
200000 20 200000 679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...
output:
920563165 270738856 355012553 363898450 515535908 734168762 81197110 448355845 204186827 966151314 377621564 856252543 311456222 368700872 197258906 567302636 172379629 579171621 1043838058 244996663 621435809 278057792 727463012 573783312 395879848 500677226 891900111 1031612062 771021332 691010101...
result:
wrong answer 191st lines differ - expected: '55840845', found: '66399869'
Subtask #4:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 1949ms
memory: 204708kb
input:
200000 1000000000 200000 28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...
output:
29294995135992468 9003943574137677 39324997066279292 37544709020512848 57388992119827952 54425124319330092 19450449300737912 25838911017710871 2608104102967357 32395369352281774 5765752637876701 65609495812941401 57820177390587134 1971831067795873 19213682025389514 30244870693646792 3672338761985429...
result:
wrong answer 1539th lines differ - expected: '1323333822875251', found: '1536123123631349'
Subtask #5:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 2567ms
memory: 271584kb
input:
199918 1000000000 199903 1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...
output:
1352416884531 1380463318391 923920163167 1525224977139 1405019709299 871233800983 721063481996 876194052054 1358007874327 127994985855 1230162209719 1532026808855 611656467332 1034111378925 414792924571 1316679734677 829000175757 1265411315424 821484360433 1051517948640 843712138862 582943943131 457...
result:
wrong answer 6th lines differ - expected: '869269749781', found: '871233800983'
Subtask #6:
score: 0
Wrong Answer
Test #43:
score: 0
Wrong Answer
time: 2279ms
memory: 217068kb
input:
200000 1000000000 200000 81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...
output:
516260625003899 380880451347644 183401242058615 56975236749493 349851829300288 188845759476214 188011317678919 414887287533565 111834744858133 305218494040213 227244584301956 365579485207024 201761449059479 246263150359463 468212144364502 389353276591541 207814284476264 341801277159919 4270404442188...
result:
wrong answer 982nd lines differ - expected: '48303930061438', found: '77953499776789'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%