QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620051 | #4924. 蜘蛛爬树 | thomaswmy | 0 | 1219ms | 87372kb | C++14 | 3.9kb | 2024-10-07 16:27:44 | 2024-10-07 16:27:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int n,m,q;
int a[N];
pair<int,int> b[N];
vector<pair<int,ll> > g[N];
int f[N],dep[N],siz[N],son[N],dfn[N],num,rnk[N],top[N];
ll dis[N];
pair<pair<int,int>,pair<int,int> > ask[N];
ll ans[N];
void dfs1(int u,int fa) {
f[u]=fa;
dep[u]=dep[fa]+1;
siz[u]=1;
for(auto i:g[u]) {
int v=i.first;
ll w=i.second;
if(v==fa) continue;
dis[v]=dis[u]+w;
dfs1(v,u);
if(siz[v]>siz[son[u]]) son[u]=v;
siz[u]+=siz[v];
}
}
void dfs2(int u,int tp) {
dfn[u]=++num,rnk[num]=u;
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(auto i:g[u]) {
int v=i.first;
ll w=i.second;
if(v==f[u] || v==son[u]) continue;
dfs2(v,v);
}
}
int LCA(int u,int v) {
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) swap(u,v);
u=f[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
return u;
}
int Dis(int u,int v) {
return dis[u]+dis[v]-2*dis[LCA(u,v)];
}
struct Val{
vector<pair<int,ll> > arr;
int L;
void clear() {
arr.clear();
L=0;
}
void ins(int x,ll y) {
int sz=arr.size();
while(sz>1 && (__int128)(arr[sz-1].second-arr[sz-2].second)*(x-arr[sz-1].first)>=(__int128)(y-arr[sz-1].second)*(arr[sz-1].first-arr[sz-1].first)) arr.pop_back(),sz--;
arr.push_back({x,y});
}
ll query(int k) {
int sz=arr.size();
while(L<sz && arr[L+1].second-arr[L].second<=1ll*k*(arr[L+1].first-arr[L].first)) L++;
if(L>=sz) return 1e18;
return -1ll*k*arr[L].first+arr[L].second;
}
};
struct Node{
Val val;
#define lid(x) (x<<1)
#define rid(x) (x<<1|1)
#define val(x) t[x].val
}t[N<<2];
void build(int id,int L,int R) {
val(id).clear();
if(L==R) return ;
int mid=L+R>>1;
build(lid(id),L,mid),build(rid(id),mid+1,R);
}
void ins(int id,int L,int R,int pos,int x,ll y) {
val(id).ins(x,y);
if(L==R) return ;
int mid=L+R>>1;
if(mid>=pos) ins(lid(id),L,mid,pos,x,y);
else ins(rid(id),mid+1,R,pos,x,y);
}
ll query(int id,int L,int R,int l,int r,int k) {
if(L>=l && R<=r) return val(id).query(k);
int mid=L+R>>1;
ll res=1e18;
if(mid>=l) res=min(res,query(lid(id),L,mid,l,r,k));
if(mid+1<=r) res=min(res,query(rid(id),mid+1,R,l,r,k));
return res;
}
void ins1(int x,int k) {
int u=x;
while(u) {
ins(1,1,n,dfn[u],k,2*dis[x]-2*dis[u]);
u=f[top[u]];
}
}
void ins2(int x,int k) {
int u=x;
while(u) {
ins(1,1,n,dfn[u],k,2*dis[x]-4*dis[u]);
u=f[top[u]];
}
}
ll query1(int u,int v,int k) {
ll res=1e18;
while(top[u]!=top[v]) {
if(dep[top[u]]<dep[top[v]]) swap(u,v);
res=min(res,query(1,1,n,dfn[top[u]],dfn[u],-k));
u=f[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
res=min(res,query(1,1,n,dfn[u],dfn[v],-k));
return res;
}
ll query2(int u,int k) {
ll res=1e18;
while(u) {
res=min(res,query(1,1,n,dfn[top[u]],dfn[u],-k));
u=f[top[u]];
}
return res;
}
int main() {
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]={a[i],i};
for(int i=1;i<n;i++) {
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
g[u].push_back({v,w}),g[v].push_back({u,w});
}
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=q;i++) {
ll u,v;
scanf("%lld%lld",&u,&v);
int uu=u%n,vv=v%n;
if(!uu) uu=n;
if(!vv) vv=n;
ask[i]={{abs((u-uu)/n-(v-vv)/n),i},{uu,vv}};
ans[i]=1e18;
}
sort(b+1,b+1+n),sort(ask+1,ask+1+q);
build(1,1,n);
for(int i=1;i<=n;i++) ins1(b[i].second,b[i].first);
for(int i=1;i<=q;i++) ans[ask[i].first.second]=min(ans[ask[i].first.second],query1(ask[i].second.first,ask[i].second.second,ask[i].first.first));
build(1,1,n);
for(int i=1;i<=n;i++) ins2(b[i].second,b[i].first);
for(int i=1;i<=q;i++) {
int lca=LCA(ask[i].second.first,ask[i].second.second);
ans[ask[i].first.second]=min(ans[ask[i].first.second],2*dis[lca]+query2(lca,ask[i].first.first));
}
for(int i=1;i<=q;i++) ans[ask[i].first.second]+=Dis(ask[i].second.first,ask[i].second.second);
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: 4ms
memory: 41948kb
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:
7395406717 1766914590 4585719996 10937789871 17879775558 41945910320 9562438253 3489591698 2828930247 2807215092 269873114 4529043282 3092065780 8706560947 3774140834 11736869183 7419033879 7101222642 11851017223 1746147356 3120119049 6075216017 61710007339 7130501583 3962495602 844350611 -115962017...
result:
wrong answer 1st lines differ - expected: '6130845802041', found: '7395406717'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 1219ms
memory: 83560kb
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 281157754 379625081 365801170 562762310 740276417 97870732 450424067 301517280 968421193 430526428 858222593 338907476 368700872 220814346 596393510 187681195 583233306 1080812812 282848007 657338293 282642194 727965316 573783312 396849531 530967206 901864114 1031612062 778954090 691010101...
result:
wrong answer 2nd lines differ - expected: '270738856', found: '281157754'
Subtask #4:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 678ms
memory: 87372kb
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:
12303248855700 43366189394765 7397559880060 6011953672784 4307740126704 3915834261292 926124003192 8565382706455 3481315850301 12016020111022 1962361219549 7757311944281 13396509936894 2652987089313 27625027493322 1845593960904 13791039834074 9506038532648 4594255941993 676642730344 12325884589485 1...
result:
wrong answer 1st lines differ - expected: '29294995135992468', found: '12303248855700'
Subtask #5:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 610ms
memory: 77984kb
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:
595462044625 687125750769 225015968189 414219971253 214606162689 29794825387 223986974510 382873753744 185208838461 26898795933 312514611077 296790190717 169114130514 663767061215 6495223241 108663972955 195717909481 169325611430 658100413331 707254933642 339666290718 579069730085 576278076661 35346...
result:
wrong answer 1st lines differ - expected: '1352416884531', found: '595462044625'
Subtask #6:
score: 0
Wrong Answer
Test #43:
score: 0
Wrong Answer
time: 962ms
memory: 83444kb
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:
5902546122107 13935696912500 66559581298767 14841607575733 6596884562861 34484840625339 269707236167 2810945286141 19820739155323 1456932030613 11830661546202 3509447186928 43639148896426 67563016839795 7542542130134 212059704757 23755782600325 7678591334895 3475064454605 43641978262364 853761809035...
result:
wrong answer 1st lines differ - expected: '516260625003899', found: '5902546122107'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%