HDU 4267 A Simple Problem with Integers 树状数组
来源:互联网 发布:php短信发送接口 代码 编辑:程序博客网 时间:2024/06/06 06:42
来源:http://acm.hdu.edu.cn/showproblem.php?pid=4267
题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。
思路:很容易想到这是一个插线问点的问题,但是在更新值 的时候和平常的更新不同,这道题又增加了一个限制条件。因为k比较小(<=10),余数的情况也很少,因此我们可以开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 <cstdio>#include <string.h>#include <algorithm>using namespace std;const int N = 50010;int cnt[N][11][11];int num[N];int inline lowbit(int x){return x & (-x);}void inline update(int x,int k,int mod,int add){while(x > 0){ cnt[x][k][mod] += add; x -= lowbit(x);}}int inline sum(int x,int a){int s = 0;while(x < N){for(int i = 1; i <= 10; ++i){ s += cnt[x][i][a%i];}x += lowbit(x);}return s;}int main(){//freopen("1.txt","r",stdin);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 op,a,b,k,add; scanf("%d",&op); if(op == 1){ scanf("%d%d%d%d",&a,&b,&k,&add); update(b,k,a%k,add); update(a-1,k,a%k,-add); } else{ scanf("%d",&a);int ans = sum(a,a);printf("%d\n",ans + num[a]); } }}return 0;}
- HDU 4267 A Simple Problem with Integers 树状数组
- hdu(4267)A Simple Problem with Integers(三维树状数组)
- HDU 4267 A Simple Problem with Integers --树状数组
- HDU 4267 A Simple Problem with Integers(树状数组)
- HDU 4267 A Simple Problem with Integers(树状数组)
- HDU - 4267 A Simple Problem with Integers(树状数组)
- hdu 4267 A Simple Problem with Integers 树状数组
- HDU-4267-A Simple Problem with Integers-(树状数组)
- A Simple Problem with Integers+hdu+树状数组
- HDOJ 4267 A Simple Problem with Integers 树状数组
- HDU 4267 A Simple Problem with Integers 数列离散化 + 树状数组
- 【三维树状数组与离散化】HDU 4267——A Simple Problem with Integers
- hdu 4267 A Simple Problem with Integers 线段树&树状数组
- HDU 4267 - A Simple Problem with Integers 树状数组区间修改
- hdu 4267 A Simple Problem with Integers(分类别维护多个树状数组)
- HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)
- HDU 4267 - A Simple Problem with Integers
- hdu 4267 A Simple Problem with Integers
- 关于文件读写冲突问题的探索
- ubuntu10.10+win7双系统重装win7后恢复ubuntu引导方法
- 商业项目中代码质量是否重要?
- 5 个常用的软件质量指标
- 使用eclipse插件创建一个web project
- HDU 4267 A Simple Problem with Integers 树状数组
- hdu3007 zoj1450最小圆覆盖
- 最长公共子序列
- 某个博客看到的,很鼓励,转了~~~
- 扩展欧几里得&&中国剩余定理 解线性同余方程组 专题
- Team Foundation Server 2010 安装、部署与配置(二):安装之前的预备工作
- 多态与new和delete
- Spring(4)-How to load multiple Spring bean configuration file
- Makefile以及链接库