`
hellojyj
  • 浏览: 58823 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

HDU 1698 Just a Hook

    博客分类:
  • ACM
阅读更多

原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1698

 

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17134    Accepted Submission(s): 8555


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.
 

 

Sample Input
1
10
2
1 5 2
5 9 3
 

 

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

 

Source
 

 

Recommend
wangye   |   We have carefully selected several similar problems for you:  1166 1542 1394 2795 1255 
 
分析:用线段树,区间更新。
 
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MAXN 100010

int sum[4*MAXN],lazy[4*MAXN];
int t,n,op,x,y,z,total;

void build(int r,int L,int R){
    if(L==R) sum[r] = 1;
    else{
        int M = (L+R)/2;
        build(r*2,L,M);
        build(r*2+1,M+1,R);
        sum[r] = sum[r*2] + sum[r*2+1];
    }
}
void push_up(int r,int L,int R)
{
    if(lazy[r]>0)
        sum[r] = lazy[r]*(R-L+1);
    else
        sum[r] = sum[r*2] + sum[r*2+1];
}

void push_down(int r)
{
    if(lazy[r]>0)
    {
        lazy[r*2] = lazy[r*2+1] = lazy[r];
        lazy[r] = 0;
    }
}
void update(int r,int L,int R,int x,int y,int z)
{
    if(x<=L&&R<=y)
    {
        lazy[r] = z;
        sum[r] = z*(R-L+1);
    }
    else
    {
        push_down(r);
        int  M = (L+R)/2;
        if(x<=M) update(r*2,L,M,x,y,z);
        else push_up(r*2,L,M);
        if(y>M) update(r*2+1,M+1,R,x,y,z);
        else push_up(r*2+1,M+1,R);
        push_up(r,L,R);
    }
}

void query(int r, int L,int R,int ql,int qr)
{
    if(lazy[r]>0)
        total+=lazy[r]*(min(qr,R)-max(ql,L)+1);
    else if(ql<=L&&R<=qr)
        total += sum[r];
    else
    {
        int M = (L+R)/2;
        if(ql<=M)
            query(r*2,L,M,ql,qr);
        if(qr>M)
            query(r*2+1,M+1,R,ql,qr);
    }
}

int main()
{
    scanf("%d",&t);
    int k = 1;
    while(t--)
    {
        memset(lazy,0,sizeof(lazy));
        lazy[1] = 1;
        total = 0;
        scanf("%d%d",&n,&op);
        build(1,1,n);
        while(op--)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(1,1,n,x,y,z);
        }
        query(1,1,n,1,n);
        printf("Case %d: The total value of the hook is %d.\n",k++,total);
    }
    return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics