【hdu】1698 just a hook 中文题意&题解

Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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.Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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.

题意:给一个序列,初始值都是1,有多组测试数据,每个数据序列长度为n,有m个操作,每个操作给出x,y,c,表示把[x,y]区间的值改为c,最后询问序列总和
输出格式为:
Case_1:_The_total_value_of_the_hook_is_num
下划线是空格

题解:很明显的线段树,为了省时间,修改操作应该用lazy标记,表示某个区间的值都被改为lazy,树的细节很多,详见代码

push_up函数是统计子叶的值总和
push_down函数是把lazy值传递给子叶
build函数是初始化
注意任何时候tre[id].v都表示区间的值之和

代码如下

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#define lson id*2
#define rson id*2+1
#define yy int mid=(l+r)/2
using namespace std;
struct node{int v;int lazy;} tre[900000];
inline void push_up(int id)
{
        tre[id].v=tre[lson].v+tre[rson].v;return ;
}
inline void push_down(int id,int l,int r)
{
        if (l>r) return ;
        if (tre[id].lazy==0)    return ;
        yy;
        tre[lson].lazy=tre[id].lazy;
        tre[rson].lazy=tre[id].lazy;
        tre[lson].v=tre[lson].lazy*(mid+1-l);
        tre[rson].v=tre[rson].lazy*(r-mid);
        tre[id].lazy=0;
        return ;
}
int ans;
inline void add(int id,int l,int r,int L,int R,int val)
{
        if (l>r || L>R) return ;
        if (l>=L && r<=R)
        {
                tre[id].lazy=val;
                tre[id].v=val*(r-l+1);
                return ;
        }
        yy;
        push_down(id,l,r);
        if (L<=mid)     add(lson,l,mid,L,R,val);
        if (R>=mid+1)   add(rson,mid+1,r,L,R,val);
        push_up(id);

}
inline void que(int id,int l,int r,int L,int R)
{
        if (l>r || L>R) return ;
        if (l>=L && R>=r)
        {
                ans+=tre[id].v;
                return ;
        }
        yy;
        push_down(id,l,r);
        if (L<=mid)     que(lson,l,mid,L,R);
        if (R>=mid+1)   que(rson,mid+1,r,L,R);
        push_up(id);
        return;

}



inline void build(int id,int l,int r)
{
        if (l>r) return ;
        if (l==r)       {tre[id].lazy=0;tre[id].v=1;return ;}
        yy;
        tre[id].lazy=0;
        build(lson,l,mid);
        build(rson,mid+1,r);
        push_up(id);
        return ;

}

int a,b,c,d,n,m,t;



int main()
{
cin>>t;
int num=0;
while(num<t)
{
num++;
scanf("%d",&n);

build(1,1,n);

scanf("%d",&m);
for (int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(1,1,n,a,b,c);
}
cout<<"Case "<<num<<": The total value of the hook is "<<tre[1].v<<"."<<endl;
}
}

猜你喜欢

转载自blog.csdn.net/williamcode/article/details/51130247
今日推荐