HDU 1698 Just a Hook(线段树区间置换)

来源:互联网 发布:淘宝开店最新流程ppt 编辑:程序博客网 时间:2024/06/05 11:15
                                                                                                     Just a Hook
Crawling in process...Crawling failed                                                Time Limit:2000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
                                                                                                   Submit Status

Description

Input

Output

Sample Input

Sample Output

Hint

Description

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

11021 5 25 9 3
 

Sample Output

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

题意:屠夫有一条长的钩子,钩子由n个小钩子组成,每次操作将一个区间的小钩子变成金(3)银(2)铜(1)三者之一,最后取出所有区间的钩子总长度

思路:区间置换好区间跟新一个思考,简单的区间更新;

AC code:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long LL;#define N 1000100struct node{LL l;LL r;LL sum;LL lazy;      //lazy标记记录更新值}tree[N << 2];void pushdown(LL root, LL len){if (tree[root].lazy){                                                  //更新子区间标记tree[root * 2].lazy = tree[root].lazy;tree[root * 2 + 1].lazy = tree[root].lazy;    //更新子区间和tree[root * 2].sum = tree[root].lazy*(len - (len / 2));tree[root * 2 + 1].sum = tree[root].lazy*(len / 2);tree[root].lazy = 0;          //lazy标记清零}}void build(LL root, LL l, LL r){tree[root].l = l;tree[root].r = r;tree[root].lazy = 0;     //初始化lazyif (l == r){tree[root].sum = 1;  // 钩子最初全为铜return;}LL mid = (tree[root].l + tree[root].r) / 2;build(root * 2, l, mid);build(root * 2 + 1, mid + 1, r);tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;}void update(LL root, LL l, LL r, LL c){if (l <= tree[root].l &&tree[root].r <= r){tree[root].lazy = c;        //更新最大区间的lazy标记,用的时候才传递 防止超时tree[root].sum = c*(tree[root].r - tree[root].l + 1);  //同时维护sumreturn;}pushdown(root, tree[root].r - tree[root].l + 1); //更新儿子节点的标记,防止二次更新使区间和,lazy标记被覆盖LL mid = (tree[root].l + tree[root].r) / 2;if (l <= mid){update(root * 2, l, r, c);}if (r > mid){update(root * 2 + 1, l, r, c);}tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;}LL query(LL root, LL l, LL r){LL ans = 0;LL mid = (tree[root].l + tree[root].r) / 2;if (l <= tree[root].l &&tree[root].r <= r){return tree[root].sum;}pushdown(root, tree[root].r - tree[root].l + 1);  //将父亲的lazy标记传递下去,更新儿子节点,维护查找的区间和准确if (l <= mid){ans += query(root * 2, l, r);}if (r > mid){ans += query(root * 2 + 1, l, r);}tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;return ans;}int main(){LL i, x, y, z, n, m, t, k = 1;scanf("%lld", &t);while (t--){scanf("%lld", &n);build(1, 1, n);scanf("%lld", &m);for (i = 1; i <= m; i++){scanf("%lld%lld%lld", &x, &y, &z);update(1, x, y, z);}printf("Case %lld: The total value of the hook is %lld.\n", k++, query(1, 1, n));}return 0;}

0 0
原创粉丝点击