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;
}
时间: 2025-01-11 23:49:02

HDU 4267-A Simple Problem with Integers(多个BIT)的相关文章

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 (树状数组)

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

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

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

HDU 3468 A Simple Problem with Integers

线段树区间更新求和 /* *********************************************** Author :Zhou Zhentao Email :[email protected] Created Time :2015/11/20 17:21:35 File Name :acm.cpp ************************************************ */ #include <stdio.h> #include <strin

hdu 4267/poj 3468 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): 4283    Accepted Submission(s): 1334 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with