QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#701113#9165. Petrol stationsRDFZchenyyCompile Error//C++148.2kb2024-11-02 13:46:262024-11-02 13:46:27

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 13:46:27]
  • 评测
  • [2024-11-02 13:46:26]
  • 提交

answer

#include<bits/stdc++.h>

using ll=long long;

#define MAXN 70005

struct Edge{
    int v; ll w;
    int nxt;
};
struct Node{
    ll d,c,s;
    friend bool operator<(Node a,Node b#include<bits/stdc++.h>

#define int long long

using ll=long long;

#define MAXN 70005

struct Edge{
    int v; ll w;
    int nxt;
};
struct Node{
    ll d,c,s;
    friend bool operator<(Node a,Node b){
        return a.d>b.d;
    }
};

int n,x,y; ll w,k;
Edge e[MAXN*2]; int h[MAXN],ec;
int del[MAXN],sz[MAXN];
ll dep[MAXN];
ll ans[MAXN];
Node q[MAXN],all[MAXN]; int atp,qtp;
int f[MAXN][20]; ll d[MAXN][20];
int cnt[MAXN];

void add(int u,int v,ll w){
    e[ec].v=v,e[ec].w=w; e[ec].nxt=h[u];
    h[u]=ec; ec++;
    return;
}

int getsz(int u,int fa){
    sz[u]=1;
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        sz[u]+=getsz(v,u);
    }
    return sz[u];
}
int getrt(int u,int fa,int asz){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        if(sz[v]>asz/2) return getrt(v,u,asz);
    }
    return u;
}
void getdep(int u,int fa){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        dep[v]=dep[u]+e[i].w; getdep(v,u);
    }
    return;
}
void clear(int u,int fa){
    for(int i=0;i<=17;i++) f[u][i]=d[u][i]=0;
    cnt[u]=0;
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        clear(v,u);
    }
    return;
}

void dfs(int u,int fa,int rt,int osz){
    for(int i=1;i<=17;i++){
        f[u][i]=f[f[u][i-1]][i-1];
        d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
    }
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        f[v][0]=u; d[v][0]=e[i].w;
        dfs(v,u,rt,osz);
    }
    
    ll t=k; int now=u;
    cnt[u]++;
    for(int i=17;i>=0;i--){
        if(f[now][i]&&d[now][i]<=t) now=f[now][i],t-=d[now][i];
    }
    if(now==rt){
        q[++qtp].d=dep[u];
        q[qtp].c=cnt[u];        
    }else{
        ans[now]+=cnt[u]*osz;
        cnt[now]+=cnt[u];
    }
    return;
}
void calc(int u,int fa,int l,int r,ll val){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        int nl=l-1,nr=r;
        while(nl<nr){
            int mid=(nl+nr+1)>>1;
            if(q[mid].d+dep[v]>k){
                nl=mid;
            }else{
                nr=mid-1;
            }
        }
        q[r+1].c=q[nl].s-q[l-1].s;
        q[r+1].d=-dep[u];
        q[r+1].s=q[r].s+q[r+1].c;
        ans[u]+=(long long)q[r+1].c*sz[v]*val;
        calc(v,u,nl+1,r+1,val);
    }
    return;
}
void solve(int u){
    del[u]=1; dep[u]=0;
    f[u][0]=0; d[u][0]=0;
    getsz(u,0),getdep(u,0);
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(del[v]) continue;
        f[v][0]=u; d[v][0]=e[i].w;
        dfs(v,u,u,sz[u]-sz[v]);
        
        std::sort(q+1,q+qtp+1);
        for(int i=1;i<=qtp;i++){
            q[i].s=q[i-1].s+q[i].c;
        }

        ll w=e[i].w;
        int l=1,r=qtp;
        int nl=l-1,nr=r;
        while(nl<nr){
            int mid=(nl+nr+1)>>1;
            if(q[mid].d+dep[v]>k){
                nl=mid;
            }else{
                nr=mid-1;
            }
        }
        q[r+1].c=q[nl].s-q[l-1].s;
        q[r+1].d=-dep[u];
        q[r+1].s=q[r].s+q[r+1].c;
        ans[u]-=(long long)q[r+1].c*sz[v];
        calc(v,u,nl+1,r+1,-1);

        for(int i=1;i<=qtp;i++){
            all[++atp]=q[i];
        }
        qtp=0; 
    }
    for(int i=1;i<=atp;i++){
        q[i]=all[i];
    }
    std::sort(q+1,q+atp+1);
    if(atp&&q[atp].d==0) q[atp].c++;
    else q[++atp].d=0,q[atp].c=1;
    for(int i=1;i<=atp;i++){
        q[i].s=q[i-1].s+q[i].c;
    }

    calc(u,0,1,atp,1);
    clear(u,0); atp=0;
    
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(del[v]) continue;
        solve(getrt(v,u,getsz(v,u)));
    }
    return;
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),std::cout.tie(0);

    memset(h,-1,sizeof(h));
    std::cin>>n>>k;
    for(int i=1;i<=n-1;i++)
        std::cin>>x>>y>>w,x++,y++,add(x,y,w),add(y,x,w);
    solve(getrt(1,0,getsz(1,0)));
    for(int i=1;i<=n;i++) std::cout<<ans[i]<<'\n';
    return 0;
}
        return a.d>b.d;
    }
};

