Just a Hook HDU - 1698 (線段樹區間修改查詢)

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 



Now Pudge wants to do some operations on the hook. 

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. 
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: 

For each cupreous stick, the value is 1. 
For each silver stick, the value is 2. 
For each golden stick, the value is 3. 

Pudge wants to know the total value of the hook after performing the operations. 
You may consider the original hook is made up of cupreous sticks. 

Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. 
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. 
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example. 

Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.

 

題意:

給出一個N表示有一個區間[1, n],區間裏每個點的初值爲1,現在給出Q(0 <=Q <= 100000)個修改操作;

每個操作給出3個數x,y,z表示修改區間[x,y]裏的每個點的值爲z(1<=z<=3),最後要你輸出[1,n]區間裏所有點的和 

注意輸出格式

#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1e6 + 7;

struct Tree {
	int l, r, sum, lazy;
};

struct Tree tree[N];

void pushup (int rt) {
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}

void build (int l, int r, int rt) {
	
	tree[rt].l = l, tree[rt].r = r, tree[rt].lazy = 0;//把懶惰標記清零 
	if (l == r) {
		tree[rt].sum = 1;
		return;
	}
	int mid = (l + r) >> 1;
	build (l, mid, rt << 1);
	build (mid + 1, r, rt << 1 | 1);
	pushup (rt);
	
}

void pushdown (int rt) {
	
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	tree[rt << 1].sum = tree[rt].lazy * (mid - tree[rt].l + 1);
	tree[rt << 1 | 1].sum = tree[rt].lazy * (tree[rt].r - mid);
	tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = tree[rt].lazy;
	tree[rt].lazy = 0;
	
}

void update (int l, int r, int val, int rt) {
	
	if (tree[rt].l == l && tree[rt].r == r) {
		tree[rt].sum = val * (r - l + 1);
		tree[rt].lazy = val;
		return;
	}
	if (tree[rt].lazy) pushdown (rt);
	
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (r <= mid) update (l, r, val, rt << 1);
	else if (l > mid) update (l, r, val, rt << 1 | 1);
	else {
		update (l, mid, val, rt << 1);
		update (mid + 1, r, val, rt << 1 | 1);
	} 
	pushup (rt);
}

int query (int l, int r, int rt) {
	
	if (tree[rt].l == l && tree[rt].r == r) {
		return tree[rt].sum;
	}
	if (tree[rt].lazy) pushdown (rt);
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (r <= mid) return query (l, r, rt << 1);
	else if (l > mid) return query (l, r, rt << 1 | 1);
	else return query (l, mid, rt << 1) + query (mid + 1, r, rt << 1 | 1);
	
}




int main() {
	
	int t, k = 0;
	scanf ("%d", &t);
	while (t--) {
		
		int n, q;
		scanf ("%d %d", &n, &q);
		build (1, n, 1);
		int x, y, z;
		for (int i = 1; i <= q; ++i) {
			scanf ("%d %d %d", &x, &y, &z);
			update (x, y, z, 1);
		}
		printf ("Case %d: The total value of the hook is %d.\n", ++k, query (1, n, 1));
	}
	
	
	return 0;
} 

 

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