HDU 4267 A Simple Problem with Integers(树状数组区间更新)

时间:2022-04-18 05:31:41

A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5402    Accepted Submission(s): 1710

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
 
Source
 
Recommend
liuyiding
/*
类似的区间更新,但这个区间不是实际意义上的区间而是,i到j满足条件的点更新
这个题和区间更新类似,用另一个数组维护,满足条件的点的前缀和,询问的时候直接用原数组的值加上满足条件的值
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#define lowbit(x) x&(-x)
#define N 50010
using namespace std;
int c[][][N];//
int n,q,op;
void update(int s1,int s2,int x,int val)//间隔为s1,起点为s2,需要更新的点为x
{
while(x<=n)
{
c[s1][s2][x]+=val;
x+=lowbit(x);
}
}
int getsum(int s1,int s2,int x)
{
int s=;
while(x>)
{
s+=c[s1][s2][x];
x-=lowbit(x);
}
return s;
}
int num[N];
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
//cout<<n<<endl;
memset(c,,sizeof c);
for(int i=;i<n;i++)
scanf("%d",&num[i]);
scanf("%d",&q);
int x,y,k,val;
while(q--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%d%d%d",&x,&y,&k,&val);
x--;
y--; int knum=(y-x)/k;//需要更新的点的个数
int s1=x%k;//这次更新的起点
update(k,s1,x/k+,val);
update(k,s1,x/k+knum+,-val);
}
else if(op==)
{
scanf("%d",&x);
x--;
int cur=num[x];
for(int i=;i<=;i++)//遍历的是k的取值
cur+=getsum(i,x%i,x/i+);
printf("%d\n",cur);
}
}
}
return ;
}