数据结构--线段树--lazy延迟操作
来源:互联网 发布:网络硬盘服务器软件 编辑:程序博客网 时间:2024/06/05 15:31
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
//更新某一段区域的时候,采用延迟标记~~#include "cstdio" //poj 3468 lazy操作#include "cstring"#include "iostream"using namespace std;#define N 100005#define LL long longstruct node{ int x,y; LL sum; LL add; //记录以当前节点为根节点的树中需要增加的值}a[3*N];void Build(int t,int x,int y){ a[t].x = x; a[t].y = y; a[t].sum = a[t].add = 0; if(a[t].x == a[t].y) //到了叶子节点 { scanf("%lld",&a[t].sum); return ; } int mid = (a[t].x + a[t].y)/2; Build(t<<1,x,mid); Build(t<<1|1,mid+1,y); a[t].sum = a[t<<1].sum + a[t<<1|1].sum;}void Push_down(int t) //将add(增值)向下推一级{ LL add = a[t].add; a[t<<1].add += add; a[t<<1|1].add += add; a[t<<1].sum += add*(a[t<<1].y-a[t<<1].x+1); a[t<<1|1].sum += add*(a[t<<1|1].y-a[t<<1|1].x+1); a[t].add = 0;}LL Query(int t,int x,int y){ if(a[t].x==x &&a[t].y==y) return a[t].sum; Push_down(t); int mid = (a[t].x + a[t].y)/2; if(y<=mid) return Query(t<<1,x,y); if(x>mid) return Query(t<<1|1,x,y); else return Query(t<<1,x,mid) + Query(t<<1|1,mid+1,y);}void Add(int t,int x,int y,int k){ if(a[t].x==x && a[t].y==y) //不在推到叶子节点,t下的子孙要增加的值存入a[t].add中 { a[t].add += k; a[t].sum += (a[t].y-a[t].x+1)*k; return ; } a[t].sum += (y-x+1)*k; Push_down(t); int mid = (a[t].x+a[t].y)/2; if(y<=mid) Add(t<<1,x,y,k); else if(x>mid) Add(t<<1|1,x,y,k); else { Add(t<<1,x,mid,k); Add(t<<1|1,mid+1,y,k); }}int main(){ int n,m; char ch; int x,y,k; scanf("%d %d",&n,&m); Build(1,1,n); while(m--) { getchar(); scanf("%c %d %d",&ch,&x,&y); if(ch=='Q') printf("%lld\n",Query(1,x,y)); else { scanf("%d",&k); Add(1,x,y,k); } } return 0;}
- 数据结构--线段树--lazy延迟操作
- 线段树lazy操作
- POJ3468 线段树 + Lazy Tag (延迟标记)
- POJ3468【线段树lazy操作】
- hdoj1698【线段树Lazy操作】
- 线段树区间更新模板(lazy延迟标记)(1698)
- poj (3468)线段树lazy操作
- hdu4027(线段树的lazy操作)
- HDU 3954 线段树 特殊LAZY操作
- BZOJ 1798 线段树 双LAZY操作
- HDU 4578 线段树 多lazy操作
- pku3468 线段树lazy
- JuQueen(线段树 lazy)
- 【例题】【线段树】lazy
- 线段树Lazy-tag
- 线段树--lazy思想
- HDU1698(线段树LAZY)
- 线段树lazy标记
- ubuntu安装离线JDK
- WIKIOI-1264 芳香数
- Struts2(二)——配置文件struts2.xml的编写
- WINDOWS下VIM配置
- BroadcastReceiver
- 数据结构--线段树--lazy延迟操作
- java判断字符串是否是数字
- html按钮需要规范填写
- (Java)微信之个人公众账号开发(二)——接收并处理用户消息 (中)
- 利用GDI+基于WIN32实现桌面雪花效果(一)
- Hardcoded string,should use @string resource警告 && eclipse中一次性导入用到的类 && 重命名多处调用的对象
- Python装饰器学习笔记
- hdu 1242 Rescue 解题报告
- HDU 1226 搜索