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

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698

题目:

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.

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

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.

思路:

懒惰标记法,延迟更新。没学过这个知识点的,可以先学习一下。网上有篇介绍该知识点的博客:http://blog.csdn.net/zip_fan/article/details/46775633

代码:

 #include <cstdio>
const int N=4e5;
struct node{
int l,r;
int lazy,sum;
}tree[N];
int n;
void pushup(int i){//更新父节点
tree[i].sum=tree[(i*)+].sum+tree[i*].sum;
}
void pushdown(int i){//更新子节点
if(tree[i].lazy){
tree[*i].lazy=tree[*i+].lazy=tree[i].lazy;//将子节点也懒惰标记
tree[*i].sum=(tree[*i].r-tree[*i].l+)*tree[*i].lazy;//sum会等于长度值*标记值
tree[*i+].sum=(tree[*i+].r-tree[*i+].l+)*tree[*i+].lazy;
tree[i].lazy=;//更新完,取消该节点的标记
}
}
void build(int bg,int ed,int i){
if(i>*n) return;
tree[i].l=bg;
tree[i].r=ed;
tree[i].lazy=;//多个测试样例,注意初始化
if (bg == ed) tree[i].sum=;
else{
int mid=(bg+ed)/;
build(bg, mid, *i);
build(mid+, ed, *i+);
pushup(i);//回溯更新父节点 }
}
void update(int bg,int ed,int i,int v){
if(bg<=tree[i].l && tree[i].r<=ed){
tree[i].lazy=v;
tree[i].sum=(tree[i].r-tree[i].l+)*v;
return ;
}
pushdown(i);//当用到该节点时,就向下更新
int mid=(tree[i].r+tree[i].l)/;
if(ed<=mid) update(bg, ed, *i, v);//该区间完全在左子树
else if(bg>mid) update(bg, ed, *i+, v);//该区间完全在右子树
else if(bg<=mid && ed>mid){//既在左子树又在右子树
update(bg, mid, *i, v);
update(mid+, ed, *i+, v);
}
pushup(i);//回溯更新父节点
}
int main(){
int t,m;
scanf("%d",&t);
for (int i=; i<=t; i++) {
scanf("%d%d",&n,&m);
build(, n, );
for (int j=; j<m; j++) {
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
update(x, y, , v);
}
printf("Case %d: The total value of the hook is %d.\n",i,tree[].sum);
}
return ;
}
上一篇:hdu 1698 线段树成段更新


下一篇:项目中通过Sorlj获取索引库中的数据