hdu1698 Just a Hook (线段树,区间更新)

来源:互联网 发布:淘宝店铺认证在哪里 编辑:程序博客网 时间:2024/05/21 17:41

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.
题意:给你n长度的钩子,每单位的初始值为1,然后m个操作,将(l,r)之间的值变为v,求最后n长度的值的和。
思路:因为是连续区间更新,如果在该区间没找到,更新的时候先将子节点的值与父节点相等,然后把父节点的值设为-1,表示该区间里面的值不统一,我们在输出查找就方便了,当区间不为-1,就能直接计算出该区间的值
代码:

#include<cstdio>using namespace std;int map[100005];struct node{   int l,r,w;}str[300005];void build(int l,int r,int n){    str[n].l=l,str[n].r=r,str[n].w=1;    if(l==r)      return;    int temp=(l+r)/2;    build(l,temp,2*n);    build(temp+1,r,2*n+1);}void update(int l,int r,int w,int n){    if(str[n].l==l&&str[n].r==r)    {        str[n].w=w;        return;    }    if(str[n].w!=-1)    {        str[2*n].w=str[2*n+1].w=str[n].w;        str[n].w=-1;    }    int temp=(str[n].l+str[n].r)/2;    if(r<=temp)        update(l,r,w,2*n);    else if(l>temp)        update(l,r,w,2*n+1);    else    {        update(l,temp,w,2*n);        update(temp+1,r,w,2*n+1);    }}int find(int n){    if(str[n].w!=-1)    {        return (str[n].r-str[n].l+1)*str[n].w;    }    else    {        return find(2*n)+find(2*n+1);    }}int main(){    int T;    scanf("%d",&T);    for(int t=1;t<=T;t++)    {        int n;        scanf("%d",&n);        build(1,n,1);        int p;        scanf("%d",&p);        while(p--)        {            int a,b,w;            scanf("%d%d%d",&a,&b,&w);            update(a,b,w,1);        }        printf("Case %d: ",t);        printf("The total value of the hook is %d.\n",find(1));    }}
0 0
原创粉丝点击