HDU4267 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): 5991 Accepted Submission(s): 1919

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

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。

思路:区间操作,离散化思想去写。
不来想着用线段树写的,可是会内存超限,看大佬用树状数组写的,开55棵树状数组,用cnt[x][k][mod] 表示x 对k 取余为mod,这样在询问的时候只要循环到10就可以 了。而且(i - a) % k == 0即是 i % k == a % k == mod,这样更新的时候就可以更新(1,b) 和(1,a)了。

随后用线段树写一下再补上。

/*
 区间修改树状数组 和离散化

*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=50010;
int cnt[mm][11][11];
int num[mm];
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int k,int mod,int add)//对a%K 进行修改存储
{
    while(x>0)
    {
        cnt[x][k][mod]+=add;
        x-=lowbit(x);
    }
}
int sum(int x,int a)
{
    int s=0;
    while(x<mm)
    {
        for(int i=1;i<=10;i++)//k小于等于10
            s+=cnt[x][i][a%i];
        x+=lowbit(x);
    }
    return s;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        memset(cnt,0,sizeof(cnt));
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int o,a,b,k,c;
            scanf("%d",&o);
            if(o==1)
            {
                scanf("%d%d%d%d",&a,&b,&k,&c);
                update(b,k,a%k,c);
                update(a-1,k,a%k,-1*c);//由于b 算的是 b到0的  需要修改的树状数组树 a到 b  所以要对a前面的树状数组进行修改,以免影响后面的操作
            }
            else
            {
                scanf("%d",&a);
                int ans=sum(a,a);
                printf("%d\n",ans+num[a]);
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值