(HDU 1698) Just a Hook(线段树)-附通用模板

Just a Hook

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


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.
 
 
线段树的模板题,因为以前线段树的模板总是有各种问题,今天重新写了一发,顺带交了这道题
线段树模板:
#define maxn 100010

struct node
{
    int l,r;
    int k;//标记值
    int m;//存储值
};

node tree[maxn];

void build(int n ,int l,int r)
{
    tree[n].l=l;
    tree[n].r=r;
    if(l==r)
    {

        tree[n].m=0;//////////////////////////视情况
        return;
    }
    int mid=(l+r)/2;
    build(n*2,l,mid);
    build(n*2+1,mid+1,r);
    tree[n].m=tree[n*2].m+tree[n*2+1].m;/////////////////////看线段树种类
}

void pushup(int n)
{
    tree[n].m=tree[n*2].m+tree[n*2+1].m;////////////////////看线段树种类
}

void pushdown(int n,int m)
{
    if(tree[n].k)
    {
        tree[n*2].k+=tree[n].k;
        tree[n*2+1].k+=tree[n].k;

        tree[n*2].m+=tree[n].k*(m-m/2);////////////////////看线段树种类
        tree[n*2+1].m+=tree[2*n+1].k*(m/2);////////////////////看线段树种类

        tree[n].k=0;//更新后还原
    }
}

void update(int n,int l,int r,int c)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        if(l!=r)
            tree[n].k+=c;/////////////////////看线段树种类
        tree[n].m+=c*(r-l+1);
        return;
    }
    if(l==r)
        return ;
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;
    if(r<=mid)
        update(n*2,l,r,c);
    else if(l>=mid+1)
        update(n*2+2,l,r,c);
    else
    {
        update(n*2,l,mid,c);
        update(n*2+1,mid+1,r,c);
    }
    pushup(n);
}

int query(int n,int l,int r)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        return tree[n].m;
    }
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;

    int res=0;
    if(r<=mid)
        res+=query(n*2,l,r);
    else if(l>=mid+1)
        res+=query(n*2+1,l,r);
    else
    {
        res+=query(n*2,l,mid);////////////////////看线段树种类
        res+=query(n*2+1,mid+1,r);////////////////////看线段树种类
    }
    return res;

}

 

 

 

AC代码:

 

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define maxn 100010

using namespace std;

struct node
{
    int l,r;
    int k;//标记值
    int m;//存储值
};

node tree[maxn*4];

void pushup(int n)
{
    tree[n].m=tree[n*2].m+tree[n*2+1].m;////////////////////看线段树种类
}

void build(int n ,int l,int r)
{
    tree[n].l=l;
    tree[n].r=r;
    tree[n].k=0;
    if(l==r)
    {
        tree[n].m=1;//////////////////////////视情况
        return;
    }
    int mid=(l+r)/2;
    build(n*2,l,mid);
    build(n*2+1,mid+1,r);
    pushup(n);
}



void pushdown(int n,int m)
{
    if(tree[n].k)
    {
        tree[n*2].k=tree[n].k;
        tree[n*2+1].k=tree[n].k;

        tree[n*2].m=tree[n].k*(m-m/2);////////////////////看线段树种类
        tree[n*2+1].m=tree[2*n+1].k*(m/2);////////////////////看线段树种类

        tree[n].k=0;//更新后还原
    }
}

void update(int n,int l,int r,int c)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        if(l!=r)
            tree[n].k=c;/////////////////////看线段树种类
        tree[n].m=c*(r-l+1);
        return;
    }
    if(tree[n].l==tree[n].r)
        return ;
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;
    if(r<=mid)
        update(n*2,l,r,c);
    else if(l>=mid+1)
        update(n*2+1,l,r,c);
    else
    {
        update(n*2,l,mid,c);
        update(n*2+1,mid+1,r,c);
    }
    pushup(n);
}

int query(int n,int l,int r)
{
    if(tree[n].l==l&&tree[n].r==r)
    {
        return tree[n].m;
    }
    pushdown(n,tree[n].r-tree[n].l+1);
    int mid=(tree[n].r+tree[n].l)/2;

    int res=0;
    if(r<=mid)
        res+=query(n*2,l,r);
    else if(l>=mid+1)
        res+=query(n*2+1,l,r);
    else
    {
        res+=query(n*2,l,mid);
        res+=query(n*2+1,mid+1,r);
    }
    return res;

}

int main()
{
    int t;
    scanf("%d",&t);
    for(int kase=1;kase<=t;kase++)
    {
        int n;
        scanf("%d",&n);
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(1,a,b,c);
        }
        printf("Case %d: The total value of the hook is %d.\n",kase,tree[1].m);
    }
    return 0;
}

 

 

 

posted @ 2018-03-03 14:20  海哥天下第一  阅读(186)  评论(0编辑  收藏  举报