QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360763 | #6350. MIT | ppip | WA | 3ms | 44664kb | C++14 | 4.3kb | 2024-03-22 07:55:38 | 2024-03-22 07:55:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int S1=1<<20;
char buf1[S1],*l1,*r1;
#define getchar() ((l1==r1&&(r1=(l1=buf1)+fread(buf1,1,S1,stdin)),l1!=r1)?*l1++:EOF)
template<typename T=int>inline T read()
{
T x=0;
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x;
}
constexpr int S2=1<<20;
char buf2[S2],*l2=buf2;
#define putchar(c) (l2==buf2+S2&&(fwrite(buf2,1,S2,stdout),l2=buf2),*l2++=(c))
int _st[22];
template<typename T>inline void write(T x)
{
int tp=0;
do
_st[++tp]=x%10;
while(x/=10);
while(tp)
putchar(_st[tp--]+'0');
putchar(' ');
}
using LL=long long;
constexpr int N(1e6);
int n;
int fa[N+5],dep[N+5],DEP[N+5],FA[N+5];
int L[N+5],R[N+5],fz[N+5];
vector<pair<int,int>> e[N+5];
int lb[N+5];
LL d[N+5];
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 LCA(int u, int v) {
if (dep[u]<dep[v]) swap(u, v);
while (dep[u]>dep[v])
if (dep[lb[u]]<dep[v])
u=fa[u];
else
u=lb[u];
while (u!=v)
if (lb[u]==lb[v])
u=fa[u],v=fa[v];
else
u=lb[u],v=lb[v];
return u;
}
inline LL dis(int u,int v) { return d[u]+d[v]-2*d[LCA(u,v)]; }
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]; }
struct fenwick {
int t[N+5];
void init() {
for (int i{1};i<=n;++i) {
t[i]=1;
for (int j{1};j<(i&-i);j<<=1)
t[i]+=t[i-j];
}
}
void add(int x) {
for (;x<=n;x+=x&-x) --t[x];
}
int qry(int u) {
if (!u) return n;
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;
}
int kth(int k) {
int x{0};
for (int i{19};i>=0;--i)
if ((x|1<<i)<=n&&t[x|1<<i]<k)
k-=t[x|=1<<i];
return fz[x+1];
}
} T;
const int ts=262144;
LL lz[ts],mn[ts];
int mp[ts];
vector<int> curz;
LL ans;
int jump(int u,int sz) {
while (T.qry(u)<sz) {
if (T.qry(lb[u])>=sz) u=fa[u];
else u=lb[u];
}
return u;
}
void build(int id,int l,int r){
if(l==r){
int x=fz[l];
LL frog=dis(curz[0],x);
ans+=frog;
if(curz.size()==2) frog=max(frog,dis(curz[1],x));
lz[id]=mn[id]=frog;
mp[id]=l;
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
mn[id]=min(mn[id*2],mn[id*2+1]);
if(mn[id]==mn[id*2]) mp[id]=mp[id*2];
else mp[id]=mp[id*2+1];
}
void add(int id,int l,int r,int ql,int qr,LL v){
if(l>qr || r<ql) return;
if(ql<=l && r<=qr){
lz[id]+=v;mn[id]+=v;
return;
}
int mid=(l+r)/2;
add(id*2,l,mid,ql,qr,v);
add(id*2+1,mid+1,r,ql,qr,v);
mn[id]=min(mn[id*2],mn[id*2+1]);
if(mn[id]==mn[id*2]) mp[id]=mp[id*2];
else mp[id]=mp[id*2+1];
mn[id]+=lz[id];
}
vector<int>findz(int k){
int mid=T.kth(k/2+1);
int z1=jump(mid,(k+1)/2);
int s1=T.qry(z1);
if(k%2==1) return {z1};
int one=T.kth(1);
int z2=jump(one,(k+1)/2);
int s2=T.qry(z2);
if(s2==k/2) return {z1,z2};
else if(s1==k/2){
int z3=jump(mid,k/2+1);
return {z3,z1};
}
else if(dep[z1]<dep[z2]) return {z2};
else return {z1};
}
int out[N+5];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
n=read();
for (int i{1};i<n;++i) {
int u{read()},v{read()},w{read()};
e[u].emplace_back(v,w);
e[v].emplace_back(u,w);
}
init(1,0);T.init();
curz=findz(n);
build(1,1,n);
out[n]=ans;
for (int i{n-1};i>=2;--i) {
int x=fz[mp[1]];
//cout << "KILL: " << x << endl;
out[i]=out[i+1]-mn[1];
add(1,1,n,L[x],L[x],1e18);
T.add(L[x]);
auto newz=findz(i);
if(newz.size()==2){
int z1=newz[0],z2=newz[1];
if(dep[z1]<dep[z2]) swap(z1,z2);
LL d1=dis(z2,curz[0]);
LL d2=dis(z1,curz[0]);
add(1,1,n,L[z1],R[z1],d1-d2);
add(1,1,n,1,n,d2);
}
else if(curz.size()==2){
int z1=curz[0],z2=curz[1];
LL d=dis(z1,z2);
if(dep[z1]<dep[z2]) swap(z1,z2);
if(z2==newz[0]){
add(1,1,n,L[z1],R[z1],d);
add(1,1,n,1,n,-d);
}
else add(1,1,n,L[z1],R[z1],-d);
}
curz=newz;
}
for (int i{2};i<=n;i+=2) write(out[i]);
fwrite(buf2,01,l2-buf2,stdout);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 44664kb
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: 3ms
memory: 40572kb
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:
1584401939 /../(*/.** 187586883 1439928118 /*'.+0.,)- **-.--*// 355101075 1237989172 1995925414 /*-'',(+)' /0'')-*,/+ **)/0/.+/ .)/,*/.0( +00)/--. 62948010 63385434 //*(/)(, .,-'(*.). +).0**(0. '()//)'.. /+-/0/+//- 2121090580 1365696059 517600492 ,/-)0((/, /,+('(-+.0 1670392323 449870002 (0.0.',), ....
result:
wrong answer 1st numbers differ - expected: '48829042195', found: '1584401939'