zoukankan      html  css  js  c++  java
  • 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
    ",getSum1(c,c)+a[c]);
                }
            }
        }
    }
  • 相关阅读:
    Word常用定义的变量
    Delphi对Word一些进阶操作
    Delphi 统计Word文档中的字数
    Delphi对Word的基本操作
    Spring3 MVC请求参数获取的几种方法
    Spring3系列13-Controller和@RequestMapping
    Spring3系列12-Spring AOP AspectJ
    Spring3系列11-Spring AOP——自动创建Proxy
    Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法
    Spring3系列9-Spring AOP——Advice
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4333266.html
Copyright © 2011-2022 走看看