A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3958 Accepted Submission(s): 1222
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)
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
连续区间中单点的修改,将其符合条件的位置建立在一个树状数组中,即建立多个树状数组。
(i-a)%k=0等价于i%k=a%k,即i满足i+k满足,i+2*k也满足……。
但是如果想用如线段树,数状数组等数据结构去优化,要求数必须是连续的一段的,所以思路就是所间隔的数整合在一起。
设数状数组t[i][j]表示当k等于i的时候,将位置x(数字位置的标号)满足x%i==j的所有数放到这个树状数组里,即更新时为t[k][a%k].add(a,b,c).
那么就将所有的数按这个位置模k是多少,分别放到了t[k][a%k]的数状数组里。询问xx位置的数,就等于t[i->(1,10)][xx%i].sum(xx)里这个数的和了。
(i-a)%k=0等价于i%k=a%k,即i满足i+k满足,i+2*k也满足……。
但是如果想用如线段树,数状数组等数据结构去优化,要求数必须是连续的一段的,所以思路就是所间隔的数整合在一起。
设数状数组t[i][j]表示当k等于i的时候,将位置x(数字位置的标号)满足x%i==j的所有数放到这个树状数组里,即更新时为t[k][a%k].add(a,b,c).
那么就将所有的数按这个位置模k是多少,分别放到了t[k][a%k]的数状数组里。询问xx位置的数,就等于t[i->(1,10)][xx%i].sum(xx)里这个数的和了。
#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<string.h>
#include<algorithm>
#define N 50005
#define mod 100007
#define ll long long
#define pii pair<int,int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define ex 2.7182818284590452354+
#define pi 3.141592653589793239
#define eps 1e-8
#define INFF 999999999
using namespace std;
int a[N];
int n;
struct node
{
int val[N];
void clear()
{
memset(val,0,sizeof(val));
}
int lowbit(int x)
{
return x&(-x);
}
void add(int st,int ed,int c)
{
int i;
for(i=st-1;i>=1;i-=lowbit(i))
val[i]-=c;
for(i=ed;i>=1;i-=lowbit(i))
val[i]+=c;
}
int sum(int x)
{
int i,addd=0;
for(i=x;i<=n;i+=lowbit(i))
addd+=val[i];
return addd;
}
}t[11][11];
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j,q,op;
for(i=0;i<11;i++)
{
for(j=0;j<11;j++)
t[i][j].clear();
}
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&q);
while(q--)
{
scanf("%d",&op);
if(op==2)
{
int xx,sum=0;
scanf("%d",&xx);
for(i=1;i<=10;i++)
sum+=t[i][xx%i].sum(xx);
printf("%d\n",sum+a[xx]);
}
else
{
int a,b,k,c;
scanf("%d %d %d %d",&a,&b,&k,&c);
t[k][a%k].add(a,b,c);
}
}
}
return 0;
}