1. 程式人生 > >HDU 1698 Just a Hook(線段樹成段更新)

HDU 1698 Just a Hook(線段樹成段更新)

there clas class rec std sts present script metal

題目網址:http://acm.hdu.edu.cn/showproblem.php?pid=1698

題目:

Problem 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 1 10 2 1 5 2 5 9 3 Sample Output Case 1:The total value of the hook is 24.

思路:

懶惰標記法,延遲更新。沒學過這個知識點的,可以先學習一下。網上有篇介紹該知識點的博客:http://blog.csdn.net/zip_fan/article/details/46775633

代碼:

 1 #include <cstdio>
 2 const int N=4e5;
 3 struct node{
 4     int l,r;
 5     int lazy,sum;
 6 }tree[N];
 7 int n;
 8 void pushup(int i){//更新父節點
 9     tree[i].sum=tree[(i*2)+1].sum+tree[i*2].sum;
10 }
11 void pushdown(int i){//更新子節點
12     if(tree[i].lazy){
13         tree[2*i].lazy=tree[2*i+1].lazy=tree[i].lazy;//將子節點也懶惰標記
14         tree[2*i].sum=(tree[2*i].r-tree[2*i].l+1)*tree[2*i].lazy;//sum會等於長度值*標記值
15         tree[2*i+1].sum=(tree[2*i+1].r-tree[2*i+1].l+1)*tree[2*i+1].lazy;
16         tree[i].lazy=0;//更新完,取消該節點的標記
17     }
18 }
19 void build(int bg,int ed,int i){
20     if(i>4*n)   return;
21     tree[i].l=bg;
22     tree[i].r=ed;
23     tree[i].lazy=0;//多個測試樣例,註意初始化
24     if (bg == ed)   tree[i].sum=1;
25     else{
26         int mid=(bg+ed)/2;
27         build(bg, mid, 2*i);
28         build(mid+1, ed, 2*i+1);
29         pushup(i);//回溯更新父節點
30         
31     }
32 }
33 void update(int bg,int ed,int i,int v){
34     if(bg<=tree[i].l && tree[i].r<=ed){
35         tree[i].lazy=v;
36         tree[i].sum=(tree[i].r-tree[i].l+1)*v;
37         return ;
38     }
39     pushdown(i);//當用到該節點時,就向下更新
40     int mid=(tree[i].r+tree[i].l)/2;
41     if(ed<=mid) update(bg, ed, 2*i, v);//該區間完全在左子樹
42     else if(bg>mid) update(bg, ed, 2*i+1, v);//該區間完全在右子樹
43     else if(bg<=mid && ed>mid){//既在左子樹又在右子樹
44         update(bg, mid, 2*i, v);
45         update(mid+1, ed, 2*i+1, v);
46     }
47     pushup(i);//回溯更新父節點
48 }
49 int main(){
50     int t,m;
51     scanf("%d",&t);
52     for (int i=1; i<=t; i++) {
53         scanf("%d%d",&n,&m);
54         build(1, n, 1);
55         for (int j=0; j<m; j++) {
56             int x,y,v;
57             scanf("%d%d%d",&x,&y,&v);
58             update(x, y, 1, v);
59         }
60         printf("Case %d: The total value of the hook is %d.\n",i,tree[1].sum);
61     }
62     return 0;
63 }

HDU 1698 Just a Hook(線段樹成段更新)