【Codeforces Round 169 (Div 2) E】【数据结构区间维护】Little Girl and Problem on Trees 大菊花树的距离性修改查询

E. Little Girl and Problem on Trees
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A little girl loves problems on trees very much. Here's one of them.

A tree is an undirected connected graph, not containing cycles. The degree of node x in the tree is the number of nodes y of the tree, such that each of them is connected with node x by some edge of the tree.

Let's consider a tree that consists of n nodes. We'll consider the tree's nodes indexed from 1 to n. The cosidered tree has the following property: each node except for node number 1 has the degree of at most 2.

Initially, each node of the tree contains number 0. Your task is to quickly process the requests of two types:

  • Request of form: 0 v x d. In reply to the request you should add x to all numbers that are written in the nodes that are located at the distance of at most d from node v. The distance between two nodes is the number of edges on the shortest path between them.
  • Request of form: 1 v. In reply to the request you should print the current number that is written in node v.
Input

The first line contains integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 105) — the number of tree nodes and the number of requests, correspondingly.

Each of the next n  -  1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi), that show that there is an edge between nodes uiand vi. Each edge's description occurs in the input exactly once. It is guaranteed that the given graph is a tree that has the property that is described in the statement.

Next q lines describe the requests.

  • The request to add has the following format: 0 v x d (1 ≤ v ≤ n1 ≤ x ≤ 1041 ≤ d < n).
  • The request to print the node value has the following format: 1 v (1 ≤ v ≤ n).

The numbers in the lines are separated by single spaces.

Output

For each request to print the node value print an integer — the reply to the request.

Examples
input
3 6
1 2
1 3
0 3 1 2
0 2 3 1
0 1 5 2
1 1
1 2
1 3
output
9
9
6
input
6 11
1 2
2 5
5 4
1 6
1 3
0 3 1 3
0 3 4 5
0 2 1 4
0 1 5 5
0 4 6 2
1 1
1 2
1 3
1 4
1 5
1 6
output
11
17
11
16
17
11

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
#define mid (l+r>>1)
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define rt 1,1,n
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, Q;
vector<int>a[N];
int dep[N], mxdep[N];
int pos[N], tim;
void dfs(int x, int fa)
{
	pos[x] = ++tim;
	dep[x] = dep[fa] + 1;
	mxdep[x] = dep[x];
	for (auto y : a[x])if (y != fa)
	{
		dfs(y, x);
		gmax(mxdep[x], mxdep[y]);
	}
}
LL bit[N];
void add(int x, int val)
{
	for (; x; x -= x&-x)bit[x] += val;
}
LL check(int x)
{
	LL ret = 0;
	for (; x <= n; x += x&-x)ret += bit[x];
	return ret;
}
int L, R, P, V;
struct SGT
{
	LL val[1 << 18], flag[1 << 18];
	void build(int o, int l, int r)
	{
		val[o] = flag[o] = 0;
		if (l == r)return;
		build(lson);
		build(rson);
	}
	void pushdown(int o)
	{
		if (flag[o])
		{
			flag[ls] += flag[o];
			flag[rs] += flag[o];
			val[ls] += flag[o];
			val[rs] += flag[o];
			flag[o] = 0;
		}
	}
	LL check(int o, int l, int r)
	{
		if (l == r)return val[o];
		pushdown(o);
		if (P <= mid)return check(lson);
		else return check(rson);
	}
	void add(int o, int l, int r)
	{
		if (L <= l && r <= R)
		{
			flag[o] += V;
			val[o] += V;
			return;
		}
		pushdown(o);
		if (L <= mid)add(lson);
		if (R > mid)add(rson);
	}
}sgt;
int main()
{
	while (~scanf("%d%d", &n, &Q))
	{
		for (int i = 1; i <= n; ++i)
		{
			a[i].clear();
			bit[i] = 0;
		}
		for (int i = 1; i < n; ++i)
		{
			int x, y; scanf("%d%d", &x, &y);
			a[x].push_back(y);
			a[y].push_back(x);
		}
		tim = 0; dep[0] = 0; dfs(1, 0);
		sgt.build(rt);
		for (int i = 1; i <= Q; ++i)
		{
			int op, x, dis;
			scanf("%d%d", &op, &x);
			if (op == 0)
			{
				scanf("%d%d", &V, &dis);
				if (dis >= dep[x] - dep[1])//能覆盖到根
				{
					int outdep = dep[1] + (dis - dep[x] + 1); gmin(outdep, n);
					add(outdep, V);
 					int mxgodep = min(mxdep[x], dep[x] + dis);
					if (outdep < mxgodep)
					{
						L = pos[x] + outdep + 1 - dep[x];
						R = pos[x] + mxgodep - dep[x];
						sgt.add(rt);
					}
				}
				else //无法覆盖到根
				{
					int mngodep = dep[x] - dis;
					int mxgodep = min(mxdep[x], dep[x] + dis);
					L = pos[x] - (dep[x] - mngodep);
					R = pos[x] + (mxgodep - dep[x]);
					sgt.add(rt);
				}
			}
			else
			{
				P = pos[x];
				LL ans1 = check(dep[x]);
				LL ans2 = sgt.check(rt);
				printf("%lld\n", ans1 + ans2);
			}
		}
	}
	return 0;
} 
/*
【题意】
怎么静下心来读懂题是非常重要的~
这道题给出一棵节点数为n(1e5)的树,树上每个点,除了根节点,最多度数为2。
这说明这棵树,是若干条链连在了1号节点上。
一开始,所有节点的权值都为0
我们有两种操作——
0 x val dis		:我们对于所有距离x不超过dis的节点,加权val
1 x				:查询节点x的权值是多少

【分析】
首先,我们知道了树的结构是这样子的,若干条链连在了1号节点上。

于是,对于操作0,其操作可以被拆分为——
<1> 生成了由根节点1发散出的等距离波的变动
<2> 生成了一段链上的变动

我们拆分之后,分别用数据结构做区间维护即可。

*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值