现在的位置: 首页 > 综合 > 正文

hdu1698Just a Hook (线段树 成段更新,这题目真是坑,按题目意思开的数组还小)

2018年02月22日 ⁄ 综合 ⁄ 共 2958字 ⁄ 字号 评论关闭
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.
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 200000
struct stick
{
    int sum,z,v;//sum为当前段的总和,z为stick的种类,v为当前段的stick种类是否单一
}node[10*N];
int p,q,z;
void set_tree(int l,int r,int k)//以z=1,每个点种类单一,一开始建线段树,l和r是第k段的左端点和右端点
{
    int m=(l+r)/2;
    node[k].sum=r-l+1;
    node[k].z=1;
    node[k].v=1;
    if(l==r) return ;
    set_tree(l,m,2*k); set_tree(m+1,r,2*k+1);
}
//-----------更新段p~q段的值-------------
void chang_p_to_q(int l,int r,int k)//计更新算出第k个段的值,l和r是第k段的左端点和右端点
{
    int m=(l+r)/2;
    if(node[k].z==z) return ;//表明第k段不用更新
    if(p<=l&&r<=q)//当第k段在修改的范围内时
    {
        node[k].z=z; node[k].v=1;//第k段的stick为z,stick种类单一
        node[k].sum=z*(r-l+1);//计算第k段的总值
        //printf("%d~%d:%d(z=%d) ",l,r,node[k].sum,z);
       return ;
    }
    if(node[k].v){//当第k个段stick的种类单一时,左右子树也一定单一,必定会先更新左右子树
         node[2*k].z=node[2*k+1].z=node[k].z; //左右子树
         node[2*k].v=node[2*k+1].v=1; //左右子树的stick种类单一
         node[2*k].sum=(m-l+1)*node[2*k].z;//计算第k段的左子树的总值
         node[2*k+1].sum=(r-m)*node[2*k+1].z;//计算第k段的右子树的总值
    }
    if(q<=m){ //要 更新的段 在左子树
        node[k*2+1].sum=node[k].sum-node[2*k].sum;//先计算出右子树的总值
        chang_p_to_q(l,m,2*k); //计算出左子树的值
    }
    else if(p>m){ //要 更新的段 在右子树
        node[k*2].sum=node[k].sum-node[2*k+1].sum;
        chang_p_to_q(m+1,r,2*k+1);
    }
    else { //要 更新的段 在左右子树
        chang_p_to_q(l,m,2*k);
        chang_p_to_q(m+1,r,2*k+1);
    }
    node[k].v=0;//第k段的stick种类不单一
    node[k].sum=node[k*2].sum+node[k*2+1].sum;//计算第k段的总值
    //printf("%d~%d:%d(%d,%d,z=%d) ",l,r,node[k].sum,node[k*2].sum,node[2*k+1].sum,node[k].z);
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d%d",&n,&m);
        set_tree(1,n,1);
        while(m--)
        {
            scanf("%d%d%d",&p,&q,&z);
            chang_p_to_q(1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,node[1].sum);
    }
}

抱歉!评论已关闭.