POJ 3468 A Simple Problem with Integers
来源:互联网 发布:淘宝有哪些美食 编辑:程序博客网 时间:2024/06/05 15:05
题目链接:点击打开链接
区间修改区间查询,练习lazy标记模板题。
lazy标记在更新和下放的时候也是要+=到下面的,而不是直接=赋值,没注意到这点wa了几次。
代码:
#include <iostream>#include <cstdio>#include <cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define MAX 100010#define ll long longusing namespace std;ll sum[MAX<<2];ll col[MAX<<2];void pushup(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void pushdown(int rt,int len){ if(col[rt]){ col[rt<<1]+=col[rt]; col[rt<<1|1]+=col[rt]; sum[rt<<1]+=(len-(len>>1))*col[rt]; sum[rt<<1|1]+=(len>>1)*col[rt]; col[rt]=0; }}void build(int l,int r,int rt){ col[rt]=0; if(l==r){ scanf("%lld",&sum[rt]); return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}void update(int L,int R,ll k,int l,int r,int rt){ if(L<=l&&R>=r){ sum[rt]+=(r-l+1)*k; col[rt]+=k; return ; } pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m) update(L,R,k,lson); if(R>m) update(L,R,k,rson); pushup(rt);}ll query(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r){ return sum[rt]; } pushdown(rt,r-l+1); ll res=0; int m=(l+r)>>1; if(L<=m) res+=query(L,R,lson); if(R>m) res+=query(L,R,rson); pushup(rt); return res;}int main(){ int N,Q; while(~scanf("%d%d",&N,&Q)){ build(1,N,1); for(int i=1;i<=Q;i++){ char c[4]; int a,b; ll k; scanf("%s",&c); if(c[0]=='C') { scanf("%d%d%lld",&a,&b,&k); update(a,b,k,1,N,1); } else{ scanf("%d%d",&a,&b); printf("%I64d\n",query(a,b,1,N,1)); } } } return 0;}
0 0
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- quick-cocos2d-x游戏开发【5】——创建菜单
- 学习pthreads,使用条件变量进行多线程之间的同步
- STL vector中的push_back方法(17)
- 正则表达式笔记 3 贪婪、勉强、独占三种匹配量词
- 【UML】UML视频总结——实战前的思考
- POJ 3468 A Simple Problem with Integers
- oracle RAC data guard 理论基础
- 基于大数据分析的网络攻击检测
- cxf快速发布webService之aegis
- va_start(), va_arg(), va_end() 宏定义的解释
- Jquery的模拟事件,input模拟focus
- 正则表达式笔记 4 分支条件 逻辑操作符 |
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- Partition List