码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-13 14:08:37      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:hdu 4267 a simple pr   树状数组   

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
 

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][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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:hdu 4267 a simple pr   树状数组   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/46861013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!