HDU 1698 Just a Hook (线段树的区间更新)

来源:互联网 发布:在线考试数据库设计 编辑:程序博客网 时间:2024/06/06 16:26
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,代表1-n内所有点值都为1,给q个操作数,x,y,z,表示把x,y内所有值改为z,q个操作完了求总区间和,区间更新基本题,练习入门lazy tag的好题

代码:

#include<iostream>#include<stdio.h>#include<map>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include<string>#include<cstring>using namespace std;struct node{    int l,r;    int tag;//上一次更新的值    int v;//当前区间的总和}t[100005*4];void Build(int l,int r,int k){    t[k].l=l;    t[k].r=r;    t[k].tag=0;    if(l==r)    {        t[k].v=1;        return;    }    int mid=(l+r)/2;    Build(l,mid,k*2);    Build(mid+1,r,k*2+1);    t[k].v=t[k*2].v+t[k*2+1].v;}void pushdown(int k)//将上次没有传递的状态传递下去{    if(t[k].tag!=0)    {        t[k*2].tag=t[k*2+1].tag=t[k].tag;//传给子标签        t[k*2].v=(t[k*2].r-t[k*2].l+1)*t[k].tag;//赋值        t[k*2+1].v=(t[k*2+1].r-t[k*2+1].l+1)*t[k].tag;        t[k].tag=0;//传完了置为0    }}void update(int l,int r,int v,int k){    if(l==t[k].l&&r==t[k].r)//找到了该区间,赋值,标记,返回    {        t[k].v=(r-l+1)*v;        t[k].tag=v;        return;    }    pushdown(k);    int mid=(t[k].l+t[k].r)/2;    if(r<=mid)        update(l,r,v,k*2);    else if(l>mid)        update(l,r,v,k*2+1);    else    {        update(l,mid,v,k*2);        update(mid+1,r,v,k*2+1);    }    t[k].v=t[k*2].v+t[k*2+1].v;//向上传递更新值}int main(){    int i,j,n,test,m,x,y,v;    scanf("%d",&test);    for(i=1;i<=test;i++)    {        scanf("%d",&n);        Build(1,n,1);        scanf("%d",&m);        for(j=0;j<m;j++)        {            scanf("%d%d%d",&x,&y,&v);            update(x,y,v,1);        }        printf("Case %d: The total value of the hook is %d.\n",i,t[1].v);    }    return 0;}


原创粉丝点击