POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
来源:互联网 发布:做网络销售公司好做吗 编辑:程序博客网 时间:2024/04/30 02:31
A Simple Problem with IntegersCrawling in process...Crawling failedTime Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
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 abc" means adding c to each of Aa,Aa+1, ... ,Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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
Hint
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int MAXN=100010;LL sum[MAXN<<2];LL lazy[MAXN<<2];void PushUp(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt,int m)//lazy的思想在这体现;就是从当前根节点rt向下更新每个子节点的值 { if(lazy[rt]){ lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; sum[rt<<1]+=lazy[rt]*(m-(m>>1)); sum[rt<<1|1]+=lazy[rt]*(m>>1); lazy[rt]=0;//更新后需要还原 }}void Build(int l,int r,int rt){ lazy[rt]=0; if(l==r){ scanf("%lld",&sum[rt]); return ; } int mid=(l+r)>>1; Build(lson); Build(rson); PushUp(rt);}void Update(int ll,int rr,int x,int l,int r,int rt){ if(ll<=l&&rr>=r){ lazy[rt]+=x; sum[rt]+=(LL)x*(r-l+1); return ; } PushDown(rt,r-l+1); int mid=(l+r)>>1; if(ll<=mid) Update(ll,rr,x,lson); if(rr>mid) Update(ll,rr,x,rson); PushUp(rt);}LL Query(int ll,int rr,int l,int r,int rt){ if (ll<=l&&rr>=r){ return sum[rt]; } PushDown(rt,r-l+1); LL ans=0; int mid=(r+l)>>1; if(ll<=mid) ans+=Query(ll,rr,lson); if(rr>mid) ans+=Query(ll,rr,rson); return ans;}int main(){ int n,q; int a,b,c; char str[10]; scanf("%d %d",&n,&q); Build(1,n,1); while(q--){ getchar(); scanf("%s",str); if(str[0]=='Q'){ scanf("%d %d",&a,&b); LL ans=Query(a,b,1,n,1); printf("%lld\n",ans); } else{ scanf("%d %d %d",&a,&b,&c); Update(a,b,c,1,n,1); } } return 0;}
- POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- 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标记)
- POJ-3468-A Simple Problem with Integers(线段树区间维护 重写Lazy)
- POJ 3468A Simple Problem with Integers(线段树 + Lazy Tag(延迟更新))
- 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 线段树lazy标记
- A Simple Problem with Integers +poj+线段树区间更新
- hdu 3038 How Many Answers Are Wrong 带权并查集
- Python中的正则表达式
- linux新建用户后shell无法自动补全命令,Backspace无法删除字符,无法翻看输入纪录
- 由卖票程序来理解线程
- Tomcat 拒绝服务总结
- POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
- How to use CCache to speed up cocos2d-x android compilation
- 计算机是如何启动的?
- Shuttle ESB介绍
- 备份和恢复
- 字符串的组合
- android项目出现红色大感叹号的问题解决方法
- 一点,虽然除了陈太忠之外,没什灼
- 【140813】C# 仿QQ农场游戏源代码