Poj A Simple Problem with Integers(lazy线段树)
来源:互联网 发布:安科瑞售电付费软件 编辑:程序博客网 时间:2024/05/17 06:30
A Simple Problem with Integers
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 40 Accepted Submission(s) : 13
Problem 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
Source
PKU
注意要用longlong。。。
#include <stdio.h>#include <algorithm>using namespace std;struct Node{ int l,r; int lazy; long long num,tag;}tree[1000000];long long A[100005];void Build(int n,int l,int r){ //先将一棵树的各个元素初始化 tree[n].l = l; tree[n].r = r; tree[n].tag = 0; tree[n].lazy = 0; if(l == r){ tree[n].num = A[l]; return; } int mid = (l + r) / 2; Build(2*n,l,mid); Build(2*n+1,mid+1,r); //分别建好一棵树的左右儿子之后,计算tree[n].num tree[n].num = tree[2*n].num + tree[2*n+1].num;}//lazy标记的思想是修改到到确切的区间后,不再修改儿子//只是用一个lazy标记,等以后修改或查找到这个区间//再把lazy标记往下传void Modify(int n,int x,int y,long long der){ //先得到所在区间的范围 int l = tree[n].l; int r = tree[n].r; int mid = (l + r) / 2; //找到恰好的区间进行修改及lazy标记 if(l == x && r == y){ tree[n].lazy = 1; tree[n].tag += der; tree[n].num += der * (y - x + 1); return; } //如果该区间有lazy标记,则修改儿子,并将标记清0 if(tree[n].lazy == 1){ tree[n].lazy = 0; Modify(2*n,l,mid,tree[n].tag); Modify(2*n+1,mid+1,r,tree[n].tag); tree[n].tag = 0; } if(x <= mid)//修改左儿子的充要条件 Modify(2*n,x,min(y,mid),der); if(y > mid)//修改右儿子的充要条件 Modify(2*n+1,max(mid+1,x),y,der); tree[n].num = tree[2*n].num + tree[2*n+1].num;}long long Find(int n,int x,int y){ int l = tree[n].l; int r = tree[n].r; int mid = (l + r) / 2; if(l == x && r == y){ return tree[n].num; } if(tree[n].lazy == 1){ tree[n].lazy = 0; Modify(2*n,l,mid,tree[n].tag); Modify(2*n+1,mid+1,r,tree[n].tag); tree[n].tag = 0; } long long ans = 0; if(x <= mid) ans += Find(2*n,x,min(mid,y)); if(y > mid) ans += Find(2*n+1,max(mid+1,x),y); return ans;}int main(){ int n,q; int i; char op; int a,b,c; while(scanf("%d%d",&n,&q) != EOF){ for(i = 1;i <= n;i++){ scanf("%lld",&A[i]); } Build(1,1,n); for(i = 0;i < q;i++){ getchar(); scanf("%c",&op); if(op == 'Q'){ scanf("%d%d",&a,&b); printf("%lld\n",Find(1,a,b)); } else{ scanf("%d%d%d",&a,&b,&c); Modify(1,a,b,c); } } } return 0;}
- Poj A Simple Problem with Integers(lazy线段树)
- poj 3468 A Simple Problem with Integers LAZY线段树
- poj 3468 A Simple Problem with Integers(线段树+lazy)
- poj3468:A Simple Problem with Integers(线段树lazy)
- POJ 3468A Simple Problem with Integers(线段树 + Lazy Tag(延迟更新))
- POJ 3468 A Simple Problem with Integers(线段树、lazy思想)
- POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
- poj 3468 A Simple Problem with Integers(线段树,lazy思想 经典题)
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- POJ-3468-A Simple Problem with Integers(线段树区间维护 重写Lazy)
- POJ3468 A Simple Problem with Integers 【线段树+lazy】
- POJ_3468 A Simple Problem with Integers(线段树+lazy标记)
- poj3468 A Simple Problem with Integers 线段树lazy标签
- 线段树+lazy-poj3468-A Simple Problem with Integers
- A Simple Problem with Integers (线段树 +lazy标记)
- poj-3468-A Simple Problem with Integers 线段树lazy标记
- poj 3468 A Simple Problem with Integers 【线段树 + 区间更新lazy】
- POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)
- java 数据类型范围
- Mapx的VC开发实践
- 重回技术怀抱
- 初学时的数据库操作辅助类
- Tangent Space的基向量计算方法
- Poj A Simple Problem with Integers(lazy线段树)
- HDOJ--最短路---Dijkstra模版
- 设置buffer cache
- 图的知识归纳
- viewDidLoad, viewWillDisappear, viewWillAppear等差别
- SQL Server中事务处理的注意事项
- android Camera 小研究 (一)
- Delphi下DBGrid拖放问题
- 如何提高论文被SCI,EI,ISTP三大检索系统收录的几率??