int n,x,y; ll w,k;
Edge e[MAXN*2]; int h[MAXN],ec;
int del[MAXN],sz[MAXN];
ll dep[MAXN];
ll ans[MAXN];
Node q[MAXN],all[MAXN]; int atp,qtp;
int f[MAXN][20]; ll d[MAXN][20];
int cnt[MAXN];

void add(int u,int v,ll w){
    e[ec].v=v,e[ec].w=w; e[ec].nxt=h[u];
    h[u]=ec; ec++;
    return;
}

int getsz(int u,int fa){
    sz[u]=1;
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        sz[u]+=getsz(v,u);
    }
    return sz[u];
}
int getrt(int u,int fa,int asz){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        if(sz[v]>asz/2) return getrt(v,u,asz);
    }
    return u;
}
void getdep(int u,int fa){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        dep[v]=dep[u]+e[i].w; getdep(v,u);
    }
    return;
}
void clear(int u,int fa){
    for(int i=0;i<=17;i++) f[u][i]=d[u][i]=0;
    cnt[u]=0;
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        clear(v,u);
    }
    return;
}

void dfs(int u,int fa,int rt,int osz){
    for(int i=1;i<=17;i++){
        f[u][i]=f[f[u][i-1]][i-1];
        d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
    }
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        f[v][0]=u; d[v][0]=e[i].w;
        dfs(v,u,rt,osz);
    }
    
    ll t=k; int now=u;
    cnt[u]++;
    for(int i=17;i>=0;i--){
        if(f[now][i]&&d[now][i]<=t) now=f[now][i],t-=d[now][i];
    }
    if(now==rt){
        q[++qtp].d=dep[u];
        q[qtp].c=cnt[u];        
    }else{
        ans[now]+=cnt[u]*osz;
        cnt[now]+=cnt[u];
    }
    return;
}
void calc(int u,int fa,int l,int r,ll val){
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(v==fa||del[v]) continue;
        int nl=l-1,nr=r;
        while(nl<nr){
            int mid=(nl+nr+1)>>1;
            if(q[mid].d+dep[v]>k){
                nl=mid;
            }else{
                nr=mid-1;
            }
        }
        q[r+1].c=q[nl].s-q[l-1].s;
        q[r+1].d=-dep[u];
        q[r+1].s=q[r].s+q[r+1].c;
        ans[u]+=(long long)q[r+1].c*sz[v]*val;
        calc(v,u,nl+1,r+1,val);
    }
    return;
}
void solve(int u){
    del[u]=1; dep[u]=0;
    f[u][0]=0; d[u][0]=0;
    getsz(u,0),getdep(u,0);
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(del[v]) continue;
        f[v][0]=u; d[v][0]=e[i].w;
        dfs(v,u,u,sz[u]-sz[v]);
        
        std::sort(q+1,q+qtp+1);
        for(int i=1;i<=qtp;i++){
            q[i].s=q[i-1].s+q[i].c;
        }

        ll w=e[i].w;
        int l=1,r=qtp;
        int nl=l-1,nr=r;
        while(nl<nr){
            int mid=(nl+nr+1)>>1;
            if(q[mid].d+dep[v]>k){
                nl=mid;
            }else{
                nr=mid-1;
            }
        }
        q[r+1].c=q[nl].s-q[l-1].s;
        q[r+1].d=-dep[u];
        q[r+1].s=q[r].s+q[r+1].c;
        ans[u]-=(long long)q[r+1].c*sz[v];
        calc(v,u,nl+1,r+1,-1);

        for(int i=1;i<=qtp;i++){
            all[++atp]=q[i];
        }
        qtp=0; 
    }
    for(int i=1;i<=atp;i++){
        q[i]=all[i];
    }
    std::sort(q+1,q+atp+1);
    if(atp&&q[atp].d==0) q[atp].c++;
    else q[++atp].d=0,q[atp].c=1;
    for(int i=1;i<=atp;i++){
        q[i].s=q[i-1].s+q[i].c;
    }

    calc(u,0,1,atp,1);
    clear(u,0); atp=0;
    
    for(int i=h[u];i+1;i=e[i].nxt){
        int v=e[i].v; if(del[v]) continue;
        solve(getrt(v,u,getsz(v,u)));
    }
    return;
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),std::cout.tie(0);

    memset(h,-1,sizeof(h));
    std::cin>>n>>k;
    for(int i=1;i<=n-1;i++)
        std::cin>>x>>y>>w,x++,y++,add(x,y,w),add(y,x,w);
    solve(getrt(1,0,getsz(1,0)));
    for(int i=1;i<=n;i++) std::cout<<ans[i]<<'\n';
    return 0;
}

