您的位置:首页 > 其它

HDU 1698 Just a Hook(线段树延迟更新)

2016-09-27 12:48 267 查看
题意:一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。

思路:区间更新。

区间更新思路:   更新时,把无代表性的父节点标记成0,

 把要更新的父节点的旧状态甩给下一层,

 然后再向下更新就行。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=100000;
int tree[3*MAXN+10],v,st,en;
int ans;
void update(int root,int n_left,int n_right){
int mid=(n_left+n_right)/2;
if(n_left>=st&&n_right<=en){
tree[root]=v;
}else{
if(tree[root]){//把当前层的状态甩给下一层
tree[root*2]=tree[root*2+1]=tree[root];
tree[root]=0;
}
if(mid<st){
update(root*2+1,mid+1,n_right);
}else if(mid>=en){
update(root*2,n_left,mid);
}else{
update(root*2,n_left,mid);
update(root*2+1,mid+1,n_right);
}
}
return ;
}
void query(int root,int n_left,int n_right){
int mid=(n_left+n_right)/2;
if(tree[root]!=0){
ans+=(n_right-n_left+1)*tree[root];
return ;
}
query(root*2,n_left,mid);
query(root*2+1,mid+1,n_right);
return ;
}
int main()
{
int T;
cin>>T;
int cut=0;
while(T--){
cut++;
memset(tree,0,sizeof(tree));
int m,n;
cin>>n>>m;
st=1;en=n;v=1;
update(1,1,n);
while(m--){
scanf("%d%d%d",&st,&en,&v);
update(1,1,n);
}
ans=0;
query(1,1,n);
cout<<"Case "<<cut<<": The total value of the hook is "<<ans<<'.'<<endl;
}
}


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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: