【区间更新区间求和】HDU 1698 Just a Hook
acm.hdu.edu.cn/showproblem.php?pid=1698
【AC】
#include<cstdio>
const int maxn=;
#define lson (i<<1)
#define rson (i<<1|1)
struct Seg
{
int l,r,lazy,val;
}tree[maxn<<];
int val[maxn]; void push_up(int i)
{
tree[i].val=tree[lson].val+tree[rson].val;
} void push_down(int i)
{
if (tree[i].lazy==-) return;
tree[lson].lazy=tree[i].lazy;
tree[rson].lazy=tree[i].lazy;
tree[lson].val=tree[i].lazy*(tree[lson].r-tree[lson].l+);
tree[rson].val=tree[i].lazy*(tree[rson].r-tree[rson].l+);
tree[i].lazy=-;
} void build(int l,int r,int i=) //build a segment tree of length n, and index 1..n
{
tree[i].l=l;
tree[i].r=r;
tree[i].lazy=-;
if (l==r) tree[i].val=val[l];
else
{
int mid=l+r>>;
build(l,mid,lson);
build(mid+,r,rson);
push_up(i);
}
} void setval(int l,int r,int x,int i=)
{
if (tree[i].l==l && tree[i].r==r)
{
tree[i].lazy=x;
tree[i].val=x*(r-l+);
}
else
{
push_down(i);
int mid=tree[i].l+tree[i].r>>;
if (r<=mid) setval(l,r,x,lson);
else if (l>mid) setval(l,r,x,rson);
else
{
setval(l,mid,x,lson);
setval(mid+,r,x,rson);
}
push_up(i);
}
} int query(int l,int r,int i=)
{
if (tree[i].l==l && r==tree[i].r) return tree[i].val;
push_down(i);
int mid=tree[i].l+tree[i].r>>;
if (r<=mid) return query(l,r,lson);
if (l>mid) return query(l,r,rson);
return query(l,mid,lson)+query(mid+,r,rson);
} int main()
{
int t;
scanf("%d",&t);
int cas=;
while (t--)
{
int n;
scanf("%d",&n);
for (int i=;i<=n;i++) val[i]=;
build(,n);
int m;
scanf("%d",&m);
for (int i=;i<m;i++)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
setval(l,r,k);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,query(,n));
}
return ;
}
【区间更新区间求和】HDU 1698 Just a Hook的更多相关文章
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
- hdu6070(分数规划/二分+线段树区间更新,区间最值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...
- 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 (线段树区间更新)
题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是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(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...
- HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...
- HDU 1698 Just a Hook(线段树/区间更新)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description In the g ...
随机推荐
- MySQL备份还原介绍
window系统下 1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql ...
- Higher level thinking
「Higher level thinking」-- 出自 Ray Dalio 的<Principles>(PDF 原文:Principles by Ray Dalio) Higher le ...
- pathForResource获取资源为nil的原因
利用NSbundle获取 资源文件的时候,如果是自己添加的文件,获取的时候纵使返回nil的解决办法.原因是因为该文件没有添加到资源文件中,只要在添加文件的时候选择添加到 Create Folder R ...
- JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案
1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合. 执行 object.getBoundingClien ...
- webpack devserver的说明
一般我们在项目中 如果用webpack的话,基本都会用到webpack-dev-server,配置大致如下: devServer={ contentBase: basePath, historyApi ...
- python打开.pkl的文件并显示里面的内容
pkl文件是pyhthon里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西.正确的打开方式如下: import cPickle as pickle f = open('path') info ...
- C-基础:表达式中存在有符号类型和无符号类型时,都自动转换为无符号类型
void foo(void) { unsigned ; ; (a+b > ) puts("> 6") : puts("<= 6"); } 答案 ...
- python之道05
1.写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...
- Spring启动流程—源码解读
https://blog.csdn.net/yangliuhbhd/article/details/80790761 Spring的AbstractApplicationContext的refresh ...
- jquery操作滚动条滚动到指定元素位置 scrollTop
$('.brand_t a').bind('click',function(){ if($(this).attr('title1')){ var toChar = $(this).attr('titl ...