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

区间更新重点在于懒惰标记。

当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r-l+1);col[rt] = c;后面的子区间就不管了,当你下次更新某一个区间的时候,把col[rt]从顶往下推(也没有推到底),推到合适的位置,刚好这个位置是我要更新的区间的子区间(可能被横跨了)就停下来。

在这里感谢网上的大牛,感谢杰哥,彬哥。我才能AC。

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27760    Accepted Submission(s):
13778

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.
 
Source
#include <stdio.h>
#include <algorithm> using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn = ; int sum[maxn<<];
int col[maxn<<]; void PushUp(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
} void PushDown(int rt,int m)
{
if(col[rt])
{
col[rt<<] = col[rt<<|] =col[rt]; //往下推
sum[rt<<] = (m-(m>>))*col[rt]; //子区间更新
sum[rt<<|] = (m>>)*col[rt];
col[rt] = ; //该点已经推了
}
} void build(int l,int r,int rt)
{
col[rt] = ;
sum[rt] = ;
if(l==r) return;
int m=(l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt] = c;
sum[rt] = c*(r-l+);
return ;
}
PushDown(rt,r-l+);
int m = (l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
PushUp(rt);
} int main()
{
int T,n,m;
scanf("%d",&T);
for(int cases=;cases<=T;cases++)
{
scanf("%d%d",&n,&m);
build(,n,);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",cases,sum[]);
}
return ;
}

HDU(1698),线段树区间更新的更多相关文章

  1. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  2. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 1698 (线段树 区间更新) Just a Hook

    有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...

  4. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  5. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  6. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  7. HDU 3016 线段树区间更新+spfa

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. hdu 1698 线段树 区间修改

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  9. Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

随机推荐

  1. idea(1)-idea初装

    1.安装插件 Alibaba Java Coding Guidelines Free Mybatis plugin MyBatis Log Plugin Lombok pluginGsonFormat ...

  2. Python 内置的GUI库tkinter方法在py2和py3中的更改

    参考资料:   https://docs.python.org/3.4/library/tkinter.html#tkinter-moduleshttps://docs.python.org/2.7/ ...

  3. 1.5 GO json转Map

    使用GO将show slave status查询返回的json串转为Map类型 package main import ( "encoding/json" "fmt&qu ...

  4. thinkPHP5.0表单令牌使用

    表单令牌的作用:避免表单的重复提交(如在tp5提交成功等待跳转页面刷新页面会在次提交表单) 原理:在初始化表单时,生成一个session标识‘token’,提交表单时将这个token一起提交过去,然后 ...

  5. RequireJS -Javascript模块化(二、模块依赖)

    上一篇文章中简单介绍了RequireJs的写法和使用,这节试着写下依赖关系 需求描述:我们经常写自己的js,在元素选择器这方面,我们可能会用jquery的$("#id")id选择器 ...

  6. 转 ogg issue

    1.http://www.dbdream.com.cn/2013/05/17/ogg-00446%E9%94%99%E8%AF%AF%E8%A7%A3%E5%86%B3/ OGG-00446错误解决 ...

  7. json处理第一篇:利用Jackson处理json

    利用Jackson处理json需要导入的jar包(2以上版本的): <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.c ...

  8. jquery mobile - select and input - horizontal - in same line

    控件组合的水平布局 select + input 在同一行 注意jquery mobile 的js 和css 的版本, 一些低版本 估计不支持 <!DOCTYPE html> <ht ...

  9. aop动态代理 事务 threadlocal

    第一:package com.itheima.utils; import java.sql.Connection; import java.sql.SQLException; /** * 处理事务 的 ...

  10. 牛客网Java刷题知识点之输入流、输出流、字节流、字符流、字节流的抽象基类(InputStream、OutputStream)、字符流的抽象基类(Reader、Writer)、FileWriter、FileReader

    不多说,直接上干货! IO流用来处理设备之间的数据传输. java对数据的操作是通过流的方式. java用于操作流的对象都在IO包中. IO流按操作数据分为两种:字节流和字符流. IO流按流向分为:输 ...