HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值。
分析:lazy标记主要体现在update上。
当l <= L && R <= r时,该结点的子结点值不再更新,取而代之的是给该结点一个lazy值,以记录下来该结点的子结点并没有更新。
当赋值的区间落在子结点上时,才将lazy标记传递,同时更新子结点相应的sum值。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int sum[MAXN << 2];
int lazy[MAXN << 2];
void build(int id, int L, int R, int v){
if(L == R){
sum[id] = v;
}
else{
int mid = L + (R - L) / 2;
build(id << 1, L, mid, v);
build(id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
void pushdown(int id, int L, int R){
if(lazy[id]){
lazy[id << 1] = lazy[id << 1 | 1] = lazy[id];
int mid = L + (R - L) / 2;
sum[id << 1] = (mid - L + 1) * lazy[id];
sum[id << 1 | 1] = (R - mid) * lazy[id];
lazy[id] = 0;
}
}
void update(int l, int r, int id, int L, int R, int v){
if(l <= L && R <= r){
sum[id] = (R - L + 1) * v;
lazy[id] = v;
}
else{
pushdown(id, L, R);
int mid = L + (R - L) / 2;
if(l <= mid) update(l, r, id << 1, L, mid, v);
if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
int main(){
int T;
scanf("%d", &T);
int kase = 0;
while(T--){
memset(sum, 0, sizeof sum);
memset(lazy, 0, sizeof lazy);
int N;
scanf("%d", &N);
build(1, 1, N, 1);
int Q;
scanf("%d", &Q);
while(Q--){
int X, Y, Z;
scanf("%d%d%d", &X, &Y, &Z);
update(X, Y, 1, 1, N, Z);
}
printf("Case %d: The total value of the hook is %d.\n", ++kase, sum[1]);
}
return 0;
}
HDU - 1698 Just a Hook (线段树---区间修改)的更多相关文章
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
随机推荐
- 树莓派4B踩坑指南 - (5)设置阿里云的源及解决apt提示依赖
解决树莓派apt升级/安装提示依赖问题: 注意!!buster是根据系统版本(cat /etc/os-release)来写的,如果是jessie或者stretch要改为buster.参考解决树莓派ap ...
- Linux centosVMware 自动化运维认识自动化运维、启动salt相关服务、saltstack配置认证、salt-key命令用法、saltstack远程执行命令、saltstack - grains、saltstack – pillar
一.认识自动化运维 传统运维效率低,大多工作人为完成 传统运维工作繁琐,容易出错 传统运维每日重复做相同的事情 传统运维没有标准化流程 传统运维的脚本繁多,不能方便管理 自动化运维就是要解决上面所有问 ...
- [swscaler @ ...] deprecated pixel format used, make sure you did set range correctly
我自己在使用如下函数进行转换时报的错 int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int sr ...
- uniGUI之uniPanel(20)
1]uniPanel常用设置: 2]多个uniPanel在一个uniPanel里显示 uniPanel常用设置: 2]多个uniPanel在一个uniPanel里显示 uniPanel0.Alignm ...
- Hive的存储和MapReduce处理——数据清洗
日期:2019.11.13 博客期:115 星期三 Result文件数据说明: Ip:106.39.41.166,(城市) Date:10/Nov/2016:00:01:02 +0800,(日期) D ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个小按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- B. Light bulbs
B. Light bulbs There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are o ...
- Hadoop端口与界面
NameNode:7180 Cloudera Manager集群管理界面: NameNode:50070 NameNode Web UI/数据管理界面: NameNode:8020/9000 Ha ...
- sentinel控制台
下载sentinel源码包:https://github.com/alibaba/Sentinel/tree/master,根据自己需要下载不同版本的分支,博主下载得是1.6 下载后解压,然后进入se ...
- R语言作图 绘制中国地图
参考:https://zhuanlan.zhihu.com/p/27360411 第一步.下载shapefile文件 一直都没有找到下载地址,死在了第一步 第二步.导入shp文件 第三步.画图