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

来源:互联网 发布:apache tomcat怎么安装 编辑:程序博客网 时间:2024/06/05 04:57

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 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



    题意:给出一个数目为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 50010using 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
原创粉丝点击