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

来源:互联网 发布:net.aspx开发 json 编辑:程序博客网 时间:2024/05/29 09:51

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 1142 12 22 32 41 2 3 1 22 1 2 22 32 41 1 4 2 12 12 22 32 4
 

Sample Output
111113312341
 


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 longusing namespace std;int n,a,b,k,v;int bit[11][11][N],num[N];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;}


0 0
原创粉丝点击