HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和)
题意分析
一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和
线段树维护区间和 。
建树的时候初始化为1,更新区间时候放懒惰标记,下推标记更新区间和。
由于是替换,不是累加,所以更新的时候不是+=,而是直接=。
注意这点就可以了,然后就是多组数据注意memset,因为这个WA几发。
代码总览
#include <bits/stdc++.h>
#define maxn 200010
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
int Sum[maxn<<2],Lazy[maxn<<2];
int A[maxn];
void PushUp(int rt){Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];}
int N,Q,lq,rq,val;
void Build(int l,int r,int rt){
if(l==r) {
Sum[rt] = 1;
return;
}
int m=(l+r)>>1;
Build(l,m,rt<<1);
Build(m+1,r,rt<<1|1);
PushUp(rt);
}
void UpdatePoint(int L,int C,int l,int r,int rt){
if(l==r){
Sum[rt]+=C;
return;
}
int m=(l+r)>>1;
if(L <= m) UpdatePoint(L,C,l,m,rt<<1);
else UpdatePoint(L,C,m+1,r,rt<<1|1);
PushUp(rt);
}
void PushDown(int rt,int ln,int rn){
if(Lazy[rt]){
Lazy[rt<<1] = Lazy[rt];
Lazy[rt<<1|1] = Lazy[rt];
Sum[rt<<1] = Lazy[rt]*ln;
Sum[rt<<1|1] = Lazy[rt]*rn;
Lazy[rt]=0;
}
}
void UpdateInterval(int L,int R,int C,int l,int r,int rt){
if(L <= l && r <= R){
Sum[rt] = C*(r-l+1);
Lazy[rt] = C;
return ;
}
int m=(l+r)>>1;
PushDown(rt,m-l+1,r-m);
if(L <= m) UpdateInterval(L,R,C,l,m,rt<<1);
if(R > m) UpdateInterval(L,R,C,m+1,r,rt<<1|1);
PushUp(rt);
}
int Query(int L,int R,int l,int r,int rt){
if(L <= l && r <= R){
return Sum[rt];
}
int m = (l+r)>>1;
PushDown(rt,m-l+1,r-m);
int ANS = 0;
if(L <= m) ANS += Query(L,R,l,m,rt<<1);
if(R > m) ANS += Query(L,R,m+1,r,rt<<1|1);
return ANS;
}
int main()
{
//freopen("in.txt","r",stdin);
int TestCase;
scanf("%d",&TestCase);
for(int i = 1;i<=TestCase;++i){
scanf("%d",&N);
memset(Lazy,0,sizeof Lazy);
Build(1,N,1);
scanf("%d",&Q);
for(int j = 0;j<Q;++j){
scanf("%d %d %d",&lq,&rq,&val);
UpdateInterval(lq,rq,val,1,N,1);
}
int ans =Query(1,N,1,N,1);
printf("Case %d: The total value of the hook is %d.\n",i,ans);
}
return 0;
}
HDU.1689 Just a Hook (线段树 区间替换 区间总和)的更多相关文章
- HDU 1754 I Hate It(线段树单点替换+区间最值)
I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu1754(线段树单点替换&区间最值模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...
- R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数
R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...
- 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(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [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 , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- Django的简介
一.MTV模型 Django的MTV模式: Model(模型):和数据库相关的.负责业务对象与数据库的对象(ORM) Template(,模板):放所有的HTML文件 模板语法:目的是将变量(数据库内 ...
- VR电竞游戏在英特尔®架构上的用户体验优化
作为人与虚拟世界之间的新型交互方式,VR 能够让用户在模拟现实中获得身临其境的感受.但是,鉴于 VR 的帧预算为每帧 11.1ms (90fps),实现实时渲染并不容易,需要对整个场景渲染两次(一只眼 ...
- Open vSwitch for CentOS
原文发表于cu:2016-06-02 本文属于重发,ovs当前的安装方式可能略有不同. 参考文档: 官方文档: http://openvswitch.org/support/dist-docs-2.5 ...
- MySQL case when 使用
case when 自定义排序时的使用 根据 case when 新的 sort字段排序 case when t2.status = 4 and t2.expire_time>UNIX_TIME ...
- 2018-2019-20172321 《Java软件结构与数据结构》第四周学习总结
2018-2019-20172321 <Java软件结构与数据结构>第四周学习总结 教材学习内容总结 第六章 6.1列表集合 列表集合是一种概念性表示法,其思想是使事物以线性列表的方式进行 ...
- HDU 5617 Jam's maze dp+滚动数组
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- HDU 5642 King's Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
- Ubuntu命令行安装显卡驱动
1. sudo apt-et purge nvidia* 卸载原有驱动 2. sudo add-apt-repository ppa:graphics-drivers sudo apt-get upd ...
- Java多线程中的wait与notify
一.wait: 1. wait 是 object 类的方法, sleep 是 thread 类的方法. 2. 当前的正在我这个对象访问的线程 wait. 3. 当前的这个线程, 锁定在当前对象的这个线 ...
- JQuery EasyUI 引用加载分析
easyui是什么,就不介绍了,接触到前端的就算没用过,肯定也应该听说过.其次,本文不是介绍它提供如calendar.tree等这些功能如何使用的,这些官网上介绍都很详细,中文的网上也不少.本文是从e ...