hdu 1689 Just a Hook
来源:互联网 发布:西门子编程线 编辑:程序博客网 时间:2024/06/08 11:59
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.
【题意】 就是给你N个数,再给你Q条指令,每条指令给X,Y,Z三个值,意思是将[X,Y]区间内的数都替换成Z,最后求出总区间的和。
【解题思路】 一道简单的区间更新,用laz标记,无需更新到叶子结点,不然会超时。因为只需要总区间的和,此题也不用写求和函数,用线段树,写法写着写着就过了。话不多说,上代码!
【AC代码】
#include<stdio.h>#define fff 100005using namespace std;struct node{ int l,r,len; int laz,sum;}tr[fff*4];void pushup(int id){ tr[id].sum=tr[id*2].sum+tr[id*2+1].sum;}void pushdown(int id){ if(tr[id].laz) { tr[id*2].laz=tr[id].laz; tr[id*2+1].laz=tr[id].laz; tr[id*2].sum=tr[id*2].len*tr[id].laz; //因为是区间替换,所以写=而不写+= tr[id*2+1].sum=tr[id*2+1].len*tr[id].laz; tr[id].laz=0; }}void build(int id,int l,int r){ tr[id].l=l; tr[id].r=r; tr[id].len=r-l+1; tr[id].laz=0; if(l==r) { tr[id].sum=1; return; } int mid=(r+l)/2; build(id*2,l,mid); build(id*2+1,mid+1,r); pushup(id);}void update(int id,int l,int r,int x){ if(tr[id].l==l&&tr[id].r==r) { tr[id].laz=x; tr[id].sum=x*tr[id].len; return; } pushdown(id); int mid=(tr[id].l+tr[id].r)/2; if(l>mid) update(id*2+1,l,r,x); else if(r<=mid) update(id*2,l,r,x); else { update(id*2,l,mid,x); update(id*2+1,mid+1,r,x); } pushup(id);}int main(){ int t,n,q,a,b,c,cas; scanf("%d",&t); for(cas=1;cas<=t;cas++) { scanf("%d%d",&n,&q); build(1,1,n); while(q--) { scanf("%d%d%d",&a,&b,&c); update(1,a,b,c); } printf("Case %d: The total value of the hook is %d.\n",cas,tr[1].sum); //只求总和,所以直接用根节点的sum值就行 } return 0;}
线段树最基础的写法,若有什么改进的地方,还请多多指教。
- hdu 1689 Just a Hook
- hdu 1689 Just a Hook
- hdu Just a Hook
- HDU Just a Hook
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook (HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- HDU 1689 Just a Hook (线段树)
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- Hdu 1698 Just a Hook
- Unity资源路径及加载外部资源介绍
- 《Java困惑》:多并发情况下HashMap是否还会产生死循环
- NKOI 3732 wjj的01序列
- python打印文档字符串
- String类
- hdu 1689 Just a Hook
- 如何旋转recovery界面:恢复出厂设置界面和Fota升级界面
- 【数据结构与算法】汉诺塔递归
- Linux中的信号(2)
- Protobuf解析失败,_extensions_ 中的数据 却成了 _unknown_fields
- 简单点,switch-case的结构简单点~ C语言学习(4)
- 约瑟夫问题
- Centos7 安装 matplotlib问题
- 【杭电4907】Task schedule