HDU 4267-A Simple Problem with Integers(多个BIT)

题意:

2种操作

1 a b k c 在区间[a,b]中的(i-a)%k==0的位置i上的数+c

2 a 查询位置a的值

输出每次查询的值

分析:

开始想到多维的线段树,但比较麻烦,看了题解才知道,用BIT实现区间更新,单点查询,若在区间[a,b]上的数加c

就在a位置加c ,b+1位置加-c 这样在查询时sum(i),(i>=a&&i<=b)就是当前i位置的值

维护多个BIT即可

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 50010
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int bit[11][11][N],a[N],n,m;
int lowbit(int x){
    return x&(-x);
}
void add(int i,int j,int x,int d){
    while(x<=n){
        bit[i][j][x]+=d;
        x+=lowbit(x);
    }
}
int sum(int i,int j,int x){
    int num=0;
    while(x>0){
        num+=bit[i][j][x];
        x-=lowbit(x);
    }
    return num;
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        scanf("%d",&m);
        memset(bit,0,sizeof(bit));
         int s,e,k,c,op,pos;
        while(m--){
            scanf("%d",&op);
            if(op==1){
                scanf("%d%d%d%d",&s,&e,&k,&c);
                add(k,s%k,s,c);
                add(k,s%k,e+1,-c);
            }
            else if(op==2){
                scanf("%d",&pos);
                int total=a[pos];
                for(int i=1;i<=10;++i)
                    total+=sum(i,pos%i,pos);
                printf("%d\n",total);
            }
        }
    }
return 0;
}

 

posted on 2015-08-16 17:14  积跬步、至千里  阅读(144)  评论(0编辑  收藏  举报

导航