http://acm.hdu.edu.cn/showproblem.php?pid=1394

题意:

给定一个数组a,一开始数组里面的元素都是0,现在有三个操作:

操作1:给第k个数字加上d.

操作2:查询区间[l,r]的和.

操作3:改变区间[l,r]的每个数为最接近的斐波那契数列.其中斐波那契数列的f0=1,f1=1.

题解:

这题是单点更新+区间求和+区间更新,我们知道区间求和为了节省一点时间用的是懒惰标记来给每个被更改过的区间打上标记,而当我们需要计算这个区间的时候才将标记下推,使得值更新.

所以对于操作1的话,当我们更新单点的值得时候,由于当前区间在之前可能经历过区间的更改,所以我们需要维护两个数组来.第一个数组sum1用来维护所需要的输出的和,第二个数组sum2用来维护的是经过操作3之后的当前区间的和.

所以对于操作1的时候,需要判断一下该点有没有懒惰标记,如果有的话,该点的值用的就是经过操作3的sum2的值来更改,否则直接更改sum1的值.

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int dir[][]={{,},{,},{,},{,-},{-,},{-,-},{,-},{-,}};
#define pi acos(-1)
#define ls rt<<1
#define rs rt<<1|1
#define me0(s) memset(s,0,sizeof(s))
#define me1(s) memset(s,1,sizeof(s))
#define mef(s) memset(s,-1,sizeof(s))
#define meinf(s) memset(s,inf,sizeof(s))
#define llinf 1e18
#define inf 1e9
const int N=1e5+;
ll fab[N];
int n,m;
ll lazy[N*]; //lazy表示该区间的数要更改为斐波那契
ll a[N],sum1[N*],sum2[N*];//sum1表示原来的和,sum2表示更改后的值
void init(){
fab[]=fab[]=;
for(int i=;i<=;i++){
fab[i]=fab[i-]+fab[i-];
}
}
ll find_fab(ll x){
ll ans=fab[];
ll c=abs(fab[]-x);
for(int i=;i<=;i++){
if(abs(x-fab[i])<c){
c=abs(x-fab[i]);
ans=fab[i];
}
}
return ans;
}
void pushup(int rt){
sum1[rt]=sum1[ls]+sum1[rs];
sum2[rt]=sum2[ls]+sum2[rs];
}
void pushdown(int rt){
if(lazy[rt]){
lazy[ls]=lazy[rs]=lazy[rt];
lazy[rt]=;
sum1[ls]=sum2[ls];
sum1[rs]=sum2[rs];
}
}
void build(int l,int r,int rt){
if(l==r){
sum2[rt]=;//初始状态所有数据为0,距离0最近的菲波那切数是1
return ;
}
int m=(l+r)/;
build(l,m,ls);
build(m+,r,rs);
pushup(rt);
}
void update1(int l,int r,int p,int c,int rt){
if(l==r){
if(lazy[rt]) sum1[rt]=sum2[rt]+c;
else sum1[rt]+=c;
sum2[rt]=find_fab(sum1[rt]);//更新sum1,sum2也要改变
lazy[rt]=;
return ;
}
pushdown(rt);
int m=(l+r)/;
if(p<=m) update1(l,m,p,c,ls);
if(p>m) update1(m+,r,p,c,rs);
pushup(rt);
}
void update2(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
sum1[rt]=sum2[rt];
lazy[rt]=;
return ;
}
pushdown(rt);
int m=(l+r)/;
if(L<=m) update2(L,R,l,m,ls);
if(R>m) update2(L,R,m+,r,rs);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return sum1[rt];
}
pushdown(rt);
int m=(l+r)/;
ll ans=;
if(L<=m) ans+=query(L,R,l,m,ls);
if(R>m) ans+=query(L,R,m+,r,rs);
return ans;
}
int main(int argc, char * argv[]){
init();
while(scanf("%d%d",&n,&m)!=EOF){
me0(sum1);
me0(lazy);
build(,n,);
while(m--){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==){
update1(,n,l,r,);
}
else if(op==){
printf("%lld\n",query(l,r,,n,));
}
else{
update2(l,r,,n,);
}
}
}
return ;
}

最新文章

  1. js中使用new Date(str)创建时间对象不兼容firefox和ie的解决方式
  2. Linux/centos下安装riak
  3. iOS UICollectionView的实现
  4. 【Oracle学习笔记-4】内连接和外连接的区别
  5. Android项目真的要去做混淆(加密)处理
  6. A - Oulipo
  7. Beyond Compare 设置打开文件的默认编码
  8. 剑指offer-面试题14.调整数组顺序使奇数位于偶数的前面
  9. JavaScript闭包小窥
  10. 洛谷 [P2762] 太空飞行计划问题
  11. 在graphviz中创建可点击的图形
  12. Data Block Compression
  13. xcode打包苹果应用遇到的问题及解决方法
  14. 为github公开项目单独设置用户名
  15. Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
  16. [JZOJ5987] 仙人掌毒题
  17. sql 表中删除字段重复的行
  18. BASIC-3_蓝桥杯_字母图形
  19. 二、thinkphp
  20. 结合Bootbox将后台JSON数据填充Form表单

热门文章

  1. css盒子模型概念
  2. 使用sqlyog工具同步两个相同结构的数据库之间的数据
  3. CSIC_716_20191129【面向对象高级----反射、类的内置方法(魔法方法)、单例模式】
  4. You believe in fate?
  5. bzoj1004题解
  6. 在linux中的rpm -ivh 是干什么的呢?
  7. NApache+JBOSS架构方案
  8. error C1083: 无法打开包括文件: “ui_roadquery.h”: No such file or directory c:\users\administrator\desktop\g_w_j\04 开发阶段\开发工程\imcshowapp\imcshowapp\roadquery.h 5 1 IMCShowApp
  9. 创建一个学生表student,默认的表空间为users,字段自定,同时为表的各个字段分别添加合适的约束,然后测试约束的验证状态。
  10. this.$router.go()和this.$router.push()的差别