QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#593585 | #3226. Distance Sum | dongyc666 | WA | 388ms | 75044kb | C++14 | 2.5kb | 2024-09-27 14:49:19 | 2024-09-27 14:49:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NR=2e5+10;
int n,dis[NR],fa[NR][20],dep[NR];
int L[NR],R[NR],dfn[NR],siz[NR],idx[NR],S;
#define pii pair<int,int>
#define fi first
#define se second
#define mkp make_pair
#define pb emplace_back
vector<pii>son[NR];
set<int>s;
void dfs(int id,int fath){
dep[id]=dep[fath]+1;
fa[id][0]=fath;siz[id]=1;
dfn[id]=++S;idx[S]=id;
L[id]=dfn[id];
for(int i=1;i<=18;i++)fa[id][i]=fa[fa[id][i-1]][i-1];
for(auto lcy:son[id]){
dis[lcy.fi]=dis[id]+lcy.se;
dfs(lcy.fi,id);
siz[id]+=siz[lcy.fi];
}
R[id]=dfn[id]+siz[id]-1;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=18;i>=0;i--)
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return x;
for(int i=18;i>=0;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int getd(int x,int y){
return dis[x]+dis[y]-2*dis[LCA(x,y)];
}
int jump(int x,int y){
for(int i=18;i>=0;i--)
if((y>>i)&1)x=fa[x][i];
return x;
}
struct BIT{
int c[NR];
int lowbit(int x){
return x&(-x);
}
void add(int x,int y){
while(x<=n){
c[x]+=y;
x+=lowbit(x);
}
}
int ask(int x){
int res=0;
while(x){
res+=c[x];
x-=lowbit(x);
}
return res;
}
int calc(int l,int r){return ask(r)-ask(l-1);}
}T;
void Insert(int x){
if(s.count(dfn[x]))return;
auto it=s.lower_bound(dfn[x]);
if(it==s.end()){
s.insert(dfn[x]);
return;
}
int tmp=LCA(idx[*it],x);
s.insert(dfn[tmp]);
s.insert(dfn[x]);
}
int getfa(int x){
auto it=s.lower_bound(dfn[x]);
if(it==s.begin())return 0;
it--;
return LCA(idx[*it],x);
}
signed main(){
cin>>n;
for(int i=2,x,y;i<=n;i++)
cin>>x>>y,son[x].pb(mkp(i,y));
dfs(1,0);
int res=0,rt=0;
for(int i=1;i<=n;i++){
if(!rt)rt=i;
res+=getd(rt,i);
Insert(i);T.add(dfn[i],1);
if(L[rt]<=L[i]&&R[i]<=R[rt]&&i!=rt){
int tmp=jump(i,dep[i]-dep[rt]-1);
auto it1=s.lower_bound(L[tmp]),it2=s.upper_bound(R[tmp]);
it2--;
int tar=LCA(idx[*it1],idx[*it2]),val=T.calc(L[tar],R[tar]);
// printf("i:%lld tmp:%lld %lld %lld tar:%lld val:%lld\n",i,tmp,idx[*it1],idx[*it2],tar,val);
if(val*2>i){
res-=(val*2-i)*(dis[tar]-dis[rt]);
rt=tar;
}
}
else{
int val=T.calc(L[rt],R[rt]);
if(val*2<i){
int nxt=getfa(rt);
res-=(i-val*2)*(dis[rt]-dis[nxt]);
rt=nxt;
}
}
// printf("i:%lld rt:%lld\n",i,rt);
// for(int x:s)cout<<idx[x]<<' ';cout<<endl;
cout<<res<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 17988kb
input:
10 5 1 2 1 1 1 4 1 2 1 5 1 2 1 2 1 5 1
output:
0 3 4 6 6 8 9 11 12 14
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 18056kb
input:
10 5 1 10 1 5 1 3 1 5 1 5 1 1 1 3 1 1 1
output:
0 4 4 6 6 7 8 12 14 16
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 2ms
memory: 17896kb
input:
10 4 1 7 1 9 1 3 1 7 1 1 1 7 1 6 1 3 1
output:
0 5 6 9 11 12 12 13 15 17
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 15940kb
input:
10 1 1 9 1 9 1 6 1 9 1 3 1 10 1 1 1 3 1
output:
0 1 3 5 7 8 10 13 13 15
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 2ms
memory: 16116kb
input:
10 6 1 6 1 3 1 3 1 1 1 1 1 9 1 3 1 1 1
output:
0 2 3 5 6 7 9 12 13 16
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 2ms
memory: 15884kb
input:
10 8 1 10 1 6 1 3 1 1 1 1 1 4 1 5 1 4 1
output:
0 4 6 6 9 10 13 14 18 19
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 0ms
memory: 17984kb
input:
10 5 1 8 1 9 1 1 1 1 1 5 1 5 1 1 1 1 1
output:
0 2 4 7 7 9 10 11 13 15
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 2ms
memory: 17988kb
input:
10 5 1 5 1 8 1 4 1 5 1 4 1 1 1 2 1 7 1
output:
0 4 5 6 6 7 9 11 13 16
result:
ok 10 lines
Test #9:
score: 0
Accepted
time: 0ms
memory: 18000kb
input:
10 4 1 1 1 3 1 3 1 5 1 6 1 6 1 8 1 4 1
output:
0 3 3 4 5 7 10 13 16 19
result:
ok 10 lines
Test #10:
score: 0
Accepted
time: 2ms
memory: 16336kb
input:
10 8 1 10 1 8 1 4 1 9 1 10 1 1 1 8 1 8 1
output:
0 2 4 5 7 9 11 11 12 13
result:
ok 10 lines
Test #11:
score: 0
Accepted
time: 313ms
memory: 66516kb
input:
200000 109891 65231 171839 5776 32431 29819 62570 71905 153470 68881 188361 76298 151469 77636 75162 130242 95864 47113 191182 742 121927 111781 18165 51034 158645 36001 193496 189958 143195 29723 140274 86466 117583 23287 184465 144332 35935 128306 192514 116854 27679 197718 138926 123165 46773 177...
output:
0 2128476 3615067 4333135 5793589 7104606 7876598 9621410 10959705 12443304 12989537 15017823 15660417 16551255 18007974 18487621 20027188 21203385 22454974 22931660 23796204 25173856 26195907 27604778 28662852 29446260 30856590 32645490 33338651 34979442 36020161 37417274 38263592 39224218 40460394...
result:
ok 200000 lines
Test #12:
score: 0
Accepted
time: 388ms
memory: 75044kb
input:
200000 196697 155143 178488 159177 49016 18473 171950 94863 69271 100194 160889 20733 40420 19766 191138 127108 142060 2610 194472 41608 6942 105158 61037 3025 150904 197834 58272 30296 5859 116513 104509 1986 48140 173435 131223 123177 175812 40180 28608 98965 157005 67994 127784 114441 17436 68620...
output:
0 11320917384 11874614449 15772614655 18523775058 21957641652 30009559722 31970095838 35324661901 38253652284 48493111245 51933582990 58121449414 67628507769 73905238960 83828871781 83828871781 86351127383 86906418351 88386492876 89035727731 94945127602 103238157194 103565667357 106031510890 1148471...
result:
ok 200000 lines
Test #13:
score: 0
Accepted
time: 216ms
memory: 64080kb
input:
200000 100271 58998 100271 15879 100271 138986 100271 113904 100271 115478 100271 83571 100271 27243 100271 160493 100271 9252 100271 48723 100271 192210 100271 94228 100271 34372 100271 164043 100271 117906 100271 65950 100271 3526 100271 27894 100271 100392 100271 125787 100271 41701 100271 73898 ...
output:
0 184014 199893 338879 452783 568261 651832 679075 839568 848820 897543 1089753 1183981 1218353 1382396 1500302 1566252 1569778 1597672 1698064 1823851 1865552 1939450 1985877 2053259 2186511 2309128 2336836 2417401 2514921 2533909 2641402 2645106 2837716 2867548 3041880 3119887 3270987 3470933 3530...
result:
ok 200000 lines
Test #14:
score: -100
Wrong Answer
time: 331ms
memory: 66740kb
input:
200000 74397 4664 156428 79828 33782 165515 92020 132016 88427 42599 80143 31404 154122 97504 38283 40204 163610 54024 103901 41100 56105 102790 713 129668 123254 101263 66304 117240 193266 30852 106010 58682 58420 44200 195244 150716 69688 108732 66176 90998 186655 161913 46056 19248 55107 2538 117...
output:
0 9321877 61989505 120141123 169103327 213727540 253084679 305259586 324006109 346052256 356850684 398366900 426326820 463012059 500894672 510409428 533431749 582755784 617910987 676094992 691040861 739632515 788623478 824330028 863333091 908581077 940061483 958059386 990954065 1042453000 1078844771...
result:
wrong answer 485th lines differ - expected: '16862822611', found: '16861427733'