QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360470 | #6350. MIT | ppip | WA | 7ms | 53220kb | C++14 | 4.3kb | 2024-03-21 19:56:37 | 2024-03-21 19:56:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int LL
using LL=long long;
constexpr int N(1e6);
int n;
int fa[N+5],dep[N+5];
int L[N+5],R[N+5],fz[N+5];
bool les(int x,int y) { return dep[fz[x]]>dep[fz[y]]; }
int Max(int x,int y) { return les(y,x)?x:y; }
void chkMax(int &x,int y) { x=Max(x,y); }
struct RMQ {
int bs;
int st[20][N/20+5];
int bl[N+5],p[N+5];
int pre[N+5],suf[N+5];
int F[N+5],S[N+5];
void init() {
bs=log2(n)*1.5;
for (int i{1};i<=n;++i) {
bl[i]=(i-1)/bs+1;
if (bl[i-1]!=bl[i]) p[i]=0;
else p[i]=p[i-1]+1;
}
for (int i{1};i<=n;++i)
if (bl[i]!=bl[i-1]) st[0][bl[i]]=i;
else chkMax(st[0][bl[i]],i);
for (int i{1};i<=__lg(bl[n]);++i)
for (int j{1};j+(1<<i)-1<=bl[n];++j)
st[i][j]=Max(st[i-1][j],st[i-1][j+(1<<i-1)]);
for (int i{1};i<=n;++i) {
if (bl[i]!=bl[i-1]) pre[i]=i;
else pre[i]=Max(pre[i-1],i);
}
for (int i{n};i>=1;--i) {
if (bl[i]!=bl[i+1]) suf[i]=i;
else suf[i]=Max(suf[i+1],i);
}
for (int i{1};i<=n;++i) {
if (bl[i]!=bl[i-1]) *S=0;
else F[i]=F[i-1];
while (*S>0&&les(S[*S],i)) F[i]^=1<<p[S[(*S)--]];
S[++*S]=i;F[i]|=(1<<p[i]);
}
}
int ask(int l,int r) {
int L{bl[l]},R{bl[r]};
if (L!=R) {
int ans{Max(suf[l],pre[r])};
if (L-R>1) {
int z{__lg(R-L-1)};
ans=Max(ans,Max(st[z][L+1],st[z][R-(1<<z)]));
}
return ans;
} else {
return l+__builtin_ctz(F[r]>>p[l]);
}
}
} A;
// tree basic op start
int LCA(int u,int v) {
if (u==v) return u;
u=L[u];v=L[v];
if (u>v) swap(u,v);
return fa[fz[A.ask(u+1,v)]];
}
LL d[N+5];
vector<pair<int,int>> e[N+5];
int lb[N+5];
LL dis(int u,int v) { return d[u]+d[v]-2*d[LCA(u,v)]; }
void init(int u,int fa) {
::fa[u]=fa;
dep[u]=dep[fa]+1;
static int lda{0};
L[u]=++lda;
fz[lda]=u;
int p{fa},q{lb[fa]},r{lb[q]};
lb[u]=(dep[lb[p]]-dep[lb[q]]!=dep[lb[q]]-dep[lb[r]]?p:r);
for (auto [v,w]:e[u])
if (v!=fa) {
d[v]=d[u]+w;
init(v,u);
}
R[u]=lda;
}
int jmp(int u,int k) {
k=dep[u]-k;
while (dep[u]>k) {
if (dep[lb[u]]<k) u=fa[u];
else u=lb[u];
}
return u;
}
bool son(int u,int v) { return L[u]<=L[v]&&L[v]<=R[u]; }
int jump(int a,int b) { return son(a,b)?jmp(b,dep[b]-dep[a]-1):fa[a]; }
// tree basic op end
struct fenwick {
int t[N+5];
void add(int x) {
for (;x<=n;x+=x&-x) ++t[x];
}
int qry(int u) {
int l{L[u]-1},r{R[u]},ans{0};
while (r>l) ans+=t[r],r&=r-1;
while (l>r) ans-=t[l],l&=l-1;
return ans;
}
} T;
struct centroid {
int cnt;
set<int> dots;
int getwt(int v) { return son(cnt,v)?T.qry(jump(cnt,v)):dots.size()-T.qry(cnt); }
int nxt(int p) {
auto it{dots.lower_bound(p)};
if (it==dots.end()) return n+1;
return *it;
}
int pre(int p) {
auto it{dots.upper_bound(p)};
if (it==dots.begin()) return 0;
return *prev(it);
}
int move(int v) {
if (son(cnt,v)) {
int a{jump(cnt,v)};
return LCA(fz[nxt(L[a])],fz[pre(R[a])]);
}
int l{L[cnt]},r{R[cnt]};
int x{v};
for (auto y:{nxt(1),pre(l-1),nxt(r+1),pre(n)}) {
if (y==0||y==n+1||l<=y&&y<=r) continue;
x=LCA(x,fz[y])^LCA(x,cnt)^LCA(fz[y],cnt);
}
return x;
}
void add(int v) {
dots.insert(L[v]);
T.add(L[v]);
if (v==cnt) return;
int wt{getwt(v)};
if (2*wt<=dots.size()) return;
int to{move(v)};
cnt=to;
}
} C;
using P=array<int,2>;
P operator+(const P &a,const P &b) {
if (a[0]==-1) return b;
if (b[0]==-1) return a;
auto [x,y]=a;
LL xy{dis(x,y)};
for (auto z:b) {
LL xz{dis(x,z)},yz{dis(y,z)},mx{max({xy,xz,yz})};
if (mx==xz) y=z,xy=xz;
else if (mx==yz) x=z,xy=yz;
}
return {x,y};
}
struct segtree {
P t[N<<1];
void build() {
for (int i{1};i<=n;++i) t[i+n-1]={i,i};
for (int i{n-1};i>=1;--i)
t[i]=t[i<<1]+t[i<<1|1];
}
void mod(int x) {
t[x+=n-1]={-1,-1};
while (x>>=1) t[x]=t[x<<1]+t[x<<1|1];
}
} S;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
for (int i{1};i<n;++i) {
int u,v,w;cin>>u>>v>>w;
e[u].emplace_back(v,w);
e[v].emplace_back(u,w);
}
init(1,0);
A.init();C.cnt=1;S.build();
LL ans{0};
for (int i{1};i<=n;++i) {
int cnt{C.cnt};
auto [a,b]{S.t[1]};
LL xa{dis(a,cnt)},xb{dis(b,cnt)};
if (xa<xb) a=b,xa=xb;
S.mod(a);C.add(a);
ans+=xa-dis(cnt,C.cnt);
if (~i&1) cout<<ans<<" ";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 52912kb
input:
7 1 3 99 2 3 82 3 4 4 4 5 43 5 6 5 4 7 3
output:
181 280 287
result:
ok 3 number(s): "181 280 287"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 53220kb
input:
1000 328 12 58771429 881 504 17030494 847 622 20805817 12 922 58532669 879 929 52585738 495 726 27873950 855 17 37971674 797 941 76999719 702 610 719916 479 127 97829887 731 557 55300256 282 615 88802280 651 318 64099880 540 51 6547869 896 54 87823596 674 879 80674008 77 691 54802376 193 851 7869172...
output:
48829042195 97562386542 146216474947 194713456438 243120633399 291394542517 339657517459 387785045812 435787622310 460963936244 486050162271 511081791686 536091000631 560991786981 585798068624 610548785478 659324794092 707792055072 755960913332 803915134162 851752548442 899402515545 946912573224 994...
result:
wrong answer 10th numbers differ - expected: '483691355869', found: '460963936244'