QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#152665 | #4924. 蜘蛛爬树 | zhouhuanyi | 0 | 3603ms | 264236kb | C++14 | 6.4kb | 2023-08-28 16:35:51 | 2023-08-28 16:35:52 |
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,sw;
long long ans[N+1],dis[N+1][M+1];
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,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)
{
maxn=max(maxn,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};
}
long long operator * (points a,points b)
{
return 1ll*a.x*b.y-1ll*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 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];
for (int j=depth[l];j>=2;--j)
{
if (ls[fa[sx]]==sx)
{
d=depth[fa[sx]],tx=rs[fa[sx]];
if (d==w&&!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][j-1]);
}
}
else
{
d=depth[fa[sx]],tx=ls[fa[sx]];
if (d==w&&!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][j-1]);
}
}
sx=fa[sx];
}
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 (d==w&&!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 (d==w&&!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 (d==w&&!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 (d==w&&!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: 7ms
memory: 87268kb
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:
199495966265 2572907432462 884891791240 1262113544160 12422940018 6925745930 1667410289331 677799457170 1083946744713 904139033702 897934911835 2780725714938 1232688561266 622161887405 -520826462 7952398130 3466532462182 1935575686700 21636556933 1629348457307 221191207011 2751303950551 181282921199...
result:
wrong answer 1st lines differ - expected: '6130845802041', found: '199495966265'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 11
Accepted
time: 3573ms
memory: 215072kb
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:
ok 200000 lines
Test #21:
score: 0
Accepted
time: 3378ms
memory: 221692kb
input:
199998 20 199928 841581743 193826897 19260647 316900759 938030012 734551083 200340391 232139411 654311599 1143318 596086442 603556286 904977745 575551276 670573487 214312499 155571640 318139630 664877075 921888211 314261245 840096855 656620366 784431866 158438090 761901044 794827280 603867695 489777...
output:
623283525 8593864781 7874704109 155914357 3646556950 3740880356 3717912008 12901066587 1524759519 4985719750 2248493864 5114948482 3925676469 579421045 1507306567 2095047126 606785057 146334438 4045519468 8910005611 4581660381 4073333567 3935919804 676004871 2344714675 5021627074 5038533943 15002805...
result:
ok 199928 lines
Test #22:
score: 0
Accepted
time: 3343ms
memory: 226284kb
input:
200000 20 200000 986369289 907363922 363774806 860858089 709715562 958810333 925993952 387795500 17150414 148015078 97834597 563293239 667378418 806659943 610215443 524417320 750481911 623874575 259982271 991286339 284729472 528334897 723997495 992805109 87608435 211268145 108070673 872622387 564643...
output:
10054223674 507380699 3217502558 539269 6315538 1928038308 1387049352 9511170763 5162637476 42235828 121118538 4623727820 10063019655 3205838882 4165257188 7261364 8095368917 8506365 5447619 2143078432 7359894792 10046658768 5435463 9232067090 6457897508 9629262 6399644 10022553858 6118703 8574986 1...
result:
ok 200000 lines
Test #23:
score: -11
Wrong Answer
time: 3603ms
memory: 219528kb
input:
200000 20 200000 747319812 390314311 314857121 211725609 263470750 979001169 50249481 658501549 959367948 893597081 177095123 795316289 460892240 678153548 117315462 524637159 259809686 647733605 266923024 115001443 857165610 939992604 661082449 538356122 31381162 71199751 506277142 981277756 241363...
output:
13662467103 15058589930 16458374764 15068421502 15918461311 13609092499 11519562416 16879299235 12561438735 13516222673 13414108442 18034454674 13386369283 17622328679 18631538755 15324696769 16688583373 14449065100 9460517704 13363610940 13733806309 18274703051 15926306728 14858932320 16642420472 1...
result:
wrong answer 5th lines differ - expected: '15811563571', found: '15918461311'
Subtask #4:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 3098ms
memory: 205164kb
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:
5844294929306 4497292014599 4219264649612 3690060264456 2416661607150 2252793577274 634066227064 3982646722649 3691769247805 5280323179754 1339227056829 3875173267741 5770956417866 2751771337121 4327004142166 1815529189832 5592270247053 4578979883630 1475227113977 1196333773160 5750020249351 1487117...
result:
wrong answer 1st lines differ - expected: '29294995135992468', found: '5844294929306'
Subtask #5:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 3579ms
memory: 264236kb
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 869269749781 715671043328 876194052054 1358007874327 127994985855 1230162209719 1532026808855 611656467332 1023855959729 414792924571 1316679734677 827308370883 1265411315424 821484360433 1051517948640 837509712760 582943943131 457...
result:
wrong answer 225th lines differ - expected: '1234710074817', found: '1234510663957'
Subtask #6:
score: 0
Wrong Answer
Test #43:
score: 0
Wrong Answer
time: 3227ms
memory: 220768kb
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:
199424907025002 142309822437366 76520853630397 31325348026325 128043460143494 81312663286262 75459126507794 161092110441992 48466549445095 119901697938219 92186631359674 140347105237488 86445872129175 106470554809255 185347849141752 150431475296355 83084059843881 138427058680683 159735261204791 1073...
result:
wrong answer 1st lines differ - expected: '516260625003899', found: '199424907025002'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%