hdu4348 To the moon(区间修改,区间查询的主席树)
来源:互联网 发布:护肤品行业数据 编辑:程序博客网 时间:2024/06/05 19:19
每个时间建一棵线段树,这样就很好回到过去的某一版本了。然后区间修改就像线段树那样,但是不能下放懒标记,因为你的左右子树可能是公用的历史版本,会出锅。你只能记成绝对标记。每次在路径上累加懒标记。还要注意区间长度。
#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const N=100005;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,m,a[N],rt[N],num=0,now=0;struct node{ int lc,rc,tag; ll sum;}tree[30*N];inline void build(int &p,int l,int r){ p=++num;tree[p].tag=0; if(l==r){tree[p].sum=a[l];return;} int mid=l+r>>1; build(tree[p].lc,l,mid);build(tree[p].rc,mid+1,r); tree[p].sum=tree[tree[p].lc].sum+tree[tree[p].rc].sum;}inline void change(int &p,int l,int r,int x,int y,int val){ tree[++num]=tree[p];p=num;tree[p].sum+=(ll)val*(y-x+1); if(x==l&&r==y){tree[p].tag+=val;return;} int mid=l+r>>1; if(y<=mid) change(tree[p].lc,l,mid,x,y,val); else if(x>mid) change(tree[p].rc,mid+1,r,x,y,val); else change(tree[p].lc,l,mid,x,mid,val),change(tree[p].rc,mid+1,r,mid+1,y,val);}ll query(int p,int l,int r,int x,int y){ if(x==l&&r==y) return tree[p].sum; ll res=0;int mid=l+r>>1; res+=(ll)tree[p].tag*(y-x+1);//沿途遇到懒标记也是要加上的 if(y<=mid) return res+query(tree[p].lc,l,mid,x,y); if(x>mid) return res+query(tree[p].rc,mid+1,r,x,y); return res+query(tree[p].lc,l,mid,x,mid)+query(tree[p].rc,mid+1,r,mid+1,y);}int main(){// freopen("a.in","r",stdin); while(~scanf("%d%d",&n,&m)){ num=0;now=0; for(int i=1;i<=n;++i) a[i]=read(); build(rt[0],1,n); while(m--){ char op[5];scanf("%s",op+1); if(op[1]=='B'){now=read();continue;} int l=read(),r=read(); if(op[1]=='C'){ rt[++now]=rt[now-1]; change(rt[now],1,n,l,r,read()); } if(op[1]=='Q') printf("%lld\n",query(rt[now],1,n,l,r)); if(op[1]=='H') printf("%lld\n",query(rt[read()],1,n,l,r)); } }return 0;}
阅读全文
0 0
- hdu4348 To the moon(区间修改,区间查询的主席树)
- 【HDU4348】To The Moon-主席树(可持久化线段树)区间修改+区间询问
- hdu4348 To the moon 区间更新
- HDU 4348 To the moon 【主席树+区间修改】
- vjudge: spoj--to the moon(主席树区间修改)
- HDU 4348 To the moon [主席树 区间修改]
- SPOJ TTM To The Moon 主席树的区间更新与查询
- hdu4348 To the moon (主席树 || 离线线段树)
- HDU4348 To The Moon <带修主席树>
- SPOJ TTM To the moon(主席树+区间操作)
- HDU 4348 To the moon(主席树区间更新)
- hdu-4348-To the moon-主席树在线区间更新
- HDU 4348To the moon 主席树 + 区间更新
- hdu4348 为 心灵之旅To The Moon 出的 时光穿梭 题。主席树
- hdu-4348-To the moon(主席树更新,区间更新,区间和查询,保存历史版本返回)
- hdu4348 To the moon
- hdu4348 To the moon
- hdu-4348-To the moon-离线处理 or 主席树区间更新
- Docker和虚拟机的区别
- 手把手带你走进MVP +Dagger2 + DataBinding+ Rxjava+Retrofit 的世界
- String,StringBuffer,StringBuilder三者之间的区别
- 无法通过ip访问redis服务
- Vue配置文件详解
- hdu4348 To the moon(区间修改,区间查询的主席树)
- 深拷贝、浅拷贝
- MySQL 加锁处理分析
- springcloud入门之配置中心
- Docker 退出容器但不关闭当前容器
- Oracle中查询表字段基本信息、主键、外键(整理)
- 自定义View圆圈跟随手指的demo
- 标准时间转换
- ffmpeg视频转码和获取视频第一帧shell脚本