【树状数组区间修改单点查询+分组】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】
#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的更多相关文章
- 【树状数组区间修改单点查询】HDU 4031 Attack
http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...
- 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 ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
- hdu-1556 Color the ball---树状数组+区间修改单点查询
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- hdu1556 树状数组区间更新单点查询板子
就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...
- hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询
点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...
- 树状数组区间修改and查询和
在差分数组上稍加改变,就可以实现这个骚操作 首先我们先来看一看普通的树状数组(基于差分)怎么暴力的求解区间和就是询问区间长度次和 \(\sum^{i=1}_{len}\sum^{j=1}_{i}bas ...
随机推荐
- ubuntu 14.04 构建openstack使用的ubunt 16 的桌面版的使用镜像
1. 下载ubuntu 16.04桌面版的iso文件,我的个人网盘中有,可以下载 https://pan.baidu.com/s/14qT3lbbqLwDaejmz2VSkyw 2. 安装制作镜像文件 ...
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...
- OC 导入类 #import和@class 区别
objective-c中#import和@class的区别 在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forwar ...
- javascript的trigger事件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...
- Windows平台下使用vs code搭建python3环境(1)
前言:最近几周在使用python开发的过程中,碰到了好多坑,由于以前使用visual studio 2015习惯了,导致刚开始搭建python开发环境以及管理各种包的时候有点不习惯,再加上python ...
- Python中读取txt文本出现:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape问题解决
windows中的路径是反斜杠\,然而反斜杠\在python中有着转义字符的意义,所以在py文件中写windows文件路径的时候,要特别注意反斜杠\的使用. 下面有三种解决方式: 方式一:转义的方式 ...
- verilog behavioral modeling --loop statement
1.forever 2.repeat 3.while 4.for The for statement accomplishes the same results as the following ps ...
- 【ORACLE】调整序列的当前种子值
[ORACLE]调整序列的当前种子值 --必须用SYS用户执行脚本:或具有SYSDBA角色登录: CREATE OR replace ); v_step ):;--步进 tsql ); BEGIN E ...
- http协议工作原理(转)
WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页.WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间 ...
- 2. vsCode 安装GoCode
1)安装gocode 打开命令提示符(以管理员身份打开),输入: go get -u -v github.com/nsf/gocode 开始下载: 下载完毕: 下载完成,查看D:\GoWorks目录, ...