A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6329 Accepted Submission(s): 2041
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
题目大意:给予一个数列和两种操作
操作一 输入left right mod add
for i from [left] to [right]
if((i-left)%mod==0) sequence[i]+=add;
操作二 输入一下标,返回当前下标值
解题思路: 考虑等式 (i-left)%mod = 0 => i%mod==left%mod
然后题目转换 区间修改和单点查询,使用魔改树状数组解决
#include <iostream> #include <algorithm> #include <cstring> using namespace std; #define N 50005 int data[N][11][11]; int num[N],n; int lowbit(int x){ return x&(-x); } void update(int mod,int ans,int cur,int add){///所有 mod, ans(left%mod) 属于一家人 把他们放在一起进行update操作 while (cur>0){ data[cur][mod][ans]+=add; cur-=lowbit(cur); } } int query(int x){ int signs; int sum = 0; for(int i = 1 ; i <= 10 ; ++i){///查询所有mod的可能情况,得到所有进行过的和 signs = x; while (signs<=n){ sum+=data[signs][i][x%i]; signs+=lowbit(signs); } } return sum; } int main(){ int q,ans,left,right,mod,add; while (cin>>n){ memset(data,0, sizeof(data)); for(int i = 1 ; i <= n ; ++i)scanf("%d",num+i); cin>>q; while (q--){ scanf("%d",&ans); if(ans==1){ scanf("%d %d %d %d",&left,&right,&mod,&add); update(mod,left%mod,right,add); update(mod,left%mod,left-1,-add); } else{ scanf("%d",&ans); printf("%d ",query(ans)+num[ans]); } } } }