Just a Hook HDU

来源:互联网 发布:怎么用dede建站 编辑:程序博客网 时间:2024/05/16 09:37
 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,对其进行k次操作,每次操作为把l 到r区间的数全部修改成m.(修改成金 3银2铜1)然后算这n 个数的和。线段树区间更新问题。

#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{    int nsum;    int l,r;}tree[401000];int num[401000];void build(int i,int l,int r){    tree[i].l = l;    tree[i].r = r;    tree[i].nsum = 1;//先把整个区间都默认成铜 即为1    if(l == r)    {        return ;    }    int mid = (l + r)/2;    build(2*i,l,mid);    build(2*i+1, mid+1,r);}void updata(int i,int l,int r,int m)//整个区间更新成m{    if(tree[i].nsum == m) return ;//如果是相同的,就不用改了      if(tree[i].l == l&&tree[i].r == r)    {        tree[i].nsum = m;        return ;    }    if(tree[i].nsum !=-1)//该区间只有一种颜色    {        tree[i*2].nsum = tree[i*2+1].nsum = tree[i].nsum;//把他的所有子节点修改成和父结点一样的颜色        tree[i].nsum = -1;//该区间不再是纯色    }    int mid = (tree[i].l + tree[i].r)/2;    if(r<=mid)        updata(2*i,l,r,m);    else if(l>mid)        updata(2*i+1,l,r,m);    else    {        updata(2*i,l,mid,m);        updata(2*i+1,mid+1,r,m);    }}int Find(int i){    if(tree[i].nsum !=-1)//若该区间是纯色则算出其和,否则就寻找左右区间    return (tree[i].r - tree[i].l+1)*tree[i].nsum;    else    return Find(i*2)+Find(i*2+1);}int main(){    int x,y,z,T,k,n;    int count = 0;    scanf("%d",&T);    while(T--)    {        scanf("%d %d",&n,&k);        build(1,1,n);        count++;        while(k--)        {           scanf("%d %d %d",&x,&y,&z);           updata(1,x,y,z);        }        printf("Case %d: The total value of the hook is %d.\n",count,Find(1));    }    return 0;}
原创粉丝点击