hdu 4267 A Simple Problem with Integers(分类别维护多个树状数组)

Let A1, A2, ... , AN be N elements. Youneed to deal with two kinds of operations. One type of operation is to add agiven number to a few numbers in a given interval. The other is to query thevalue 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 toanswer all query operations.

 

对数组分类,建立多个树状数组,分别保存不同类别修改的不同被别数的数值

const int MAXN = 1000000;
int s[11][11][50005];
int a[50005];
int n;
int lowbit(int x)
{
   return x & (-x);
}
int add(int a, int b, int i, int v)
{
   while (i <= n)
    {
       s[a][b][i] += v;
       i += lowbit(i);
    }
}
int sum(int a, int b, int i)
{
   int ret = 0;
   while (i > 0)
    {
       ret += s[a][b][i];
       i -= lowbit(i);
    }
   return ret;
}
 
int main ()
{
   int T;
   int op, aa, bb, v, k;
   while (RI(n) != EOF)
    {
       FE(i, 1, n) RI(a[i]);
       CLR(s, 0);
       int m;
       RI(m);
       while (m--)
       {
           RI(op);
           if (op == 1)
           {
                RIV(aa, bb, k, v);
                add(k, aa % k, aa, v);
               add(k, aa % k, (bb + 1),-v);
           }
           else
           {
                RI(aa);
                int ans = a[aa];
                FE(i, 1, 10) ans += sum(i, aa %i, aa);
                cout << ans <<endl;
           }
       }
    }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值