51nod - 1199 Money out of Thin Air

一棵有N个节点的树,每个节点对应1个编号及1个权值,有2种不同的操作。

操作1:S x y z,表示如果编号为x的节点的权值 < y,则将节点x的权值加上z。(Single)

操作2:A x y z,表示如果编号为x的节点以及其所有子节点的权值平均值 < y,则将节点x及其所有子节点的权值加上z。(All)

给出树节点之间的关系,进行M次操作,问所有操作完成后,各个节点的权值为多少?

节点的编号为0 - N - 1,根节点的编号为0,并且初始情况下,根节点的权值也是0。

Input

第1行:2个数N, M,N为节点的数量,M为操作的数量(1 <= N, M <= 50000)。
第2 - N行:每行描述一个节点N[i]的信息,第2行对应编号为1的节点,第N行对应编号为N - 1的节点。具体内容为:每行2个数P[i], W[i]。P[i]为当前节点的父节点的编号,W[i]为当前节点的权值。(0 <= W[i] <= 10^5, P[i] < i)
第N + 1 - N + M行:每行表示一个操作,S x y z或A x y z,(0 <= y, z <= 10^5)。

Output

输出共N行,每行1个数W[i],表示经过M次后,编号为0 - N - 1的节点的权值。

Input示例

4 3
0 10
0 10
1 2
S 0 1 1
A 0 20 1
S 3 2 1

Output示例

2
11
11
3

思路:

先根据父子关系用DFS将各项放到数组里,然后利用线段树进行操作。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;

typedef long long ll;
const int MAXN = 50000 + 5;
#define lChild rt << 1
#define rChild rt << 1 | 1

ll n, m, counter;
int w[MAXN], son[MAXN], dfn[MAXN], num[MAXN];
 
vector<int> G[MAXN];
 
struct node
{
    int l, r;
    ll sum;
    ll lazy;
}tree[MAXN << 2];
 
void dfs(int u)
{
    dfn[u] = ++counter;
    son[u] = 1;
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        dfs(v);
        son[u] += son[v];
    }
}
 
void PushDown(int rt)
{
    if (tree[rt].lazy)
    {
        tree[lChild].lazy += tree[rt].lazy;
        tree[rChild].lazy += tree[rt].lazy;
        tree[lChild].sum += tree[rt].lazy * (tree[lChild].r - tree[lChild].l + 1);
        tree[rChild].sum += tree[rt].lazy * (tree[rChild].r - tree[rChild].l + 1);
        tree[rt].lazy = 0;
    }
}
 
void build(int rt, int l, int r)
{
    tree[rt].lazy = 0;
    tree[rt].l = l;
    tree[rt].r = r;
    tree[rt].sum = 0;
    if (l == r)
    {
        tree[rt].sum = num[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lChild, l, mid);
    build(rChild, mid + 1, r);
    tree[rt].sum = tree[lChild].sum + tree[rChild].sum;
}
 
void update(int rt, int ql, int qr, int l, int r, ll z)
{
    if (ql <= l && qr >= r)
    {
        tree[rt].sum += (r - l + 1) * z;
        tree[rt].lazy += z;
        return;
    }
    PushDown(rt);
    int mid = (l + r) >> 1;
    if (ql <= mid)
	{
		update(rt << 1, ql, qr, l, mid, z);
	}
    if (qr > mid) 
	{
		update(rt << 1 | 1, ql, qr, mid + 1, r, z);
	}
    
    tree[rt].sum = tree[lChild].sum + tree[rChild].sum;
}
 
ll query(int rt, int ql, int qr, int l, int r)
{
    if (ql <= l && qr >= r)  
	{
		return tree[rt].sum;
	}
    PushDown(rt);
    int mid = (l + r) >> 1;
    ll result = 0;
    if (ql <= mid)  
	{
		result += query(lChild, ql, qr, l, mid);
	}
    if (qr > mid)
	{
		result += query(rChild, ql, qr, mid + 1, r);
	}
    return result;
}
 
int main()
{
	cin >> n >> m;
    for (int i = 0; i < n; i++)
	{
		G[i].clear();
	}
    for (int i = 1; i < n; i++)
    {
        int p; 
		cin >> p >> w[i];
        G[p].push_back(i);
    }
    w[0] = 0;
    counter = 0;
    dfs(0);
    for (int i = 0; i < n; i++)
	{
		num[dfn[i]] = w[i];
	}
 
    build(1, 1, n);
    char op; 
	ll x, y, z;
    
	for (int i = 0; i < m; i++)
    {
		cin >> op >> x >> y >> z;
        if (op == 'S')
        {
            if (query(1, dfn[x], dfn[x], 1, n) < y)  
			{
				update(1, dfn[x], dfn[x], 1, n, z);
			}
        }
        else
        {
            if (query(1, dfn[x], dfn[x] + son[x] - 1, 1, n) < son[x] * y)  
			{
				update(1, dfn[x], dfn[x] + son[x] - 1, 1, n, z);
			}
        }
    }
    for (int i = 0; i < n; i++)
	{
		cout << query(1, dfn[i], dfn[i], 1, n) << endl;
	}
 
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值