【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> 生成了一段鏈上的變動

我們拆分之後,分別用數據結構做區間維護即可。

*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章