您的位置:首页 > 其它

HDU 1698 Just a Hook【线段树—区间更新】

2017-03-23 17:51 393 查看

                                                            Just a Hook

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

Total Submission(s): 30587    Accepted Submission(s): 15087


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 
[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

1
10
2
1 5 2
5 9 3

 

[align=left]Sample Output[/align]

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

 

[align=left]Source[/align]
2008 “Sunline Cup” National Invitational Contest

题意:

一个胖子有三种钩子每种钩子价值不同,现在要改变其中一系列钩子的规格,求最后所有钩子的价值总和。

思路:

总体来说这道题在线段树中并不是一个难题,针对线段树的区间更新所出的一道水题,不过 输出的时候格式要和题目上一样,我就是少复制一个“ . ”一直wa。让我纠结了半天。。。细节,细节,细节  重要的事情说三遍。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=100005;
int tree[maxn*3];
int add[maxn*3];
void build(int l,int r,int rt){
add[rt]=0;
if(l==r){
tree[rt]=1;
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int rt,int m){//标记下移
if(add[rt]){
add[rt<<1]=add[rt];
tree[rt<<1]=add[rt<<1]*(m-(m>>1));
add[rt<<1|1]=add[rt];
tree[rt<<1|1]=add[rt<<1|1]*(m>>1);
add[rt]=0;
}
}
void update(int p,int q,int l,int r,int rt,int c){//区间更新
if(p<=l&&q>=r){//如果这个节点在更新区间范围内
add[rt]=c;
tree[rt]=c*(r-l+1);//这里重点叶子结点更新为c  则父节点为他们的总和,
return;
}
pushdown(rt,r-l+1);
int m=(r+l)>>1;
if(p<=m)//更新左节点在中值前 更新l_m;找交集
update(p,q,l,m,rt<<1,c);
if(q>m)//更新有节点在中值后 更新m+1_r;找交集
update(p,q,m+1,r,rt<<1|1,c);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];//更新根节点
}

int main(){
int t,i,j,k;
scanf("%d",&t);
for(i=1;i<=t;i++){
int n;
memset(add,0,sizeof(add));
memset(tree,0,sizeof(tree));
scanf("%d",&n);
build(1,n,1);
scanf("%d",&k);
while(k--){
int l,r,z;
scanf("%d%d%d",&l,&r,&z);
update(l,r,1,n,1,z);
}
printf("Case %d: The total value of the hook is %d.\n",i,tree[1]);//最后的那个点一定不要少。
}
return 0;
}


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