首页 > 其他 > 详细

树链差分的一道题

时间:2019-04-05 23:22:42      阅读:152      评论:0      收藏:0      [点我收藏+]

https://vjudge.net/contest/292999#problem/J

https://blog.csdn.net/V5ZSQ/article/details/64919651?locationNum=5&fps=1

#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
const int maxm = 5e5 + 5;
typedef long long ll;
ll w[maxm], sum[maxm];
int pa[maxm], id[maxm], cnt[maxm];
int t;
int n;
struct edge {
int v;
ll w;
edge(int v = 0, ll w = 0) : v(v), w(w) {};
};
vector<edge> ve[maxm];
int res;
void dfs(int u, int p) {
    for(int i = 0; i < ve[u].size(); i++) {
        int v = ve[u][i].v;
        ll val = ve[u][i].w;
        if(v == p) continue;
        pa[v] = u;
        id[res] = v;
        sum[res] = sum[res - 1] + val;
        int pos = lower_bound(sum, sum + res + 1, sum[res] - w[v]) - sum;
        if(pos < res) cnt[u]++, cnt[pa[id[pos] ] ]--;
        res++;
        dfs(v, u);
        res--;
        cnt[u] += cnt[v];
    }
}

int main() {
freopen("car.in", "r", stdin);
scanf("%d", &t);
while(t--) {
    scanf("%d", &n);
    memset(pa, 0, sizeof(pa));
    memset(sum, 0, sizeof(sum));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 1; i <= n; i++) ve[i].clear();
    int u, v;
    ll val;
    for(int i = 1; i <= n; i++) scanf("%lld", &w[i]);
    for(int i = 1; i < n; i++) {
        scanf("%d%d%lld", &u, &v, &val);
        ve[u].push_back(edge(v, val));
        ve[v].push_back(edge(u, val));
    }
    pa[1] = 0, id[0] = 1, sum[0] = 0;
    res = 1;
    dfs(1, 1);
    for(int i = 1; i <= n; i++) {
        printf("%d%c", cnt[i], i == n ? \n :  );
    }
}

return 0;
}

 

树链差分的一道题

原文:https://www.cnblogs.com/downrainsun/p/10660377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!