739B Codeforces Alyona and a tree 树上差分+二分(倍增)

题目大意是给出一棵有根树,树上每个点、每条边都有一个权值。

现在给出“控制”的定义:对一个点u,设点v在其子树上,且 dis(u,v)av ,则称u控制v。

要求求出每个点控制了多少个点

模拟dfs过程,我们很容易发现dfs到点u时,其祖先节点到根的dis值都已经算出,且是单调递增,所以我们可以用二分或者倍增,在log的时间复杂度内找到深度最小的满足 dis(u,v)au 的点了。找到点v后,v到fa(u)上的每个点的答案都要增加1,显然用树上差分来实现,对ans[fa[v]]–,ans[fa[u]]++

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iostream>

using namespace std;

const int maxn = 200010;
const int lg = 20;

int n;
long long a[maxn];
long long c[maxn];
struct edge
{
    int v,w,next;
}e[maxn << 1];
long long dep[maxn];
int h[maxn],num = 0;
int f[maxn][lg+1];
int u,v;
long long w;

void build(int u,int v,int w)
{
    num++;
    e[num].v = v;
    e[num].w = w;
    e[num].next = h[u];
    h[u] = num;
}

void dfs1(int x,int fa)
{
    int v;
    f[x][0] = fa;
    for(int i = 1; i < lg; i++)
        f[x][i] = f[f[x][i-1]][i-1];
    int u = x;
    for(int i = lg - 1; i >= 0; i--)
        while(u > 1 && dep[x] - dep[f[u][i]] <= a[x])
            u = f[u][i];
    u = max(1,u);
    c[f[u][0]]--;
    c[f[x][0]]++;
    for(int i = h[x]; i; i = e[i].next)
    {
        v = e[i].v;
        if(v != fa)
        {
            dep[v] = dep[x] + e[i].w;
            dfs1(v,x);
        }
    }
}

void dfs2(int x,int f)
{
    int v;
    for(int i = h[x]; i; i = e[i].next)
    {
        v = e[i].v;
        if(v != f)
        {
            dfs2(v,x);
            c[x] += c[v];
        }
    }
}

int main()
{
    freopen("1.in","r",stdin);
    cin >> n;
    for(int i = 1; i <= n; i++)
        scanf("%I64d",&a[i]);
    for(int i = 2; i <= n; i++)
    {
        scanf("%d%I64d",&v,&w);
        build(i,v,w);
        build(v,i,w);
    }
    dfs1(1,0);
    dfs2(1,0);
    for(int i = 1; i <= n; i++)
        printf("%I64d%c",c[i],i == n ? '\n' : ' ');
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用的内容,CodeForces的初始分由1500改为了1400,并且前六场的初始分配如下:第一场500分,第二场350分,第三场250分,第四场150分,第五场100分,第六场50分。所以,根据你在第一场的表现分为368,你的总分应为868(1400+368)。接下来的五场比赛,你的表现分将根据具体情况来决定。每场比赛的表现分会对你的总分产生影响。 关于其他引用和的代码,它们似乎是一些针对不同情况进行计算和输出的算法,并不直接涉及到CodeForces上的分数计算。所以,如果你有关于CodeForces上分的具体问题,请提供更多详细信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [CodeForces前六场定级赛赋分规则(2020.5修订)及各段位对应分数段](https://blog.csdn.net/Sunshine_xiaohao/article/details/112106625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [CodeForces上分日记 (思维题)(A. Garland)1809A (Educational Round 145 For Div.2)](https://blog.csdn.net/weixin_60375636/article/details/130243342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [【codeforces】一切为了上分:骚操作合集](https://blog.csdn.net/weixin_45497996/article/details/109157711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值