博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
阅读量:5276 次
发布时间:2019-06-14

本文共 3552 字,大约阅读时间需要 11 分钟。

Just a Hook
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit     

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.
 
 
题目大意:给你T组测试数据,每组有n,m表示n个棒,每个棒起始都是铜棒,值为1。银棒为2,金棒为3。现在要让区间ui,vi内的棒变为wi类型的棒,问你最后棒的总值是多少。
解题思路:延迟标记。首先在区间更新时,应该保证要更新的那段区间内的结点的值是正确的,然后做延迟标记。在下放标记的时候,应该保证下放后的结点的值是正确的。这样在询问的时候,才能向上推出正确的结果。
 
 
#include
#include
#include
using namespace std;#define mid (L+R)/2#define lson rt*2,L,mid#define rson rt*2+1,mid+1,Rconst int maxn = 120000;struct SegTree{ int val; int lazy;}segs[maxn*4];void PushUp(int rt){ segs[rt].val = segs[rt*2].val + segs[rt*2+1].val;}void PushDown(int rt,int L,int R){ if(segs[rt].lazy){ segs[rt*2].lazy = segs[rt].lazy; segs[rt*2+1].lazy = segs[rt].lazy; segs[rt*2].val = (mid - L+1)*segs[rt].lazy; segs[rt*2+1].val = (R-mid) * segs[rt].lazy; } segs[rt].lazy = 0;}void buildtree(int rt,int L,int R){ segs[rt].lazy = 0; if(L == R){ segs[rt].val = 1; return ; } buildtree(lson); buildtree(rson); PushUp(rt);}void Update(int rt,int L,int R,int l_ran,int r_ran,int _v){ if(l_ran <= L && R <= r_ran){ segs[rt].val = _v * (R-L+1); segs[rt].lazy = _v; return; } PushDown(rt,L,R); if(l_ran <= mid){ Update(lson,l_ran,r_ran,_v); } if(r_ran > mid){ Update(rson,l_ran,r_ran,_v); } PushUp(rt);}int main(){ int T, n, m, cas = 0; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); buildtree(1,1,n); int u,v,w; for(int i = 1; i <= m; i++){ scanf("%d%d%d",&u,&v,&w); Update(1,1,n,u,v,w); } printf("Case %d: The total value of the hook is %d.\n",++cas,segs[1].val); } return 0;}

  

转载于:https://www.cnblogs.com/chengsheng/p/4392266.html

你可能感兴趣的文章
JavaScript 变量
查看>>
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>
《TCP/IP 详解 卷一》读书笔记 -----第四章 ARP
查看>>
C# Stream 和 byte[] 之间的转换
查看>>
OMG: daily scrum nine
查看>>
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>