【区间更新区间求和】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 导出一句话
听说是很老的东西了,学习的时候发现还是很好用的,故学习转载过来,留备学习. mysql 导出一句话 方法1:网上流行的方法 流程:(1)建表--->(2)插入数据--->(3)select ...
- SnowKiting 2017/1/24
原文 Let's go fly a kite...in the snow Your snowkiting checklist To snowkite safely,you'll need a litt ...
- search bar 自定义背景
//修改搜索框背景 self.searchCarKeyWord.backgroundColor=[UIColorclearColor]; //去掉搜索框背景 //1. [[self.searchCar ...
- 10048 - Audiophobia (Floyd)
Floyd的变形,本质是动态规划,路径分成的两个部分中取最大值作为该路径的答案,在所有可行路径之中选一个最小值. #include<bits/stdc++.h> using namespa ...
- ping 不通。无法访问目标主机
台式机 使用无线网卡 又登录了VPN 有时候访问不了局域网内的主机 解决方案 添加路由即可 window+R 打开运行 输入cmd 然后输入 route add 10.16.1.89 10.13 ...
- ucosii(2.89) 在Lpc1765移植中定时器的使用。
1,lpc1765的systicker register是24bit, cpu 频率64Mhz时候,注意不要设置systicker 的值超过24bit. 2, 使用timer 的callback函数, ...
- ubuntu 16.04 安装node.js 8.x
引自 https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04#how-to-in ...
- SniperOJ-leak-x86-64
参考:1.借助DynELF实现无libc的漏洞利用小结 2.一步一步学ROP之linux_x64篇 - 蒸米 题目源码 #include <stdio.h> #include <un ...
- jquery动态实现填充下拉框
当点下拉框时动态加载后台数据. 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) ...
- Broadcast BCM94322 用ubuntu修改ID
1.按这个教程的6楼做的http://bbs.pcbeta.com/viewthread-1324168-1-1.html.注意我先下载 的是ubuntu9.05版本,做U盘启动进live 模式,43 ...