poj 3468 A Simple Problem with Integers(线段树,lazy思想 经典题)
2014-10-29 10:35
183 查看
A Simple Problem with Integers
Crawling in process...
Crawling failed
Time 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
Sample Output
Hint
The sums may exceed the range of 32-bit integers
题意:
给你n个数,m次操作,每次操作有Q/C两种,Q:求出所给区间的和,C:给所给区间的每个值都加上a;
思路:
线段树,用sets[]给每个区间做标记
My Code:
Crawling in process...
Crawling failed
Time 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 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
The sums may exceed the range of 32-bit integers
题意:
给你n个数,m次操作,每次操作有Q/C两种,Q:求出所给区间的和,C:给所给区间的每个值都加上a;
思路:
线段树,用sets[]给每个区间做标记
My Code:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos(-1.0); const double E=2.718281828; typedef long long ll; const int INF=1000010; using namespace std; ll tree[N<<2],sets[N<<2]; void push_up(int idx) { tree[idx]=tree[lc]+tree[rc]; } void build(int l,int r,int idx) { sets[idx]=0; if(l==r) { scanf("%I64d",&tree[idx]); return ; } int mid=(l+r)>>1; build(lson); build(rson); push_up(idx); } void push_down(int idx,int m)///m为区间长度 { if(sets[idx]) { sets[rc]+=sets[idx]; sets[lc]+=sets[idx]; tree[lc]+=sets[idx]*(m-m/2); tree[rc]+=sets[idx]*(m/2); sets[idx]=0; } } void update(int l,int r,int idx,int x,int y,ll a) { if(x<=l&&y>=r) { sets[idx]+=a; tree[idx]+=(ll)(r-l+1)*a; return; } push_down(idx,r-l+1);//注意此函数不要忘了 int mid=(l+r)>>1; if(x<=mid) update(lson,x,y,a); if(y>mid) update(rson,x,y,a); push_up(idx); } ll query(int l,int r,int idx,int x,int y) { if(x<=l&&y>=r) return tree[idx]; push_down(idx,r-l+1); int mid=(l+r)>>1; ll ans=0; if(x<=mid) ans+=query(lson,x,y); if(y>mid) ans+=query(rson,x,y); return ans; } int main() { //freopen("in.txt","r",stdin); ll n,m; while(~scanf("%I64d%I64d",&n,&m)) { build(1,n,1); char c; ll a; int x,y; getchar(); while(m--) { scanf("%c%d%d",&c,&x,&y); if(c=='Q') printf("%I64d\n",query(1,n,1,x,y)); else { scanf("%I64d",&a); update(1,n,1,x,y,a); } getchar(); } } 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(线段树、lazy思想)
- 基本线段树(标记下传lazy-tag思想)poj 2468 A Simple Problem with Integers
- POJ - 3468 - A Simple Problem with Integers - 线段树Lazy大法
- poj 3468 A Simple Problem with Integers(线段树成段更新,懒惰标记的使用)经典题目
- POJ 3468 A 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 (线段树区间更新经典)
- 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 线段树
- POJ 3468 A Simple Problem with Integers 线段树 成段更新
- poj 3468 A Simple Problem with Integers[线段树 ]
- POJ 3468 A Simple Problem with Integers 线段树