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

题意:给你一个序列,有两个操作,1是将区间[a,b]内满足 (i - a) % k == 0的位置加上c,2是查询a位置的数。

分析:因为k比较小,所以可以对于每一个k建立一个树状数组,这样还不够,无法确定应该向哪个位置加数,再加一维表示模k的余数,这样建立就行了。

对于更新,我们只需要对F[k][a%k][x]的树状数组更新,因为是区间更新,可以考虑加一段和剪一段,在a这个位置更新c,在b+1的位置减去c就不影响后面了。

查询就普通查询,注意要加上初始值。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

const int MAXN = 50000+100;
int F[12][12][MAXN];
int num[MAXN];
int n;

void update(int s,int t,int x,int val)
{
    while(x<=n)
    {
        F[s][t][x]+=val;
        x+=x&-x;
    }
}
int query(int s,int t,int x)
{
    int res=0;
    while(x>0)
    {
        res+=F[s][t][x];
        x-=x&-x;
    }
    return res;
}

int main()
{
    int m;
    int op,a,b,k,c;
    while(scanf("%d",&n)!=EOF)
    {
        memset(F,0,sizeof(F));
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d%d%d",&a,&b,&k,&c);
                int t=a%k;
                update(k,t,a,c);
                update(k,t,b+1,-c);
            }
            else if(op==2)
            {
                int ans=0;
                scanf("%d",&a);
                for(int j=1;j<=10;j++) ans+=query(j,a%j,a);
                printf("%d\n",ans+num[a]);
            }
        }
    }
    return 0;
}
时间: 2024-08-28 04:24:30

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

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): 4191    Accepted Submission(s): 1309 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