POJ 3468 A Simple Problem with Integers
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 110999 | Accepted: 34570 | |
Case Time Limit: 2000MS |
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 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
线段树区间更新,区间求和
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define mem(a) (memset(a,0,sizeof(a))) typedef long long LL; const int mod=100006; LL n,m,x,y,z; LL vis[mod<<2]; LL sum[mod<<2]; void pushup(LL node) { sum[node]=sum[node<<1]+sum[node<<1|1]; } void pushdown(LL node,LL m) { if(vis[node]) { vis[node<<1]+=vis[node]; vis[node<<1|1]+=vis[node]; sum[node<<1]+=vis[node]*(m-(m>>1)); sum[node<<1|1]+=vis[node]*(m>>1); vis[node]=0; } } void build(LL l,LL r,LL node) { vis[node]=0; if(l==r) { scanf("%lld",&sum[node]); return; } LL mid=(l+r)>>1; build(l,mid,node<<1); build(mid+1,r,node<<1|1); pushup(node); } void update(LL ll,LL rr,LL val,LL l,LL r,LL node) { if(ll<=l && rr>=r) { vis[node]+=val; sum[node]+=val*(r-l+1); return ; } pushdown(node,r-l+1); LL mid=(l+r)>>1; if(ll<=mid) update(ll,rr,val,l,mid,node<<1); if(rr>mid) update(ll,rr,val,mid+1,r,node<<1|1); pushup(node); } LL query(LL ll,LL rr,LL l,LL r,LL node) { LL ans=0; if(ll<=l && rr>=r) return sum[node]; pushdown(node,r-l+1); LL mid=(l+r)>>1; if(ll<=mid) ans+=query(ll,rr,l,mid,node<<1); if(rr>mid) ans+=query(ll,rr,mid+1,r,node<<1|1); return ans; } int main() { scanf("%lld%d",&n,&m); build(1,n,1); while(m--) { char a[5]; scanf("%s",a); if(a[0]=='Q') { scanf("%lld%lld",&x,&y); printf("%lld\n",query(x,y,1,n,1)); } else { scanf("%lld%lld%lld",&x,&y,&z); update(x,y,z,1,n,1); } } return 0; }
非递归实现
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const int maxn=1e5+6; ll sum[maxn<<1]; ll add[maxn<<1]; ll n,m,N,x,y,c; char s[3]; void build() { N=1;while(N<n+2) N<<=1; for(int i=1;i<=n;i++) scanf("%lld",&sum[N+i]); for(int i=N-1;i;i--) sum[i]=sum[i<<1]+sum[i<<1|1]; } void updata(int L,int R,int val) { ll i,j,Ln=0,Rn=0,x=1; for(i=N+L-1,j=N+R+1;i^j^1;i>>=1,j>>=1,x<<=1) { sum[i]+=val*Ln; sum[j]+=val*Rn; if(~i&1) add[i^1]+=val,sum[i^1]+=val*x,Ln+=x; if(j&1) add[j^1]+=val,sum[j^1]+=val*x,Rn+=x; } for(;i;i>>=1) { sum[i]+=Ln*val; sum[j]+=Rn*val; } } ll query(int L,int R) { ll i,j,Rn=0,Ln=0,x=1,ans=0; for(i=N+L-1,j=N+R+1;i^j^1;i>>=1,j>>=1,x<<=1) { if(add[i]) ans+=add[i]*Ln; if(add[j]) ans+=add[j]*Rn; if(~i&1) ans+=sum[i^1],Ln+=x; if(j&1) ans+=sum[j^1],Rn+=x; } for(;i;i>>=1) { ans+=add[i]*Ln; ans+=add[j]*Rn; } return ans; } int main() { scanf("%lld%lld",&n,&m); build(); while(m--) { scanf("%s",&s); if(s[0]=='Q') { scanf("%lld%lld",&x,&y); printf("%lld\n",query(x,y)); } else { scanf("%lld%lld%lld",&x,&y,&c); updata(x,y,c); } } return 0; }