博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
阅读量:4327 次
发布时间:2019-06-06

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

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

11021 5 25 9 3

Sample Output

Case 1: The total value of the hook is 24. 题意:给一个长度n初始值为1,每次把区间[x,y]上的值变成z,求[1,n]上所有的值的和。 思路:用线段树进行更新给的区间,但是发现如果更新到底的话会TLE。这时就用到了,成段更新和懒惰标记。成段更新是更新到当前需要的区间即可,记下区间的结果和子节点要更新的值。懒惰标记是当需要对子节点进行更改时,就把他往下进行更改。 AC代码:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int maxn=100000+10;10 int ans;11 struct note12 {13 int l,r,ans,lazy;14 } a[maxn<<2];15 int build(int l,int r,int k,int d)16 {17 if(l==r)18 {19 a[k].l=l;20 a[k].r=r;21 a[k].lazy=0;22 a[k].ans=d;23 return 0;24 }25 a[k].l=l;26 a[k].r=r;27 a[k].lazy=0;28 int mid=(l+r)/2;29 build(l,mid,k*2,d);30 build(mid+1,r,2*k+1,d);31 a[k].ans=a[2*k+1].ans+a[k*2].ans;32 return 0;33 }34 int pushdown(int k)//懒惰标记对子节点进行更新35 {36 if(a[k].lazy)37 {38 a[k*2].ans=(a[k*2].r-a[k*2].l+1)*a[k].lazy;39 a[k*2+1].ans=(a[k*2+1].r-a[k*2+1].l+1)*a[k].lazy;40 a[k*2].lazy=a[k*2+1].lazy=a[k].lazy;41 a[k].lazy=0;42 }43 return 0;44 }45 int ins(int l,int r,int k,int d)46 {47 if(a[k].l==l&&a[k].r==r)//进行懒惰标记和成段更新48 {49 a[k].ans=(r-l+1)*d;50 a[k].lazy=d;51 return 0;52 }53 pushdown(k);54 int mid=(a[k].l+a[k].r)/2;55 if(mid
=r) ins(l,r,k*2,d);57 else58 {59 ins(l,mid,2*k,d);60 ins(mid+1,r,k*2+1,d);61 }62 a[k].ans=a[k*2].ans+a[k*2+1].ans;63 ans=a[k].ans;//树顶a[1].ans即为结果64 return 0;65 }66 int main()67 {68 int t,n,m,a,b,x;69 while(~scanf("%d",&t))70 {71 for(int k=1; k<=t; k++)72 {73 ans=0;74 scanf("%d",&n);75 build(1,n,1,1);76 scanf("%d",&m);77 for(int i=0; i
View Code

 

转载于:https://www.cnblogs.com/wang-ya-wei/p/6022693.html

你可能感兴趣的文章
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>