A Simple Problem with Integers(多个数状数组)
2017-10-03 21:49
176 查看
Problem I
Time Limit : 5000/1500ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 5
Problem Description
Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element.
Input
There are a lot of test cases. The first line contains an integer N. (1 <= N <= 50000) The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000) The third line contains an integer
Q. (1 <= Q <= 50000) Each of the following Q lines represents an operation. "1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000) "2 a" means querying the value of
Aa. (1 <= a <= N)
Output
For each test case, output several lines to answer all query operations.
Sample Input
4
1 1 1 1
14
2 1
2 2
2 3
2 4
1 2 3 1 2
2 1
2 2
2 3
2 4
1 1 4 2 1
2 1
2 2
2 3
2 4
Sample Output
1
1
1
1
1
3
3
1
2
3
4
1
Statistic | Submit | Back
思路:根据每个数相对起点的对k取余得到余数的值维护多个树状数组。。。。难啊。。。。
代码:
#include<bits/stdc++.h>
using namespace std;
#define maxn 50010
int N,M;
int a[maxn];
struct node{
int a[maxn];
int lowbit(int x){
return x&(-x);
}
void clear(){
memset(a,0,sizeof(a));
}
int getsum(int x){
int res=0;
while(x){
res+=a[x];
x-=lowbit(x);
}
return res;
}
void update(int l,int r,int c){
while(l<=N+1){
a[l]+=c;
l+=lowbit(l);
}
r++;
while(r<=N+1){
a[r]+=-c;
r+=lowbit(r);
}
}
}tree[11][11]; //多个数组
int main(){
while(scanf("%d",&N)!=EOF){
for(int i=1;i<=N;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=10;i++){
for(int j=0;j<=10;j++){
tree[i][j].clear(); //初始化为0
}
}
int op,x,y,k,c;
scanf("%d",&M);
while(M--){
scanf("%d",&op);
if(op==1){
scanf("%d%d%d%d",&x,&y,&k,&c);
int j=(x-1)%k;
tree[k][j].update(x,y,c); //进行变化
} else {
scanf("%d",&x);
int ans=0;
for(int i=1;i<=10;i++){
ans+=tree[i][(x-1)%i].getsum(x); //得到这个数的总的变化数
}
printf("%d\n",ans+a[x]);
}
}
}
return 0;
}
相关文章推荐
- POJ3468 A Simple Problem with Integers(数状数组||区间修改的RMQ问题)
- poj 3468 A Simple Problem with Integers 解题报告 线段树 数状数组两种实现
- [树状数组] poj3468 A Simple Problem with Integers
- A Simple Problem with Integers(树状数组,更新区间查询区间模板)
- POJ 3468 A Simple Problem with Integers 【树状数组】
- [POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
- 【树状数组】 HDOJ 4276 A Simple Problem with Integers
- 【三维树状数组与离散化】HDU 4267——A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
- 【G - A Simple Problem with Integers 树状数组 】
- poj-3468-A Simple Problem with Integers(树状数组更新区间查区间)
- 20140719 「树状数组 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- HDU 4267 A Simple Problem with Integers (树状数组)
- HDU 4267 A Simple Problem with Integers【树状数组】
- HDU4267 A Simple Problem with Integers(树状数组 离散化)
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- HDU 4267 A Simple Problem with Integers 多个树状数组
- POJ 3468 A Simple Problem with Integers(树状数组区间更新) 续
- HDU-4267-A Simple Problem with Integers-(树状数组)