HDU-1698-Just a Hook-区间更新+线段树成段更新

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. 

InputThe 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.


题意:
  
首先给出数据的组数1,然后给出个数10,代表n从1——10,初始值全是1。再给出2,代表接下来有两个区间,1 5 2代表把区间[1,5]里面所有元素的值变为2,以此类推。求区间总和


思路:
   
区间更新,算是线段树的成段更新,因为要把这一段区间内的所有元素全部变成相同的。写的时候注意lazy的标记向下传递的操作即可

   还有就是最后输出的时候可以不用去查询,看了别人的博客才知道可以直接输出sum[1],因为所有的子节点和都是向上传递的,最后最顶上的那个元素记录的就是整个大区间的和。

  1 #include<iostream>
  2 #include<string.h>
  3 #include<cmath>
  4 #include<stdio.h>
  5 #include<algorithm>
  6 #include<stdlib.h>
  7 #include<iomanip>
  8 #define inf 0x3f3f3f3f
  9 #define ios() std::ios::sync_with_stdio(false)
 10 #define cin() cin.tie(0)
 11 #define cout() cout.tie(0)
 12 #define mem1(a) memset(a,0,sizeof(a))
 13 #define mem2(b) memset(b,'\0',sizeof(b))
 14 typedef long long ll;
 15 const int N=100020;
 16 using namespace std;
 17 
 18 
 19 //struct node
 20 //{
 21 //    int left;
 22 //    int right;
 23 //    int co;
 24 //}sum[N*4];
 25 
 26 int sum[4*N];
 27 int lazy[4*N];
 28 
 29 void pushdown(int i,int len)
 30 {
 31     if(lazy[i])
 32     {
 33         lazy[i<<1]=lazy[i];
 34         lazy[i<<1|1]=lazy[i];
 35         sum[i<<1]=(len-(len>>1))*lazy[i];
 36         sum[i<<1|1]=(len>>1)*lazy[i];
 37         lazy[i]=0;
 38     }
 39 }
 40 
 41 void build(int L,int R,int i)
 42 {
 43 //    s[i].left=L;
 44 //    s[i].right=R;
 45     lazy[i]=0;
 46     if(L==R)
 47     {
 48         sum[i]=1;
 49         return;
 50     }
 51     int mid=(L+R)/2;
 52     build(L,mid,i<<1);
 53     build(mid+1,R,i<<1|1);
 54     sum[i]=sum[i<<1]+sum[i<<1|1];
 55 }
 56 
 57 void update(int left,int right,int change,int L,int R,int i)
 58 {
 59     if(left<=L&&right>=R)
 60     {
 61         lazy[i]=change;
 62         sum[i]=change*(R-L+1);
 63         return;
 64     }
 65     pushdown(i,R-L+1);
 66     int mid=(R+L)/2;
 67     if(left<=mid)
 68         update(left,right,change,L,mid,i<<1);
 69     if(right>mid)
 70         update(left,right,change,mid+1,R,i<<1|1);
 71      sum[i]=sum[i<<1]+sum[i<<1|1];
 72 }
 73 
 74 int query(int left,int right,int L,int R,int i)
 75 {
 76         if(L==R)
 77             return sum[i];
 78         pushdown(i,R-L+1);
 79         int mid=(R+L)/2;
 80         int ans=0;
 81         if(left<=mid)
 82             ans+=query(left,right,L,mid,i<<1);
 83         if(mid<R)
 84             ans+=query(left,right,mid+1,R,i<<1|1);
 85         sum[i]=sum[i<<1]+sum[i<<1|1];
 86         return ans;
 87 }
 88 
 89 int main()
 90 {
 91     int t,len,m;
 92     scanf("%d",&t);
 93     int tt=1;
 94     while(t--)
 95     {
 96         mem1(sum);
 97         mem1(lazy);
 98         scanf("%d %d",&len,&m);
 99         build(1,len,1);
100         int a,b,change;
101         for(int i=1;i<=m;i++)
102         {
103             scanf("%d %d %d",&a,&b,&change);
104             update(a,b,change,1,len,1);
105         }
106         printf("Case %d: The total value of the hook is %d.\n",tt++,query(1,len,1,len,1));
107     }
108     return 0;
109 }

 

转载于:https://www.cnblogs.com/OFSHK/p/11291766.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值