【区间更新区间求和】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 ...
随机推荐
- 浅谈table和DIV网页布局
DIV+CSS是网站标准(或称“WEB标准”)中常用的术语之一,通常为了说明与HTML网页设计语言中的表格(table)定位方式的区别,因为XHTML网站设计标准中,不再使用表格定位技术,而是采用DI ...
- Web开发者必须知道的10个jQuery代码片段
在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...
- springboot-i18n国际化
简介 In computing, internationalization and localization are means of adapting computer software to di ...
- shell脚本,alias别名命令用法。
[root@localhost ~]# alias alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' [root@localhost ~]# [ro ...
- 初涉trie
trie:字符串算法中的重要“数据结构” 什么是trie trie就是利用字符串的公共前缀所建成的树. 众所周知树是有很多很好的性质的,于是trie可以结合其他知识点做一些有趣的事情. trie的例题 ...
- js函数arguments与获取css样式方法
函数传参,当参数的个数不定时,可以使用arguments:表示实参集合 arguments.length=实参个数 获得css样式方法: getComputedStyle()方法---->得到的 ...
- 日志logging
日志: 日志分为5个级别:debug(10),info(20),warning(30),error(40),critical(50) 日志四个组成部分:logger,handler,filter,fo ...
- web开发框架之Django基础
在脚本中如何进行Django的运行 if __name__ == '__main__': import os import django # 注意路径(当前所在的位置,要加载Django的配置文件) ...
- python爬虫入门七:pymysql库
我们使用python爬取得到的数据,有时候会数据量特别大,需要存入数据库. 需要注意的是,MySQL是一种关系型数据库管理系统,利用MySQL可以对数据库进行操作,而MySQL并不是一个数据库. 而p ...
- ios开发中关闭textview控件的虚拟键盘
在ios开发中,textfield控件在点击的时候出现虚拟键盘,关掉虚拟键盘可以通过虚拟键盘中的done button和点击view中的任意地方来关闭虚拟键盘. 1.第一种方法是textfield控件 ...