Details

answer.code:13:40: error: stray ‘#’ in program
   13 |     friend bool operator<(Node a,Node b#include<bits/stdc++.h>
      |                                        ^
answer.code:13:41: error: expected ‘,’ or ‘...’ before ‘include’
   13 |     friend bool operator<(Node a,Node b#include<bits/stdc++.h>
      |                                         ^~~~~~~
answer.code:17:19: error: expected ‘)’ before ‘;’ token
   17 | using ll=long long;
      |                   ^
      |                   )
answer.code:13:26: note: to match this ‘(’
   13 |     friend bool operator<(Node a,Node b#include<bits/stdc++.h>
      |                          ^
answer.code:25:8: error: ‘Node::Node’ has the same name as the class in which it is declared
   25 | struct Node{
      |        ^~~~
answer.code:37:6: error: field ‘q’ has incomplete type ‘Node [70005]’
   37 | Node q[MAXN],all[MAXN]; int atp,qtp;
      |      ^
answer.code:11:8: note: definition of ‘struct Node’ is not complete until the closing brace
   11 | struct Node{
      |        ^~~~
answer.code:37:14: error: field ‘all’ has incomplete type ‘Node [70005]’
   37 | Node q[MAXN],all[MAXN]; int atp,qtp;
      |              ^~~
answer.code:11:8: note: definition of ‘struct Node’ is not complete until the closing brace
   11 | struct Node{
      |        ^~~~
answer.code:38:31: error: redeclaration of ‘ll Node::d [70005][20]’
   38 | int f[MAXN][20]; ll d[MAXN][20];
      |                               ^
answer.code:12:8: note: previous declaration ‘ll Node::d’
   12 |     ll d,c,s;
      |        ^
answer.code:192:9: error: expected unqualified-id before ‘return’
  192 |         return a.d>b.d;
      |         ^~~~~~
answer.code:193:6: error: expected ‘;’ after struct definition
  193 |     }
      |      ^
      |      ;
answer.code: In member function ‘void Node::clear(long long int, long long int)’:
answer.code:70:37: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   70 |     for(int i=0;i<=17;i++) f[u][i]=d[u][i]=0;
      |                                     ^
answer.code: In member function ‘void Node::dfs(long long int, long long int, long long int, long long int)’:
answer.code:82:10: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   82 |         d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
      |          ^
answer.code:82:18: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   82 |         d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
      |                  ^
answer.code:82:36: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   82 |         d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
      |                                    ^
answer.code:86:21: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   86 |         f[v][0]=u; d[v][0]=e[i].w;
      |                     ^
answer.code:93:24: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   93 |         if(f[now][i]&&d[now][i]<=t) now=f[now][i],t-=d[now][i];
      |                        ^
answer.code:93:55: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
   93 |         if(f[now][i]&&d[now][i]<=t) now=f[now][i],t-=d[now][i];
      |                                                       ^
answer.code: In member function ‘void Node::solve(long long int)’:
answer.code:126:17: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
  126 |     f[u][0]=0; d[u][0]=0;
      |                 ^
answer.code:130:21: error: invalid types ‘ll {aka long long int}[long long int]’ for array subscript
  130 |         f[v][0]=u; d[v][0]=e[i].w;
      |                     ^
answer.code: At global scope:
answer.code:194:1: error: expected declaration before ‘}’ token
  194 | };
      | ^
cc1plus: error: ‘::main’ must return ‘int’