【线段树成段更新-模板】【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 ...
随机推荐
- 解决 VM虚拟机网卡无法拉起 的问题
复制虚拟机后,第二块网卡始终无法正常运作,表现为无法自动获得dhcp地址,且重启后与第一块网卡使用同样的地址,让人苦笑不得,反复重启了很多次都是这样,后面想到ifcfg-eth1的配置文件可能写得太简 ...
- iOS开发-CGAffineTransformMakeRotation改变了中心解决的方法
坑爹的. 为了这个问题折腾了2个小时. 恼.. 今天在写一个时钟demo的时候, 时针的旋转用到了CGAffineTransformMakeRotation, 按理说. 图像的旋转是以图像本身的中心 ...
- MVC4中 jquery validate 不用submit方式验证表单或单个元素
正确引入MVC4 jquery验证的相关文件 <script src="/Scripts/jquery-1.4.4.js"></script> <sc ...
- .Net 中DataSet导出为excel的方法
依旧是留下代码防止以后忘记 protected void Export_Click(object sender, EventArgs e) { DataSet data = "" ...
- C#关于params的用法(使用数量可变的参数)
有些方法需要传递个数不定的值进行运算.比如求最小值的方法.除了用容器外,还可以使用params来做 例子如下: using System; using System.Collections.Gener ...
- CSS3实现图片鼠标悬浮放大效果
.excerpt .focus a img{ -webkit-transition: all ease .3s; transition: all ease .3s }.excerpt .focus a ...
- Hortonworks 用于做 Sentimental Analysis的Hiveddl.sql 文件
The hiveddl.sql script has performed the following steps to refine the data: Converted the raw Twitt ...
- 时间的获取和转换,C#和Sql
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- Android性能优化建议
1.减少View树的高度(多层嵌套) 2.使用<include>重用layout 3.使用<ViewStub>实现View的延迟加载 作用范围:当这个布局在初始化加载时候,不需 ...
- iframe自适应高度的多种方法方法小结(转)
对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的 ...