【线段树成段更新-模板】【HDU1698】Just a Hook
题意 Q个操作,将l,r 的值改为w
问最后1,n的sum 为多少
成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候
题意:O(-1)
思路:O(-1)
线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息)
线段:1-N(题目天然给出)
区间和性质:求sum
重点在于如何设计一个lazy操作
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=100000+5;
int CASE;
using namespace std;
int N,Q;
int tree[maxn*4];
int col[maxn*4]; //延迟标记
void Pushup(int rt) //向上更新
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(int l,int r,int rt) //与以往差不多,注意延迟标记的重新赋值,有了build 一般不用memset初始化了
{
col[rt]=0;
if(l==r) {tree[rt]=1;return 0;}
int m=(l+r)/2;
build(lson);
build(rson);
Pushup(rt);
}
void Pushdown(int rt,int k) //k是长度
{
if(col[rt])
{
col[rt<<1]=col[rt<<1|1]=col[rt];
tree[rt<<1]=(k-(k>>1))*col[rt];
tree[rt<<1|1]=(k>>1)*col[rt];
col[rt]=0;
}
}
int update(int L,int R,int c,int l,int r,int rt) {
if (L<=l&&r<= R) {
col[rt]=c; //不同题目处理方式不同,这里不属于下放操作,属于更新操作,与下放没有任何冲突关系
tree[rt]=c*(r-l+1);
return 0;
}
Pushdown(rt,r-l+1); //Lazy 下放
int m=(l+r)>>1; //下面与以往一样
if (L<=m) update(L,R,c,lson);
if (R>m) update(L,R,c,rson);
Pushup(rt);
}
void solve()
{
int ll,rr,w;
for(int i=1;i<=Q;i++)
{
scanf("%d%d%d",&ll,&rr,&w);
update(ll,rr,w,1,N,1);
}
printf("Case %d: The total value of the hook is %d.\n",CASE,tree[1]);
}
int main()
{
int T;
cin>>T;
CASE=0;
while(T--)
{
CASE++;
cin>>N>>Q;
build(1,N,1);
solve();
}
return 0;
}
【线段树成段更新-模板】【HDU1698】Just a Hook的更多相关文章
- 线段树 [成段更新] HDU 1698 Just a Hook
成段更新,需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候. 此处建议在纸上模拟一遍. Problem Descript ...
- 线段树成段更新模板POJ3468 zkw以及lazy思想
别人树状数组跑几百毫秒 我跑 2500多 #include<cstdio> #include<map> //#include<bits/stdc++.h> #inc ...
- 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. T ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
随机推荐
- 事关Animation Tree的工作随笔(一)
最近的业务上,又回到Animation Tree这块了. 众所周知的是Animation Tree这些概念已经提出很久了,但是使用有着AT支持的CE引擎的项目,却依然义无反顾地没有使用AT,而且,连某 ...
- Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation
问题:Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation 1. Windows-& ...
- Multiscale Combinatorial Grouping 学习和理解源代码(一)
目标探测由于所做的最新研究.因此,这一领域的一般阅读文章.发现这篇文章,效果是比较新的比较好.在如此仔细研究.贴纸和共享.下面已经发布若干个连续的,分别对论文和代码进行大致地介绍,最后依据自己的实验对 ...
- Effective C++:条款35:考虑virtual函数以外的其它选择
游戏中的人物伤害值计算问题. (一)方法(1):一般来讲能够使用虚函数的方法: class GameCharacter { public: virtual int healthValue() cons ...
- winform —— 常用控件
1.textbox: 属性:text: 文本selectedtext: 获或设置选中文本canundo: 是否能够撤销 passwordchar:替换字符实现密码 ...
- js 进阶笔记
JS中substr和substring的用法和区别 substr和substring都是JS截取字符串函数,两者用法很相近, substr方法 返回一个从指定位置开始的指定长度的子字符串. strin ...
- JQ点击高亮显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 今天进行了一次IOS面试,分享一下面试结果
IOS开发工程师岗位职责:1.负责移动产品IOS版客户端软件开发:2.可根据需求独立完成客户端软件的设计和开发;3.日常工作包括手机软件系统开发.单元测试.维护以及文档编写:不定期的公司内部培训.任职 ...
- hdu2393Higher Math
Problem Description You are building a house. You’d prefer if all the walls have a precise right ang ...
- js编码规范
使用统一的 编码规范 编写代码能提高JS代码的可读性,利于后期的维护和扩展,利于团队开发. 引用规范: 1.采用<script>...</script>方式引入 *.js 文件 ...