A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
2015-03-31 21:34
309 查看
A Simple Problem with Integers
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
Sample Output
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 69589 | Accepted: 21437 | |
Case Time Limit: 2000MS |
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
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <sstream> #include <iomanip> using namespace std; typedef long long LL; const int INF=0x4fffffff; const int EXP=1e-5; const int MS=100005; LL sum[MS]; LL C[2][MS]; int N,Q; int lowbit(int x) { return x&(-x); } void updata(int no,int x,LL value) { while(x<=N) { C[no][x]+=value; x+=lowbit(x); } } LL getsum(int no,int x) { LL res=0; while(x>0) { res+=C[no][x]; x-=lowbit(x); } return res; } void solve() { scanf("%d%d",&N,&Q); sum[0]=0; for(int i=1;i<=N;i++) { scanf("%lld",&sum[i]); sum[i]+=sum[i-1]; } memset(C,0,sizeof(C)); int l,r; LL c; char cmd[10]; for(int i=1;i<=Q;i++) { scanf("%s",cmd); if(cmd[0]=='Q') { scanf("%d%d",&l,&r); LL ans=sum[r]-sum[l-1]+(getsum(0,r)-getsum(1,r)*(N-r))-(getsum(0,l-1)-getsum(1,l-1)*(N-l+1)); printf("%lld\n",ans); } else { scanf("%d%d%lld",&l,&r,&c); updata(0,l,c*(N-l+1)); updata(0,r+1,(N-r)*(-c)); updata(1,l,c); updata(1,r+1,-c); } } } int main() { solve(); return 0; }
相关文章推荐
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- POJ 3468 A Simple Problem with Integers (线段树区间修改)
- POJ_3468 A Simple Problem with Integers(线段树区间修改+附线段树模板)
- [POJ 2368 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(线段树区间修改+区间求和)
- 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(线段树区间修改+区间求和)
- POJ3468 A Simple Problem with Integers(数状数组||区间修改的RMQ问题)
- poj 3468 : A Simple Problem with Integers 【线段树 区间修改】
- POJ 3468 A Simple Problem with Integers(线段树区间修改)
- POJ -3468- A Simple Problem with Integers (线段是区间修改)
- 模板(线段树 + 树状数组 + 区间修改 + 区间查询)eg:POJ 3468 - A Simple Problem with Integers