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): 4475    Accepted Submission(s): 1378


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
 

Source



    题意:给出一个数目为n的序列,然后有m步操作,有两种操作方式
1.输入四个数据a,b,k,c将区间[a,b]中的数i满足(i-a)%k  == 0加上c.
2.输入一个数y,输出序列中第y个数的值。

  思路:因为k的值很小,那么对k取余的值也会很小,所以可以建立一个树状数组c[x][k][x%k]代表x对k取余的值,然后每次更新树状数组的时候只需要更新updata(a,.....) 与updata(b+1,.....);




#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N 50010

using namespace std;

int a[N];
int c[N][12][12];
int n,m;

int lowbit(int x) {
    return x&(-x);
}

void updata(int x,int k,int mod,int num) {
    while(x<=n) {
        c[x][k][mod] += num;
        x += lowbit(x);
    }
}

int getsum(int x,int y) {
    int s = 0;
    while(x>0) {
        for(int i=1; i<=10; i++) {
            s += c[x][i][y%i];
        }
        x -= lowbit(x);
    }
    return s;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        memset(c,0,sizeof(c));
        for(int i=1; i<=n; i++) {
            scanf("%d",&a[i]);
        }
        int x,y,k,num;
        scanf("%d",&m);
        while(m--) {
            scanf("%d",&x);
            if(x == 1) {
                scanf("%d%d%d%d",&x,&y,&k,&num);
                updata(x,k,x%k,num);
                updata(y+1,k,x%k,-num);
            } else {
                scanf("%d",&y);
                int sum = getsum(y,y);
                printf("%d\n",sum+a[y]);
            }
        }

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值