POJ 3468 A Simple Problem with Integers
Posted 十年换你一句好久不见
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3468 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; }
以上是关于POJ 3468 A Simple Problem with Integers的主要内容,如果未能解决你的问题,请参考以下文章
A Simple Problem with Integers POJ - 3468
POJ - 3468 A Simple Problem with Integers
[poj3468]A Simple Problem with Integers
POJ3468 a simple problem with integers 分块