Just a Hook (区间更新,lazy标记)

来源:互联网 发布:linux 拷贝后重命名 编辑:程序博客网 时间:2024/06/05 02:19
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.


#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;const int maxn=1e5+10;struct node{    int l,r;    int sum_val;    int tag,lazy;}tree[maxn*4];void build(int st,int ed,int now_id){    tree[now_id].l=st;    tree[now_id].r=ed;    tree[now_id].tag=0,tree[now_id].lazy=0;//标记是否未释放,应该释放多少    if(st==ed)//叶子上才能填充值    {         tree[now_id].sum_val=1;         return;    }    int mid=(st+ed)>>1;    build(st,mid,now_id<<1);    build(mid+1,ed,now_id<<1|1);    tree[now_id].sum_val=tree[now_id<<1].sum_val+tree[now_id<<1|1].sum_val;//对数值进行加和操作}void update(int st,int ed,int val,int now_id){    if(tree[now_id].l==st&&tree[now_id].r==ed)//说明正好到了我们要更新的区间,我们就可以停在这里,lazy一下    {        tree[now_id].sum_val=(ed-st+1)*val;        tree[now_id].lazy=1, tree[now_id].tag=val;        return;    }    int mid=(tree[now_id].l+tree[now_id].r)>>1;//这是当前数的mid,我们那个st和ed是不变的    if(tree[now_id].lazy==1)//如果这个点有懒惰标记,那我们就去先释放,要不然,这个值不下去,下面的值    {                        //上次应该更新的可是没有更新,所以我们先释放一下        tree[now_id].lazy=0;        update(tree[now_id].l,mid,tree[now_id].tag,now_id<<1);//本来就懒,所以就只是走了一步,不会说直接到了叶子节点,        update(mid+1,tree[now_id].r,tree[now_id].tag,now_id<<1|1);//这里是更新树,就是之前的数据,不是我们这次的数据        tree[now_id].tag=0;//当前树的节点值已经释放,所以重新写为0    }    if(ed<=mid)        update(st,ed,val,now_id<<1);    else if(st>mid)        update(st,ed,val,now_id<<1|1);    else    {        update(st,mid,val,now_id<<1);        update(mid+1,ed,val,now_id<<1|1);    }    tree[now_id].sum_val=tree[now_id<<1].sum_val+tree[now_id<<1|1].sum_val;}int main(){  int T;  scanf("%d",&T);  int cnt=1;  while(T--)  {      int n;      scanf("%d",&n);      build(1,n,1);      int time;      scanf("%d",&time);      while(time--)      {          int a,b,x;          scanf("%d %d %d",&a,&b,&x);          update(a,b,x,1);      }      printf("Case %d: The total value of the hook is %d.\n",cnt++,tree[1].sum_val);  }  return 0;}


阅读全文
0 0
原创粉丝点击