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

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

时间: 2024-08-04 10:44:24

HDU 4267 A Simple Problem with Integers(树状数组)的相关文章

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

poj 3468 A Simple Problem with Integers 树状数组 或 线段树

题目链接:http://poj.org/problem?id=3468 一般说来 树状数组是 [单点更新 区间查询] 而本题是“区间更新 区间查询” 所以要改成维护前缀和 推公式的过程见<挑战程序设计竞赛>第181~182面 代码中bit0是i的零次项 bit1是i的一次项 以后以此类推 在[l, r]加数的时候 写出公式 在l的地方 一次项及以上的直接写 然后在零次项那减去 在r的地方 一次项及以上的减掉之前加上的 然后再零次项那加上“公式化简之后的最终结果 减去 之前在零次项加的那一项”

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

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): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

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): 5402    Accepted Submission(s): 1710 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include

HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)

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

POJ3468 A Simple Problem with Interger [树状数组,差分]

题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 130735   Accepted: 40585 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One

A Simple Problem with Integers_树状数组

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 a