您的位置:首页 > 其它

HDU 4267 A Simple Problem with Integers(树状数组)

2015-07-13 12:31 423 查看


A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4191 Accepted Submission(s): 1309



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

2012 ACM/ICPC Asia Regional Changchun Online



Recommend

liuyiding | We have carefully selected several similar problems for you: 4276 4274 4273 4272 4270



题目:给出n个数,每次将一段区间内满足(i-l)%k==0 (r>=i>=l) 的数ai增加c

题解:可以建55个树状数组,bit[k][i][j]: i=a%k,j表示这个树状数组的第j个数。

求和的话就是10棵树的总和,更新只要更新一颗就行了。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define N 50020
#define ll long long
using namespace std;

int n,a,b,k,v;
int bit[11][11]
,num
;

void add(int i,int j,int x,int v) {
    while(x<=n) {
        bit[i][j][x]+=v;
        x+=x&-x;
    }
}

int getsum(int i,int j,int x) {
    int s=0;
    while(x>0) {
        s+=bit[i][j][x];
        x-=x&-x;
    }
    return s;
}

int main() {
    //freopen("test.in","r",stdin);
    while(cin>>n) {
        for(int i=1; i<=n; i++)
            scanf("%d",&num[i]);
        memset(bit,0,sizeof bit);
        int q;
        cin>>q;
        int op;
        while(q--) {
            scanf("%d%d",&op,&a);
            int ans=num[a];
            a--;
            if(op==2) {
                for(int i=1; i<=10; i++) {
                    ans+=getsum(i,a%i,a/i+1);
                }
                printf("%d\n",ans);
                continue;
            }
            scanf("%d%d%d",&b,&k,&v);
            b--;
            add(k,a%k,a/k+1,v);  
            add(k,a%k,a/k+(b-a)/k+1+1,-v);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: