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】

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
const int maxn=5e4+;
int tree[][][maxn];
int a[maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int k,int x,int pos,int val)
{
while(pos<=n)
{
tree[k][x][pos]+=val;
pos+=lowbit(pos);
}
} int query(int k,int x,int pos)
{
int res=;
while(pos)
{
res+=tree[k][x][pos];
pos-=lowbit(pos);
}
return res;
}
int main()
{
while(~scanf("%d",&n))
{
memset(tree,,sizeof(tree));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int q;
scanf("%d",&q);
while(q--)
{
int tp;
scanf("%d",&tp);
if(tp==)
{
int x,y,k,c;
scanf("%d%d%d%d",&x,&y,&k,&c);
int st=x/k;
if(x%k) st++;
int ed=st+(y-x)/k;
add(k,x%k,st,c);
add(k,x%k,ed+,-c);
}
else
{
int x;
scanf("%d",&x);
int ans=a[x];
int pos;
for(int i=;i<=;i++)
{
pos=x/i;
if(x%i) pos++;
ans+=query(i,x%i,pos);
}
printf("%d\n",ans);
}
}
}
return ;
}

树状数组

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers的更多相关文章

  1. 【树状数组区间修改单点查询】HDU 4031 Attack

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...

  2. POJ2155 Matrix(二维树状数组||区间修改单点查询)

    Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...

  3. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  4. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  5. hdu-1556 Color the ball---树状数组+区间修改单点查询

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...

  6. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  7. hdu1556 树状数组区间更新单点查询板子

    就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...

  8. hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询

    点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...

  9. 树状数组区间修改and查询和

    在差分数组上稍加改变,就可以实现这个骚操作 首先我们先来看一看普通的树状数组(基于差分)怎么暴力的求解区间和就是询问区间长度次和 \(\sum^{i=1}_{len}\sum^{j=1}_{i}bas ...

随机推荐

  1. 树状数组 简单题 cf 961E

    题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...

  2. MAC进入文件夹快捷键

    common + O common+up common+Down shift + common +G

  3. CS193p Lecture 8 - Protocols, Blocks and Animation

    一.协议(Protocols) 1. 声明协议 @protocol Foo <Xyzzy, NSObject> // ... @optinal // @required //... @en ...

  4. [LUOGU] P1048 采药

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...

  5. Linux菜鸟起飞之路【三】Linux常用命令

    一.Linux命令的基本格式 命令 [选项] [参数] a)命令:就是告诉操作系统要做什么 b)选项:说明命令的运行方式,有的会改变命令的功能,选项通常以“-”开始 c)参数:说明命令的操作对象,如文 ...

  6. 离线功能对比:service worker和applicationCache

    SW 复杂,事件驱动,可以拦截请求,和缓存这些请求的响应数据,实现的效果更加灵活 AppCache 简单易用,声明式的将要缓存的文件清单声明在一个文件中.由于设计上的原因,它存在一些问题,导致难以运用 ...

  7. Codeforces Round #439 (Div. 2) B. The Eternal Immortality

    B. The Eternal Immortality 题目链接http://codeforces.com/contest/869/problem/B 解题心得:题意就是给出a,b,问(a!)/(b!) ...

  8. PHP-redis命令之 散列(hashes)

    二.散列(hashes) 1.hset:设置散列的值 $redis->hset('myhas','field1','hello'); $redis->hset('myhas','field ...

  9. HDU 3045 DP 斜率优化 Picnic Cows

    题意:将n个数分成若干组,每组数字的个数不少于t个,要把每组的数字减小到这组最小值,求所有数字减少的最小值. 先将这n个数从小到大排个序,可以想到一组里面的数一定是排序后相邻的. 设d(i)表示前i个 ...

  10. Python学习案例

    例1.求101到200之间所有的质数,并打印总数. 说明:除去1和它本身之外,不能被其他数整除,就是质数. #!/bin/python #-*- coding:utf-8 -*- #使用集合法 l = ...