hdu 1698 Just a Hook 线段树求解

来源:互联网 发布:js定义空的二维数组 编辑:程序博客网 时间:2024/05/14 03:56

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
11021 5 25 9 3
 

Sample Output

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

题意:有 t 组测试数据,n 为钩子长度(1<=n<=100000),m 为操作的次数。初始时,每个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。

使用 lazy 懒惰标记来节省时间

注意:建树部分

void build_tree(int id,int l,int r)
{
tree[id].lazy=0;//      因为要多次建树,所以初始化时不能仅仅更新叶节点的lazy 要将lazy全部清零
if (l>=r)
{
tree[id].val=1;
return;
}

int mid=(l+r)/2;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
push_up(id);
return ;
}
另外,注意输入输出格式

具体代码实现:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
struct edge{
int val;
int lazy;
}tree[800005];
int n,ans=0,m,t;
void push_up(int id)
{
tree[id].val=tree[lson].val+tree[rson].val;
return ;
}
void push_down(int id,int l,int r)
{
int mid=(l+r)/2;
if (tree[id].lazy==0) return ;
tree[lson].val=tree[id].lazy*(mid-l+1);
tree[rson].val=tree[id].lazy*(r-mid);
tree[lson].lazy=tree[id].lazy;
tree[rson].lazy=tree[id].lazy;
tree[id].lazy=0;
return ;
}
void build_tree(int id,int l,int r)
{
tree[id].lazy=0;
if (l>=r)
{
tree[id].val=1;
return;
}
int mid=(l+r)/2;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
push_up(id);
return ;
}
void add_tree(int id,int l,int r,int L,int R,int v)
{
if (l>=L&&r<=R)
{
tree[id].val=(r-l+1)*v;
tree[id].lazy=v;
return ;
}
int mid=(l+r)/2;
push_down(id,l,r);
if (mid>=L)
add_tree(lson,l,mid,L,R,v);
if (mid+1<=R)
add_tree(rson,mid+1,r,L,R,v);
push_up(id);
return ;
}
void query_tree(int id,int l,int r,int L,int R)
{
if (l>=L&&r<=R)
{
ans+=tree[id].val;
return ;
}
int mid=(l+r)/2;
push_down(id,l,r);

if (mid>=L) query_tree(lson,l,mid,L,R);
if (mid+1<=R) query_tree(rson,mid+1,r,L,R);

push_up(id);
return ;
}
int main()
{
cin>>t;
int num=0;
while(num<t)
{
num++;
scanf("%d",&n);
build_tree(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_tree(1,1,n,a,b,c);
}
cout<<"Case "<<num<<": The total value of the hook is "<<tree[1].val<<"."<<endl;
}
}
2 0
原创粉丝点击