HDU 1698 Just a Hook(线段树区间更新)
2015-08-08 22:14
501 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
注意这个题目要输出的结果是整个区间的,因此不需要query函数,直接输出1节点的值。
注意这个题目要输出的结果是整个区间的,因此不需要query函数,直接输出1节点的值。
#include<cstdio> #include<algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define root 1,n,1 const int maxn=100000+5; int add[maxn<<2]; int S[maxn<<2]; void push_up(int rt) { S[rt]=S[rt<<1]+S[rt<<1|1]; } void push_down(int rt,int dis) { if(add[rt]) { add[rt<<1]=add[rt]; add[rt<<1|1]=add[rt]; S[rt<<1]=add[rt]*(dis-(dis>>1)); S[rt<<1|1]=add[rt]*(dis>>1); add[rt]=0; } } void build(int l,int r,int rt) { add[rt]=0; S[rt]=1; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson); push_up(rt); } void update(int L,int R,int d,int l,int r,int rt) { if(L<=l&&r<=R) { add[rt]=d; S[rt]=(r-l+1)*d; return ; } push_down(rt,r-l+1); int m=(l+r)>>1; if(L<=m) update(L,R,d,lson); if(m<R) update(L,R,d,rson); push_up(rt); } int main() { int t,i; scanf("%d",&t); for(i=1;i<=t;i++) { int n,q; scanf("%d%d",&n,&q); build(root); while(q--) { int x,y,z; scanf("%d%d%d",&x,&y,&z); update(x,y,z,root); } printf("Case %d: The total value of the hook is %d.\n",i,S[1]); } return 0; }
相关文章推荐
- druid 数据源的配置
- python从一种时间字符串格式转换到另一种时间字符串格式
- 可能用到的函数
- HashMap使用
- 20150805训练解题报告
- string 小部分用法归类(初学)
- iOS 用GCD下载网络图片方法
- linux下supervisor无法开机启动
- linux之uname命令
- iOS开发(Objective-C)常用库索引
- Implement Stack using Queues
- GCDMulticastDelegate多播委托
- 设计模式--装饰模式
- android平台的技术架构
- MongoDB基本用法(增删改高级查询、mapreduce)
- 卡尔曼滤波融合库函数+Arduino实例
- iOS开发概述-13.UI控件图层属性CALayer
- gpio驱动开发第一天
- iOS程序启动的完整过程
- HTML文档中应用css样式的方法总结