HDU - 4267 A Simple Problem with Integers 三维树状数组

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

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

题意:

给出 n 表示有 n 个位置

然后有 一行 n 个数表示初始状态 每个位置的值

然后给出 m 表示有 m 条order

接下来 m 行..

每行的格式有两种..

1 b 表示询问在 b 点的值

2 a b k x 表示在 a 到 b 里面满足 (i-a)%k == 0 的位置上的值都加 x

思路:建立三维树状数组

(i-a)%k==0等价于i%k==a%k,所以可以把不同位置按取K的模放到一组里

ar[k][mod][x]表示在x位置k=k时,余数为mod的值,

求一个位置的值,只要遍历与v%i相等的数的和就行了

//这个题目我不懂得地方有为什么进行了a-- b--

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
const int MAXN=50020;

int c[12][12][MAXN];
int num[MAXN];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void update(int t1,int t2,int i,int val)
{
    while(i<=n){
        c[t1][t2][i]+=val;
        i+=lowbit(i);
    }
}
int sum(int t1,int t2,int i)
{
    int s=0;
    while(i>0){
        s+=c[t1][t2][i];
        i-=lowbit(i);
    }
    return s;
}
int main()
{
    int m;
    while(scanf("%d",&n)==1){
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);
        memset(c,0,sizeof(c));
        scanf("%d",&m);
        int a,b,k,q;
        int t;
        while(m--)
        {
            scanf("%d",&t);
            if(t==1){
                scanf("%d%d%d%d",&a,&b,&k,&q);
                a--;
                b--;
                int num=(b-a)/k;
                int s=a%k;
                update(k,s,a/k+1,q);
                update(k,s,a/k+num+2,-q);
            }
            else{
                scanf("%d",&a);
                a--;
                int ans=num[a];
                for(int i=1;i<=10;i++)
                    ans+=sum(i,a%i,a/i+1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81084203