QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#692718 | #5439. Meet in the Middle | LinkWish | WA | 45ms | 32360kb | C++14 | 4.4kb | 2024-10-31 14:57:09 | 2024-10-31 14:57:10 |
Judging History
answer
//Linkwish's code
#include<bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define si inline
using namespace std;
typedef long long ll;typedef __int128 li;typedef long double ld;
typedef unsigned long long ull;typedef pair<int,int> pii;typedef pair<ll,ll> pll;
typedef const int ci;typedef const ll cl;ci iinf=INT_MAX;cl linf=LLONG_MAX;
template<typename T>si bool gmax(T &x,const T y){if(x<y)return x=y,true;return false;}
template<typename T>si bool gmin(T &x,const T y){if(y<x)return x=y,true;return false;}
namespace LinkWish{
char buf[1<<16],*bp1,*bp2;
si void Init(){if(bp1==bp2)bp2=(bp1=buf)+fread(buf,1,1<<15,stdin);}
si char Texas(){return (Init(),bp1!=bp2)?*bp1++:EOF;}
si void read(int &x){
char ch=Texas();for(;ch<'0'||ch>'9';ch=Texas());
for(x=0;ch>='0'&&ch<='9';ch=Texas())x=x*10+(ch^48);
}
char wrbuf[1<<26],*cur=wrbuf;
char stk[60];int top;
si void write(ll x){
if(x==0)return *(cur++)='0',*(cur++)='\n',void();
for(;x;x/=10)stk[++top]=x%10;
for(;top;top--)*(cur++)=stk[top]+'0';
*(cur++)='\n';
}
ci N=100005,M=500005;
int n,m;
ll ans[M];
vector<pii> que[N];
int col[N];
struct Tree2{
vector<pii> e[N];
int dep[N],dfn[N],sign;
ll dis[N];
pii pf[17][N];
si void add(int x,int y,int w){
e[x].emplace_back(y,w);
e[y].emplace_back(x,w);
}
void dfs(int x,int fa){
pf[0][dfn[x]=++sign]=pii(dep[x]=dep[fa]+1,fa);
for(pii i:e[x])if(i.fi!=fa)dis[i.fi]=dis[x]+i.se,dfs(i.fi,x);
}
si void init(){
dfs(1,0);
for(int i=1;i<17;i++)
for(int j=1;j+(1<<i)-1<=n;j++)
pf[i][j]=min(pf[i-1][j],pf[i-1][j+(1<<(i-1))]);
}
si int lca(int x,int y){
if(x==y)return x;
if((x=dfn[x])>(y=dfn[y]))swap(x,y);
int k=__lg(y-(x++));
return min(pf[k][x],pf[k][y-(1<<k)+1]).se;
}
si ll Dis(int x,int y){return dis[x]+dis[y]-2*dis[lca(x,y)];}
si void Clear(){
sign=0;
for(int i=1;i<=n;i++)e[i].clear();
}
}T2;
vector<pii> e[N];
si void add(int x,int y,int w){
e[x].emplace_back(y,w);
e[y].emplace_back(x,w);
}
int sz[N];
ll dis[N];
bool vis[N];
void getrt(int x,int fa,ci S,int &rt,vector<int> &all){
int mx=0;sz[x]=1;
all.push_back(x);
for(pii i:e[x])
if(!vis[i.fi]&&i.fi!=fa)
getrt(i.fi,x,S,rt,all),sz[x]+=sz[i.fi],gmax(mx,sz[i.fi]);
if(max(mx,S-sz[x])<=S/2)rt=x;
}
void getinfo(int x,int fa,ci c){
col[x]=c,sz[x]=1;
for(pii i:e[x])
if(!vis[i.fi]&&i.fi!=fa)
dis[i.fi]=dis[x]+i.se,getinfo(i.fi,x,c),sz[x]+=sz[i.fi];
}
void solve(int x,int S){
if(S==1){
for(pii i:que[x])gmax(ans[i.se],T2.Dis(x,i.fi));
return ;
}
vector<int> cur;
getrt(x,0,S,x,cur);
dis[x]=0,col[x]=x;
for(pii i:e[x])
if(!vis[i.fi])
dis[i.fi]=i.se,getinfo(i.fi,x,i.fi);
int d1=0,d2=0,d3=0,d4=0,d5=0,d6=0;ll nv=-linf;
for(int i:cur)if(gmax(nv,dis[i]+T2.dis[i]))d1=i;
nv=-linf;for(int i:cur)if(col[i]!=col[d1]&&gmax(nv,dis[i]+T2.Dis(d1,i)))d2=i;
nv=-linf;for(int i:cur)if(col[i]!=col[d1]&&col[i]!=col[d2]&&gmax(nv,dis[i]+T2.Dis(d1,i)))d3=i;
nv=-linf;for(int i:cur)if(col[i]!=col[d1]&&gmax(nv,dis[i]+T2.dis[i]))d4=i;
nv=-linf;for(int i:cur)if(col[i]!=col[d4]&&gmax(nv,dis[i]+T2.Dis(d4,i)))d5=i;
nv=-linf;for(int i:cur)if(col[i]!=col[d4]&&col[i]!=col[d5]&&gmax(nv,dis[i]+T2.Dis(d4,i)))d6=i;
vector<int> cd={d1,d2,d3,d4,d5,d6};
sort(cd.begin(),cd.end()),cd.erase(unique(cd.begin(),cd.end()),cd.end());
for(int i:cur){
for(pii j:que[i]){
ll v=0;
for(int k:cd)
if(col[k]!=col[i])
gmax(v,dis[k]+T2.Dis(j.fi,k));
gmax(ans[j.se],dis[i]+v);
}
}
vis[x]=true;
for(pii i:e[x])if(!vis[i.fi])solve(i.fi,sz[i.fi]);
}
si void Clear(){
for(int i=1;i<=n;i++)e[i].clear(),vis[i]=false;
T2.Clear();
for(int i=1;i<=n;i++)que[i].clear();
for(int i=1;i<=m;i++)ans[i]=0;
}
si void solve(){
read(n),read(m);
for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),add(x,y,z);
for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),T2.add(x,y,z);
for(int i=1,x,y;i<=m;i++)read(x),read(y),que[x].emplace_back(y,i);
T2.init();
solve(1,n);
for(int i=1;i<=m;i++)write(ans[i]);
Clear();
}
void mian(){
solve();
fwrite(wrbuf,1,cur-wrbuf,stdout);
}
}
signed main(){
#ifndef ONLINE_JUDGE
assert(freopen("move.in","r",stdin));
assert(freopen("move.out","w",stdout));
#endif
LinkWish::mian();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 28428kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 28232kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 0ms
memory: 30180kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 45ms
memory: 32208kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
647838384844 626539793176 514273941704 637066393138 472546379596 645842915960 641537859258 573604504956 644081575470 803875451466 674370549986 734764046916 744862815441 763778393516 553499885160 526743824759 610373719514 689550247042 549161302822 726811438160 653134244120 666761991962 701575393972 6...
result:
ok 50000 numbers
Test #5:
score: 0
Accepted
time: 43ms
memory: 30500kb
input:
10000 50000 5314 8843 137901358 5314 4153 459134340 5314 8667 933926892 4153 6504 330487798 4153 8880 750362377 4153 5990 874275912 4153 546 563436331 5990 6216 902348875 8843 3101 669215553 6216 8138 732343176 8667 8675 581114294 6504 7416 127778711 546 4239 282695908 6504 9455 549237168 5314 8340 ...
output:
464564968641 331633000004 565299667784 484694871646 570451097836 417492802442 372302349684 638725688107 386235986078 355738655551 462027769535 558485994764 524714144289 450157947013 432701214095 494566741391 529031758638 637683369382 415646847933 344894296260 390294136162 527685175763 575151290175 3...
result:
ok 50000 numbers
Test #6:
score: 0
Accepted
time: 40ms
memory: 32240kb
input:
10000 50000 2808 2490 757309564 2808 9601 312271047 2808 4046 119325897 2808 4894 466822371 4894 1507 498399554 2490 5982 84088145 9601 1251 149019541 2808 6681 416590999 2808 6583 357757899 1251 3192 889947539 6583 9762 350572496 6681 22 597479070 5982 8744 263208242 8744 5281 49894126 1507 8806 30...
output:
1501072697023 2058806276380 2017086500812 2044250452467 1543567245539 1695101693278 1765462307870 2576423082091 2302805133490 2090282734929 2375783476943 1954788661090 2056530503168 2453153202726 1978028047409 2106220371212 2210163378358 2015714406862 1555876274751 2122832986951 2102262624814 169085...
result:
ok 50000 numbers
Test #7:
score: 0
Accepted
time: 30ms
memory: 32360kb
input:
10000 50000 4064 7188 81750473 7188 8466 310631946 8466 2276 154981798 2276 7347 162965456 7188 464 806245243 464 2250 849189978 8466 641 734602751 8466 9246 225800419 4064 5267 191524437 2276 5292 192776095 2276 9036 414997994 9246 5470 362146726 2250 473 98496385 4064 7726 700294189 473 9503 42824...
output:
3589143478793 5241855728342 3397106617685 3432843859461 4544481241003 3649934075137 3020107625030 3297847713344 3894730366667 3030559097282 4824131552194 4821302024170 4471510161493 3291683748595 4954639576578 2961243269520 3659899432127 3421183608349 5262802614761 4408705330639 5203984107670 500158...
result:
ok 50000 numbers
Test #8:
score: -100
Wrong Answer
time: 37ms
memory: 32280kb
input:
10000 50000 8676 4714 406191383 8676 5040 603960140 5040 9715 635348098 4714 9483 594267326 9483 5451 409058229 8676 8913 909259106 9715 1399 320185961 9715 4857 180234031 4714 8888 585099487 5040 1244 645347755 5451 7736 479423492 9483 1038 272038574 1399 3970 638817231 8888 3314 55726955 8888 2295...
output:
447424387353 491327570749 614052040822 384218910068 429859933145 356174725430 609432604118 465420084327 472632020898 382647454960 343751681021 441874503695 463199624732 610943875286 563031986601 566780763247 346991783125 601234775562 619765985074 357316826763 495874578271 526431260851 331681020073 4...
result:
wrong answer 550th numbers differ - expected: '535172937869', found: '533899228664'