(hdu 1698) Just a Hook(區間更新)

feng_zhiyu發表於2017-08-10

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

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
2008 “Sunline Cup” National Invitational Contest

題意:一個鉤有銅,銀,金三種棍子,重分別為1,2,3
給定n,q,開始[1,n]內都是銅棍,經過q次[x,y]內的更新後,問鉤多重?

分析:區間更新,直接把[x,y]內的數值 置為 z
注意輸出的標點符號!!! 找這種bug 可以讓自己懷疑題目錯了。。。 噗

1341MS 5972K 1675 B C++

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
const LL N=1e5+5;
LL sum[N<<2],a[N],MAX[N<<2],ad[N<<2];
void pushup(LL rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    // MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void pushdown(int rt,int m)
{
    if(ad[rt])
    {
        ad[rt<<1]=ad[rt];
        ad[rt<<1|1]=ad[rt];
        sum[rt<<1]=ad[rt]*(m-(m>>1));
        sum[rt<<1|1]=ad[rt]*(m>>1);
        ad[rt]=0;
    }
}
void build(LL l,LL r,LL rt)
{
    ad[rt]=0;
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    LL m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(LL L,LL R,LL c,LL l,LL r,LL rt)
{
    if(L<=l&&R>=r)
    {
        ad[rt]=c;
        sum[rt]=c*(r-l+1);
        return ;
    }
    pushdown(rt,r-l+1);
    LL m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt);
}
LL query(LL L,LL R,LL l,LL r,LL rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    LL m=(l+r)>>1;
    LL ret=0;
    if(L<=m) ret+=query(L,R,lson);
    if(R>m) ret+=query(L,R,rson);
    return ret;
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        build(1,n,1);
        for(int i=0;i<q;i++)
        {
            LL x,y,z;
            scanf("%lld%lld%lld",&x,&y,&z);
            update(x,y,z,1,n,1);
        }
        printf("Case %d: The total value of the hook is %lld.\n",cas++,query(1,n,1,n,1));
    }
    return 0;
}

相關文章