QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#528809#4839. Smaller LCANanani_fan#TL 2ms10064kbC++202.8kb2024-08-23 22:28:332024-08-23 22:28:33

Judging History

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

  • [2024-08-23 22:28:33]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:10064kb
  • [2024-08-23 22:28:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//注意Sum和cnt的区别
const int maxn = 3e5+10; // 数据范围
ll ans[maxn];
int n, cnt;
int sum[maxn*20], ls[maxn*20], rs[maxn*20];
// 用法:update(root, 1, n, x, f); 其中 x 为待修改节点的编号
void update(int& p, int L, int R, int x, int f) {  // 引用传参
    if (!p) p = ++cnt;  // 当结点为空时,创建一个新的结点
    if (L == R) {
        sum[p] += f;
        return;
    }
    int m = L + ((R - L) >> 1);
    if (x <= m)
        update(ls[p], L, m, x, f);
    else
        update(rs[p], m + 1, R, x, f);
    sum[p] = sum[ls[p]] + sum[rs[p]];  // pushup
}
// 用法:query(root, 1, n, l, r);
int query(int p, int L, int R, int l, int r) {
    if (!p) return 0;  // 如果结点为空,返回 0
    if (L >= l && R <= r) return sum[p];
    int m = L + ((R - L) >> 1), ans = 0;
    if (l <= m) ans += query(ls[p], L, m, l, r);
    if (r > m) ans += query(rs[p], m + 1, R, l, r);
    return ans;
}
int merge(int a, int b, int l, int r) {
    if (!a) return b;
    if (!b) return a;
    if (l == r) {
        // do something...
        return a;
    }
    int mid = (l + r) >> 1;
    ls[a] = merge(ls[a], ls[b], l, mid);
    rs[a] = merge(rs[a], rs[b], mid + 1, r);
    sum[a]=sum[ls[a]]+sum[rs[a]];
    return a;
}
vector<int> ve[maxn];
int L[maxn],R[maxn],dfn;
int Sum[maxn];
void add(int l,int r,int x){
    Sum[l]+=x;
    Sum[r+1]-=x;
}
void dfs1(int x,int h)
{
    L[x]=++dfn;
    for(auto it:ve[x])if(it!=h){
        dfs1(it,x);
    }
    R[x]=dfn;
}
int root;
int dfs(int x,int h){
    int rt=0;
    for(auto it:ve[x])if(it!=h){
        int g=dfs(it,x);
        int t=query(g,1,n,x,n);
        add(L[x],R[x],t);
        add(L[it],R[it],-t);
        rt=merge(rt,g,1,n);
        // cout<<"root="<<root<<" x="<<x<<" it="<<it<<" t="<<t<<endl;
    }   
    if((ll)x*root<=(ll)n&&x>=root){
        add(L[x],R[x],1);
        update(rt,1,n,x*root,1);
    }
    return rt;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++){
        int x,y;cin>>x>>y;
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    ll all=0;
    for(int i=1;i<=n;i++){
        int g=n/i;
        g++;
        g=max(g,i);
        if(n>=g)all+=n-g+1;
    }
    vector<ll> ans(n+1);
    for(root=1;root<=n;root++){
        if(root*root>n)break;
        for(int i=1;i<=cnt;i++)sum[i]=ls[i]=rs[i]=0;
        cnt=0;
        dfn=0;
        memset(Sum,0,sizeof(Sum));
        dfs1(root,-1);
        dfs(root,-1);
        for(int i=1;i<=n;i++)Sum[i]+=Sum[i-1];
        for(int i=1;i<=n;i++)ans[i]+=Sum[L[i]];
        // for(int i=1;i<=n;i++)cout<<sum[L[i]]<<" \n"[i==n];
    }
    for(int i=1;i<=n;i++)cout<<all+ans[i]<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10064kb

input:

5
1 2
4 2
2 5
3 5

output:

15
15
15
15
14

result:

ok 5 number(s): "15 15 15 15 14"

Test #2:

score: -100
Time Limit Exceeded

input:

300000
40632 143306
32259 40632
225153 143306
269774 225153
289668 40632
191636 269774
85717 191636
58564 191636
156509 143306
289939 40632
247103 269774
40257 40632
98149 289668
142277 143306
291616 40257
46813 225153
56324 143306
277154 142277
53903 289668
114266 32259
152231 58564
241151 152231
4...

output:


result: