HDU 4267 A Simple Problem with Integers 多个树状数组

A Simple Problem with Integers

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

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    
题意 就是给你n个数,m个操作 操作分为两种 1 a b k c,就是 i属于(a,b)这个区间,而且i必须满足(i-a)%k==0,然后这个数加上c 2 a,问你坐标为a的数的大小是多少    
题解 他是分段求和,分段加,肿么办! 那我们建立N多树状数组就好啦,然后直接区间加加加加!!! 然后就好了  
代码

int d[maxn][12][12];
int a[maxn];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void update2(int x,int num,int k,int mod)
{
    while(x>0)
    {
        d[x][k][mod]+=num;
        x-=lowbit(x);
    }
}
int getSum1(int x,int k)
    int s=0;
    while(x<=n)
    {
        REP_1(i,10)
        {
            s+=d[x][i][k%i];
        }
        x+=lowbit(x);
    }
    return s;
}

int main()
{
    while(RD(n)!=-1)
    {
        REP_1(i,n)
            RD(a[i]);
        memset(d,0,sizeof(d));
        int q;
        RD(q);
        while(q--)
        {
            int t;
            RD(t);
            if(t==1)
            {
                int l,r,k,c;
                RD(l),RD(r),RD(k),RD(c);
                update2(r,c,k,l%k);
                update2(l-1,-c,k,l%k);
            }
            if(t==2)
            {
                int c;
                RD(c);
                printf("%d\n",getSum1(c,c)+a[c]);
            }
        }
    }
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4333266.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