QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#645777 | #9416. Intersection of Paths | msk_sama | AC ✓ | 1963ms | 232192kb | C++14 | 3.9kb | 2024-10-16 19:48:42 | 2024-10-16 19:48:46 |
Judging History
answer
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <ctime>
#include <cstdio>
#include <random>
#include <vector>
#include <bitset>
#include <cassert>
#include <cstring>
#include <algorithm>
#define MISAKA main
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define _rep(i,a,b) for(int i=a;i>=b;--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
const int IN=1e8;
inline char _getch(){
static char buf[IN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,IN,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
char ch=_getch();int f=1,x=0;
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=_getch();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=_getch();}
return x*f;
}
const double eps=1e-9;
const int N=5e5+10,mod=998244353;
int n,m,st[N<<1][20],tag[N],F[N],lg[N],k=0,dep[N],dfn[N],id[N],pos[N],siz[N],vis[N],tim=0;ll ans[N],D[N];
struct Edge{int v,w;};vector<Edge> g[N];
struct edge{int u,v,w,id;}e[N];vector<edge> E[N];
struct node{int a,b,id;};vector<node> q[N];
void dfs(int u,int fa){
st[pos[u]=++k][0]=u;id[dfn[u]=++tim]=u;siz[u]=1;dep[u]=dep[fa]+1;F[u]=fa;
for(auto [v,w]:g[u])if(v!=fa) D[v]=D[u]+w,dfs(v,u),st[++k][0]=u,siz[u]+=siz[v];
}
int _min(int a,int b){return dep[a]<dep[b]?a:b;}
int lca(int x,int y){
x=pos[x],y=pos[y];if(x>y) swap(x,y);
int k=lg[y-x+1];return _min(st[x][k],st[y-(1<<k)+1][k]);
}
ll dis(int x,int y){return D[x]+D[y]-(D[lca(x,y)]<<1);}
struct tnode{int l,r,p[2];}t[N<<2];
tnode mg(tnode X,tnode Y){
if(!X.p[0]) return {X.l,Y.r,Y.p[0],Y.p[1]};
if(!Y.p[0]) return {X.l,Y.r,X.p[0],X.p[1]};
int nx=X.p[0],ny=X.p[1];
rep(i,0,1)rep(j,0,1){
int x=X.p[i],y=Y.p[j];
if(dis(x,y)>dis(nx,ny)) nx=x,ny=y;
}
int x=Y.p[0],y=Y.p[1];
if(dis(x,y)>dis(nx,ny)) nx=x,ny=y;
return {X.l,Y.r,nx,ny};
}
void pushup(int now){t[now]=mg(t[2*now],t[2*now+1]);}
void build(int now,int l,int r){
t[now].l=l,t[now].r=r;int mid=l+r>>1;
t[now].p[0]=t[now].p[1]=0;
if(t[now].l==t[now].r) return;
build(2*now,l,mid);build(2*now+1,mid+1,r);
}
void upd(int now,int x){
if(t[now].l==t[now].r){t[now].p[0]=t[now].p[1]=id[x];return;}
upd(2*now+(x>t[2*now].r),x);pushup(now);
}
tnode qry(int now,int l,int r){
if(t[now].l>r||t[now].r<l) return {0,0,0,0};
if(l<=t[now].l&&t[now].r<=r) return t[now];
return mg(qry(2*now,l,r),qry(2*now+1,l,r));
}
signed MISAKA(){
// freopen("data.in","r",stdin);
n=read(),m=read();lg[0]=-1;
rep(i,1,n-1){
int u=read(),v=read(),w=read();
g[u].push_back({v,w});
g[v].push_back({u,w});
e[i]={u,v,w,i};
}
dfs(1,0);
rep(i,1,n-1){
int &x=e[i].u,&y=e[i].v;
if(dep[x]>dep[y]) swap(x,y);
int k=min(siz[y],n-siz[y]);
E[k].push_back({x,y,e[i].w,i});
}
rep(i,1,k) lg[i]=lg[i>>1]+1;
rep(j,1,19)rep(i,1,k-(1<<j)+1)
st[i][j]=_min(st[i][j-1],st[i+(1<<j-1)][j-1]);
build(1,1,n);
rep(i,1,m){
int a=read(),b=read(),k=read();
q[k].push_back({a,b,i});
}
_rep(i,n/2,1){
for(auto [x,y,w,id]:E[i]){
vis[id]=1;
if(!tag[dfn[x]]) upd(1,dfn[x]),tag[dfn[x]]=1;
if(!tag[dfn[y]]) upd(1,dfn[y]),tag[dfn[y]]=1;
}
for(auto [a,b,id]:q[i]){
if(vis[a]){
int y=e[a].v,w=b-e[a].w;
tnode X=qry(1,dfn[y],dfn[y]+siz[y]-1);
tnode Y=mg(qry(1,1,dfn[y]-1),qry(1,dfn[y]+siz[y],n));
ans[id]=max(dis(X.p[0],X.p[1]),dis(Y.p[0],Y.p[1]));
rep(j,0,1)rep(k,0,1) ans[id]=max(ans[id],dis(X.p[j],Y.p[k])+w);
}
else ans[id]=dis(t[1].p[0],t[1].p[1]);
}
}
// printf("%d",clock());
rep(i,1,m) printf("%lld\n",ans[i]);
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 59184kb
input:
7 3 1 2 20 2 3 10 2 4 40 4 6 10 1 5 30 5 7 10 2 100 1 5 50 2 2 100 3
output:
160 110 20
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 3ms
memory: 61164kb
input:
200 500 124 178 427099307 158 192 319431399 117 104 710101310 194 96 839101283 136 4 101584313 105 185 76238080 84 121 653168782 143 136 831330689 147 53 258107910 183 161 822725527 171 165 701914427 127 83 753685257 94 167 437105834 41 173 974941718 33 54 850655642 140 32 414784060 40 24 166931598 ...
output:
4662267167 6250994729 4662267167 0 9861651445 0 0 2874455352 1871656394 1444557087 4662267167 1444557087 6250994729 5898042200 5898042200 0 1444557087 2155191170 1444557087 1444557087 649122524 1444557087 0 5715608407 649122524 0 1444557087 5715608407 0 2874455352 649122524 0 0 6585664630 5898042200...
result:
ok 500 lines
Test #3:
score: 0
Accepted
time: 3ms
memory: 59104kb
input:
200 500 112 128 943053618 196 10 109992136 92 19 372527046 193 51 8504187 111 176 997847081 64 7 511677289 87 64 59358634 92 121 204355409 33 122 426611600 19 28 382475107 113 187 928826711 68 64 914241911 133 40 791046827 8 193 140839139 155 38 35149576 166 82 350823876 139 151 293902264 148 41 921...
output:
8195272055 18038155199 18015634184 6114250165 10575951438 6114250165 13052879291 7036092739 0 13052879291 6077290338 2194906169 12123675487 0 3064424495 3934400794 19314790986 7036092739 0 8706949344 7036092739 11130725173 0 13052879291 0 12123675487 3934400794 3934006014 6553353146 6077290338 12123...
result:
ok 500 lines
Test #4:
score: 0
Accepted
time: 8ms
memory: 65316kb
input:
200 500 30 4 813691610 84 120 817637491 88 111 663721050 5 72 404108850 70 36 348963382 150 134 45804645 188 158 37935708 169 16 882040499 80 12 694412572 97 170 811712290 127 38 9501854 128 115 389754928 75 192 597486526 133 193 975891833 41 100 780943771 161 142 197395217 50 21 884175932 83 84 995...
output:
22172546870 52354768762 27703719679 20639364056 40927307908 14352624650 13315172638 14352624650 42670461106 39281433571 21716516085 50064335349 40927307908 45161769836 33491991214 45263858848 43550274975 26858825538 51968402130 24594720223 46510771787 25974649606 3468011441 55153680913 2278040758 54...
result:
ok 500 lines
Test #5:
score: 0
Accepted
time: 7ms
memory: 59212kb
input:
200 500 50 185 630130276 185 90 932042903 6 187 368883288 14 103 932774738 145 93 183033691 132 60 234965135 184 80 467923508 103 166 358970344 61 98 647414179 9 2 588228845 168 93 442821577 100 61 992122892 60 187 290721521 184 68 552742642 25 38 68998047 13 1 870165451 58 60 931574059 60 125 20953...
output:
0 0 785362711 0 0 0 0 0 0 0 0 1719788928 0 0 0 785362711 1719788928 0 0 0 1531836478 0 3536428150 0 0 0 0 1531836478 0 1583664077 0 0 0 1531836478 2714881481 785362711 0 0 0 0 0 1719788928 1719788928 1583664077 0 1873786210 0 0 0 0 785362711 0 0 0 1719788928 0 0 0 0 0 0 0 0 0 1531836478 1531836478 2...
result:
ok 500 lines
Test #6:
score: 0
Accepted
time: 12ms
memory: 59156kb
input:
200 500 5 27 388148671 5 119 680224137 173 87 986442294 66 147 118501544 22 147 676951375 161 5 727540296 93 5 232588510 147 88 385371846 196 147 314746005 35 178 19636528 98 5 242192853 195 178 544655136 55 5 271306482 5 155 942384947 56 178 303301289 181 5 671105997 147 153 54431483 5 142 49742903...
output:
0 0 0 194315768 0 0 201368555 0 0 194315768 0 0 194315768 0 194315768 0 0 0 395268412 0 0 194315768 3142868981 0 0 0 0 194315768 376961479 395268412 0 194315768 0 0 194315768 194315768 0 0 194315768 0 395268412 0 0 194315768 376961479 194315768 0 395268412 395268412 376961479 0 201368555 0 194315768...
result:
ok 500 lines
Test #7:
score: 0
Accepted
time: 1963ms
memory: 229488kb
input:
500000 500000 317156 54965 230759833 353491 77148 577942447 69059 132442 385326926 271915 94996 349962753 426805 265784 125714287 275095 49947 676489232 27293 430253 301562436 187863 475264 955971338 353263 433269 143567602 357681 310259 581877350 226404 39050 934609526 239119 204471 523754243 59587...
output:
1035108567 6886920194 393499496 9261470755 550314199 1035108567 1035108567 14330359099 550314199 393499496 0 1035108567 0 550314199 0 550314199 7341784214 393499496 550314199 550314199 393499496 550314199 550314199 0 1035108567 12626406441 14330359099 0 550314199 550314199 14330359099 1035108567 0 5...
result:
ok 500000 lines
Test #8:
score: 0
Accepted
time: 1797ms
memory: 228804kb
input:
500000 500000 101136 182341 255570946 45109 39454 824459295 292096 91801 196644175 300463 233417 961460180 5484 385014 198620547 464291 129845 942200533 99694 311784 122024892 27995 363914 559175166 200488 154575 773940047 92565 121726 574493648 180505 383816 397190682 231591 367594 936808806 4973 6...
output:
1720496473 0 16441040338 21036781141 0 14014394631 29189202442 8978191390 14014394631 29189202442 1720496473 14014394631 59812673829 7305842926 1720496473 1720496473 14014394631 10376295567 57941139695 25226868378 1720496473 14014394631 1720496473 7305842926 1720496473 8978191390 21036781141 3515557...
result:
ok 500000 lines
Test #9:
score: 0
Accepted
time: 1564ms
memory: 228780kb
input:
500000 500000 117422 56054 567560567 37609 51426 693318154 369948 25732 191341054 321431 469509 292868280 195218 150408 289330899 243114 32964 644463794 177181 3035 79162702 415066 204754 968798525 173772 440746 519973405 353210 75063 871376863 496610 208175 604393048 56122 126699 574969590 471302 3...
output:
19395057376 0 243758554869 0 19395057376 217129057419 0 86785348108 0 19395057376 0 0 406352878791 110510030058 326764779846 203656112567 28076461907 86785348108 301036711918 39759627385 0 0 86785348108 140469878703 86785348108 28076461907 28548372637 217129057419 19395057376 38312190750 21712905741...
result:
ok 500000 lines
Test #10:
score: 0
Accepted
time: 1854ms
memory: 230984kb
input:
500000 500000 385104 316086 203846482 90329 213814 14603218 119475 462619 752784726 498372 392323 865975281 304606 356710 129730417 242416 90188 373943907 292652 212607 63942753 111888 117722 855017284 362999 185574 323831658 302995 193341 143516276 370319 477011 115160893 212555 260622 552905885 40...
output:
961669517763 0 415448480082 125646612706 404921129645 0 378221674796 328765391722 262703284594 434520533651 415448480082 1168166453100 41888634064 114198363306 1064978063360 934717591514 729363217 221837076088 0 415448480082 305153161603 281434671538 2953480407378 630659988687 729363217 221837076088...
result:
ok 500000 lines
Test #11:
score: 0
Accepted
time: 1706ms
memory: 232028kb
input:
500000 500000 212178 335920 810458391 3968 383346 371060553 254524 168866 389037327 128669 470844 205547455 58828 217327 475404547 26846 200911 855903434 439798 233100 958501687 106285 324294 316767886 148013 368341 95627209 325855 279353 343917744 370568 273994 95266415 181067 395743 971354233 1930...
output:
1657697652188 588524410090 3986517524575 4292654087134 14745007507790 6859218245479 8699750506287 5877970905246 7621845358668 13425818817641 4291312458514 8886887696888 3970061425113 883771794077 2146471095474 6356949872854 9677944159886 601648300654 2818441761905 8473319373686 2772802043808 1320412...
result:
ok 500000 lines
Test #12:
score: 0
Accepted
time: 1869ms
memory: 232192kb
input:
500000 500000 31555 288134 394613692 355957 458485 375579843 126806 46407 545440829 406838 419632 549006915 172681 188725 236842242 105275 431054 900847846 478484 376936 229539493 433118 387062 650606705 23384 213517 58546600 254648 521 62347332 135409 21964 709872982 480882 107159 111319611 136690 ...
output:
248260194 248260194 248260194 765530529 765530529 0 248260194 248260194 0 0 248260194 1631260856 0 0 0 0 248260194 248260194 0 248260194 248260194 248260194 248260194 0 0 248260194 248260194 765530529 0 0 0 0 248260194 0 0 0 0 248260194 248260194 248260194 0 248260194 0 248260194 248260194 248260194...
result:
ok 500000 lines
Test #13:
score: 0
Accepted
time: 1369ms
memory: 232000kb
input:
500000 500000 401547 432681 418666883 374165 94786 721328975 389649 185309 178564131 444634 139701 493003972 34811 377124 601980813 42045 398192 535069177 67009 437437 668672486 495940 197831 745314009 255457 10319 552592132 152468 134332 101366144 112209 341038 945768202 67542 275586 477864247 8089...
output:
0 854433336 0 0 0 0 1869108742 0 0 1517850672 0 0 0 0 854433336 854433336 1844438150 0 854433336 0 0 0 1517850672 0 0 0 0 0 0 0 1517850672 0 0 0 0 854433336 0 1517850672 0 0 0 0 0 0 0 0 0 0 0 1517850672 0 0 0 854433336 0 854433336 0 854433336 0 0 1517850672 0 0 1517850672 0 1517850672 1517850672 0 0...
result:
ok 500000 lines
Test #14:
score: 0
Accepted
time: 1196ms
memory: 231948kb
input:
500000 500000 457852 271238 917738694 266814 474275 414331550 435124 77090 357330460 229795 188172 589247914 205253 173271 504388850 59713 483685 539037652 284974 376871 359402095 60301 499649 709358880 144507 224701 328191001 457309 40020 802196106 301404 356725 210765686 96399 290906 484238562 383...
output:
0 0 0 0 0 936571055 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 936571055 0 0 0 0 0 0 0 0 1511230822 0 0 0 0 936571055 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3654148537 0 0 0 936571055 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 936571055 0 0 0 0 1986401741 0 0 0 0 0 0 0 0 0...
result:
ok 500000 lines
Test #15:
score: 0
Accepted
time: 790ms
memory: 231268kb
input:
500000 500000 43888 48212 179626807 450799 64537 503182622 271313 327157 748716512 89686 209891 668339242 244235 178213 13339384 387281 297281 487239241 362793 154117 821483132 225770 172442 336980690 5589 184727 928795315 310841 366390 410466687 131748 359089 752402192 61148 236886 35578549 490278 ...
output:
1371380934 0 0 0 0 0 0 0 0 0 0 0 1371380934 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 794586902 0 0 0 0 1371380934 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1968089286 0 1436483857 0 0 1831337801 0 0 0 1905534453 0 0 0 794586902 0 0 0 0 0 0 0 0 0 0 0 0 794586902 0 0 0 0 794586902 0 0 0 0 0 0 0 0 0 0 0 0 0 1642806249 0 0 0 ...
result:
ok 500000 lines
Extra Test:
score: 0
Extra Test Passed