hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)

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.
题意:给一个长度n初始值为1,每次把区间[x,y]上的值变成z,求[1,n]上所有的值的和。
思路:用线段树进行更新给的区间,但是发现如果更新到底的话会TLE。这时就用到了,成段更新和懒惰标记。成段更新是更新到当前需要的区间即可,记下区间的结果和子节点要更新的值。懒惰标记是当需要对子节点进行更改时,就把他往下进行更改。
AC代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 #include<map>
 8 using namespace std;
 9 const int maxn=100000+10;
10 int ans;
11 struct note
12 {
13     int l,r,ans,lazy;
14 } a[maxn<<2];
15 int build(int l,int r,int k,int d)
16 {
17     if(l==r)
18     {
19         a[k].l=l;
20         a[k].r=r;
21         a[k].lazy=0;
22         a[k].ans=d;
23         return 0;
24     }
25     a[k].l=l;
26     a[k].r=r;
27     a[k].lazy=0;
28     int mid=(l+r)/2;
29     build(l,mid,k*2,d);
30     build(mid+1,r,2*k+1,d);
31     a[k].ans=a[2*k+1].ans+a[k*2].ans;
32     return 0;
33 }
34 int pushdown(int k)//懒惰标记对子节点进行更新
35 {
36     if(a[k].lazy)
37     {
38         a[k*2].ans=(a[k*2].r-a[k*2].l+1)*a[k].lazy;
39         a[k*2+1].ans=(a[k*2+1].r-a[k*2+1].l+1)*a[k].lazy;
40         a[k*2].lazy=a[k*2+1].lazy=a[k].lazy;
41         a[k].lazy=0;
42     }
43     return 0;
44 }
45 int ins(int l,int r,int k,int d)
46 {
47     if(a[k].l==l&&a[k].r==r)//进行懒惰标记和成段更新
48     {
49         a[k].ans=(r-l+1)*d;
50         a[k].lazy=d;
51         return 0;
52     }
53     pushdown(k);
54     int mid=(a[k].l+a[k].r)/2;
55     if(mid<l) ins(l,r,k*2+1,d);
56     else if(mid>=r) ins(l,r,k*2,d);
57     else
58     {
59         ins(l,mid,2*k,d);
60         ins(mid+1,r,k*2+1,d);
61     }
62     a[k].ans=a[k*2].ans+a[k*2+1].ans;
63     ans=a[k].ans;//树顶a[1].ans即为结果
64     return 0;
65 }
66 int main()
67 {
68     int t,n,m,a,b,x;
69     while(~scanf("%d",&t))
70     {
71         for(int k=1; k<=t; k++)
72         {
73             ans=0;
74             scanf("%d",&n);
75             build(1,n,1,1);
76             scanf("%d",&m);
77             for(int i=0; i<m; i++)
78             {
79                 scanf("%d%d%d",&a,&b,&x);
80                 ins(a,b,1,x);
81             }
82             printf("Case %d: The total value of the hook is %d.
",k,ans);
83         }
84     }
85     return 0;
86 }
View Code
原文地址:https://www.cnblogs.com/wang-ya-wei/p/6022693.html