1. 程式人生 > >瀋陽網路賽 J題 Ka Chang (樹狀陣列+dfs序+分塊思想)

瀋陽網路賽 J題 Ka Chang (樹狀陣列+dfs序+分塊思想)

Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point.

Then, you need to handle QQ operations. There're two types:

1\ L\ X1 L X: Increase points by XX of all nodes whose depth equals LL ( the depth of the root is zero ). (x \leq 10^8)(x≤108)

2\ X2 X: Output sum of all points in the subtree whose root is XX.

Input

Just one case.

The first lines contain two integer, N,QN,Q. (N \leq 10^5, Q \leq 10^5)(N≤105,Q≤105).

The next n-1n−1 lines: Each line has two integer aa,bb, means that node aa is the father of node bb. It's guaranteed that the input data forms a rooted tree and node 11 is the root of it.

The next QQ lines are queries.

Output

For each query 22, you should output a number means answer.

樣例輸入複製

3 3
1 2
2 3
1 1 1
2 1
2 3

樣例輸出複製

1
0

題目來源

#include <bits/stdc++.h>
using namespace std;
#define ll long long int 
const int maxn = 100100;
const int inf = 0x3f3f3f3f;
const int Big = 1000;
int n, m, head[maxn], cnt, st[maxn], en[maxn], ecnt;
vector<int>deep[maxn];
vector<int>large;
ll c[maxn], extra[maxn];
int read() {
	char ch = ' ';
	int ans = 0;
	while (ch<'0' || ch>'9')
		ch = getchar();
	while (ch <= '9' && ch >= '0') {
		ans = ans * 10 + ch - '0';
		ch = getchar();
	}
	return ans;
}
int lowbit(int x) {
	return x&(-x);
}
ll sum(int x) {
	ll ans = 0;
	while (x) {
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
void add_c(int x, int d) {
	while (x <= n) {
		c[x] += d;
		x += lowbit(x);
	}
}
void init() {
	for (int s = 0; s <= n; s++)
		deep[s].clear();
	large.clear();
	cnt = ecnt = 0;
	memset(c, 0, sizeof(c));
	memset(extra, 0, sizeof(extra));
	memset(head, -1, sizeof(head));
}
struct fuck {
	int u, v, w, ne;
}ed[maxn];
void add(int u, int v) {
	ed[cnt].u = u; ed[cnt].v = v;
	ed[cnt].ne = head[u]; head[u] = cnt++;
}
void dfs(int x, int dep) {
	st[x] = ++ecnt;
	deep[dep].push_back(st[x]);
	for (int s = head[x]; ~s; s = ed[s].ne) {
		int v = ed[s].v;
		dfs(v, dep + 1);
	}
	en[x] = ecnt;
}
int main() {
	while (~scanf("%d%d", &n, &m)) {
		init();
		for (int s = 1; s < n; s++) {
			int a, b, c;
			a = read(); b = read();
			add(a, b);
		}
		dfs(1, 0);
		for (int s = 1; s <= n; s++)
			if (deep[s].size() >= Big)
				large.push_back(s);
		while (m--) {
			int kind, a, b;
			kind = read();
			if (kind == 1) {
				a = read(); b = read();
				int sz = deep[a].size();
				if (sz < Big)
					for (int s = 0; s < sz; s++) {
						int v = deep[a][s];
						add_c(v, b);
					}
				else
					extra[a] += b;
			}
			else {
				a = read();
				ll fir, sec, ans;
				fir = sum(st[a] - 1); sec = sum(en[a]);
				ans = sec - fir;
				for (int s = 0; s < large.size(); s++) {
					ans += (upper_bound(deep[large[s]].begin(), deep[large[s]].end(), en[a]) -
						lower_bound(deep[large[s]].begin(), deep[large[s]].end(), st[a])) * extra[large[s]];
				}
				printf("%lld\n", ans);
			}
		}
	}
}

相關推薦

瀋陽網路 J Ka Chang 陣列+dfs+思想

Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, you need to handle QQ operations. The

ACM-ICPC 2018 瀋陽賽區網路預賽 J. Ka Chang +陣列+dfs

題意 給你一顆樹,由兩種操作: 1.把這棵樹深度為 D D D的點全部都加上一個值。 2.求以p為根節點的子樹的權值和是多少? 思路 對於樹上的東西,我們可以把他求一下DFS序,之後就可以把樹上的結構變成

ACM-ICPC 2018 瀋陽賽區網路預賽 J. Ka Chang +陣列+dfs

題意 給你一顆樹,由兩種操作: 1.把這棵樹深度為DD的點全部都加上一個值。 2.求以p為根節點的子樹的權值和是多少? 思路 對於樹上的東西,我們可以把他求一下DFS序,之後就可以把樹上的結構變成線性的結構,之後就是查詢和修改這兩個東西了。 關於修改

藍書演算法競賽進階指南記錄——POJ3468 A Simple Problem with Intergers陣列維護差

題目:poj3468. 題目大意:給定一個序列a,要求支援: 1.格式C a b c,表示將[a,b]的權值都加上c. 2.格式Q a b,表示查詢[a,b]的權值和. 線段樹裸題(我像個傻子一樣寫了個LCT做了一遍),可是我們這裡不用線段樹,我們討論樹狀陣列的解法. 我們已

NOIP模擬 相遇LCA+陣列+dfs

【題目描述】 豪哥生活在一個n個點的樹形城市裡面,每一天都要走來走去。雖然走的是比較的多,但是豪哥在這個城市裡面的朋友並不是很多。 當某一天,猴哥給他展現了一下大佬風範之後,豪哥決定要獲得一些交往機會來提升交往能力。豪哥現在已經物色上了一條友,打算和它(豪哥並不讓吃瓜群眾

hdu 2838 Cow Sorting 陣列+逆對變形

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked

UVALive 4329 Ping pong陣列求逆序數+順序數

題意:對於給定的一個長度為n序列a,對於每個位置i,若左邊存在一個數a[l],右邊存在一個數a[r],滿足a[l] < a[i] < a[r]或a[l] > a[i] > a[r],則(l, r)構成一對,求總共有多少對。 思路:雖

2018南京網路 j sum篩法、非平方數相乘

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #incl

ACM-ICPC 2018瀋陽網路G唯一分解定理、容斥原理

A sequence of integer \lbrace a_n \rbrace{an​} can be expressed as: \displaystyle a_n = \left\{ \begin{array}{lr} 0, & n=0\\ 2, &am

ACM-ICPC 2018 瀋陽賽區網路預賽 F Fantastic Graph 有源匯的上下界可行流

題意 給你兩個集合X,Y,X集合有N個點,Y集合有M個點,輸入一個上下界down,up,現在有K條邊,輸入K條邊(u,v)。每選擇一條邊(u,v),u和v點的權值就+1,問能否通過選擇一些邊(每條邊只能選一次)使得所有點的權值都在[down,up]之間。 思路 有源匯的

1018 ACM-ICPC 青島賽區網路-J Press the Button瞎搞

BaoBao and DreamGrid are playing a game using a strange button. This button is attached to an LED light (the light is initially off), a co

計蒜客-2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網路JOur Journey of Dalian Ends (最小費用最大流)

題意: 給定若干個城市,出發點為大連,目的地為西安,但是要求中途必須經過上海,並且圖中每個城市只能經過一次,給出m條路(雙向道路),走第i條路需要wi代價,求所有滿足要求的方案中花費的最小代價,如果沒有滿足的方案,輸出-1。 思路: 相當於求從大連到上海加上西安到上海花費的

2018 ICPC徐州網路 H.Ryuji doesn't want to study 陣列

Ryuji is not a goodstudent, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]. Unfort

ICPC2017網路南寧子序列最大權值陣列+dp

Let SS be a sequence of integers s_{1}s ​1 ​​ , s_{2}s ​2 ​​ , ......, s_{n}s ​n ​​ Each integer is is associated with a weight by the f

牛客網暑期ACM多校訓練營第一場J Different Integers陣列 + 離線 或 莫隊

題目描述  Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l

牛客網暑期ACM多校訓練營第二場J.farm (隨機數+二維陣列)

題目連結 時間限制:C/C++ 4秒,其他語言8秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld 題目描述 White Rabbit has a rectangular farmland of n*m. In

bnu 51636 Squared Permutation陣列北師16校

最近,無聊的過河船同學在玩一種奇怪的名為“小Q的惡作劇”的紙牌遊戲。 現在過河船同學手有張牌,分別寫著,打亂順序之後排成一行,位置從左往右按照標號。 接下來小Q同學會給出個操作,分為以下兩種: 1.給定,交換從左往右數的第和第張牌, 2.給定,對從左往右數的第張牌,記下

2018牛客網暑假多校第一場J陣列+思維

題目描述:     有一個n個數的數列,並由q個詢問,每一個詢問有一個l和r,問你在區間a1—al和ar—an這兩個區間中有多少個不同的數。 題目分析:     這個題目事實上是spoj的某一題的改編,原題是求l到r區間有多少個不同的數,現在這個題是要求兩個分開的區間有

【bzoj1103】【POI2007】【大都市】數組+差

sin 騎摩托車 為什麽 ons con size hint ont iostream 在經濟全球化浪潮的影響下,習慣於漫步在清晨的鄉間小路的郵遞員Blue Mary也開始騎著摩托車傳遞郵件了。不過,她經常回憶起以前在鄉間漫步的情景。昔日,鄉下有依次編號為1..n的n個小村

2018.10.29 bzoj3718: [PA2014]Parking陣列

傳送門 顯然只用判斷兩個會相交的車會不會卡住就行了。 直接樹狀陣列維護字尾最大值就行了。 程式碼: #include<bits/stdc++.h> using namespace std; const int N=5e4+5; struct Matrix{int x1,x