Just a Hook HDU - 1698 (线段树区间修改查询)

版权声明: https://blog.csdn.net/nucleare/article/details/82694026

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.

题意:

扫描二维码关注公众号,回复: 3754805 查看本文章

给出一个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;
} 

猜你喜欢

转载自blog.csdn.net/nucleare/article/details/82694026