码迷,mamicode.com
首页 > 其他好文 > 详细

E - Just a Hook HDU 1698 (线段树+类似延迟标记)

时间:2015-04-05 09:12:34      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:


E - Just a Hook
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice

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.


Sample Input

1
10
2
1 5 2
5 9 3



Sample Output

Case 1: The total value of the hook is 24.



//类似延迟标记一样的思路

//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100011;
const int inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,n) for(int i=0;i<(n);i++)
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32);
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return x;
}
template<class T> inline void read_(T&x,T&y)
{
    read(x);
    read(y);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------IO template------
typedef long long LL;

struct node
{
    int sum;
    int val;
} p[maxn<<3];

void build(int rt,int L,int R)
{
    p[rt].val=0;
    if(L==R)
    {
        p[rt].sum=1;
        p[rt].val=1;
        return;
    }
    build(lson);
    build(rson);
    p[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum;
}

void update(int rt,int L,int R,int x,int y,int z)
{
   // printf("%d %d %d %d %d %d\n",rt,L,R,x,y,z);
    if(x==L&&y==R)
    {
        p[rt].sum=(R-L+1)*z;
        p[rt].val=z;
        return ;
    }
    if(p[rt].val>0)//向下更新时,判断条件写好,
    {
        p[rt<<1].sum=(M-L+1)*p[rt].val;
        p[rt<<1|1].sum=(R-M)*p[rt].val;//右儿子 区间是R-M即R-(M+1)+1 
        p[rt<<1].val=p[rt<<1|1].val=p[rt].val;
        p[rt].val=0;
    }
    if(y<=M)
        update(lson,x,y,z);
    else if(x>M)
        update(rson,x,y,z);
    else
    {
        update(lson,x,M,z);
        update(rson,M+1,y,z);
    }
    p[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum;
}

int main()
{
//#ifndef ONLINE_JUDGE
  // freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
    int T;
    int n,m,i,j,k,t;
    read(T);
    int cas=1;
    while(T--)
    {
        read_(n,m);
        build(1,1,n);
        int x,y,z;
        while(m--)
        {
            read_(x,y);
            read(z);
            update(1,1,n,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",cas++,p[1].sum);
    }
    return 0;
}





E - Just a Hook HDU 1698 (线段树+类似延迟标记)

标签:

原文地址:http://blog.csdn.net/u013167299/article/details/44874513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!