线段树(区间修改,区间查询)
来源:互联网 发布:网络上终端设备的功能 编辑:程序博客网 时间:2024/05/19 02:18
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define lson (rt<<1)#define rson (rt<<1|1)#define lowbit(x) ((x)&(-(x)))using namespace std;typedef long long LL;const int MAXN=2e5+5;int val[MAXN];struct node{ int tot; int left,right; int mark;}tree[MAXN*4];int build(int rt,int left,int right){ tree[rt].mark=0; tree[rt].left=left; tree[rt].right=right; if(left==right) return tree[rt].tot=val[left]; int mid=(left+right)/2; int a=build(lson,left,mid); int b=build(rson,mid+1,right); return tree[rt].tot=a+b;}void update_mark(int rt){ if(tree[rt].mark) { tree[rt].tot=tree[rt].mark*(tree[rt].right-tree[rt].left+1); if(tree[rt].left!=tree[rt].right) tree[lson].mark=tree[rson].mark=tree[rt].mark; tree[rt].mark=0; }}int calc(int rt,int left,int right){ update_mark(rt); if(tree[rt].left>right||tree[rt].right<left) return 0; if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot; int a=calc(lson,left,right); int b=calc(rson,left,right); return a+b;}int update(int rt,int left,int right,int val){ update_mark(rt); if(tree[rt].left>right||tree[rt].right<left) return tree[rt].tot; if(tree[rt].left>=left&&tree[rt].right<=right) { tree[rt].mark=val; return tree[rt].tot=val*(tree[rt].right-tree[rt].left+1); } int a=update(lson,left,right,val); int b=update(rson,left,right,val); return tree[rt].tot=a+b;}int main(){ int T; scanf("%d",&T); int cas=0; while(T--) { int n,q; scanf("%d%d",&n,&q); for(int i=1;i<=n;++i) { val[i]=1; } build(1,1,n); int x,y,z; for(int i=1;i<=q;++i) { scanf("%d%d%d",&x,&y,&z); update(1,x,y,z); } printf("Case %d: The total value of the hook is %d.\n",++cas,calc(1,1,n)); } return 0;}
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define lson (rt<<1)#define rson (rt<<1|1)#define lowbit(x) ((x)&(-(x)))using namespace std;typedef long long LL;const int MAXN=1e5+5;int val[MAXN];struct node{ LL tot,add; int left,right;}tree[MAXN*4];void pushup(int rt){ tree[rt].tot=tree[lson].tot+tree[rson].tot;}void pushdown(int rt,int m){ if(tree[rt].add) { tree[lson].add+=tree[rt].add; tree[rson].add+=tree[rt].add; tree[lson].tot+=(m-(m>>1))*tree[rt].add; tree[rson].tot+=(m>>1)*tree[rt].add; tree[rt].add=0; }}void build(int rt,int left,int right){ tree[rt].add=0; tree[rt].left=left; tree[rt].right=right; if(left==right) {tree[rt].tot=val[left];return;} int mid=(left+right)>>1; build(lson,left,mid); build(rson,mid+1,right); pushup(rt);}LL query(int rt,int left,int right){ if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot; pushdown(rt,tree[rt].right-tree[rt].left+1); int mid=(tree[rt].left+tree[rt].right)>>1; LL res=0; if(left<=mid) res+=query(lson,left,right); if(right>mid) res+=query(rson,left,right); return res;}void update(int rt,int left,int right,int val){ if(left<=tree[rt].left&&tree[rt].right<=right) { tree[rt].add+=val; tree[rt].tot+=(LL)val*(tree[rt].right-tree[rt].left+1); return; } pushdown(rt,tree[rt].right-tree[rt].left+1); int mid=(tree[rt].left+tree[rt].right)>>1; if(left<=mid) update(lson,left,right,val); if(right>mid) update(rson,left,right,val); pushup(rt);}int main(){ int n,q; while(scanf("%d%d",&n,&q)!=EOF) { for(int i=1;i<=n;++i) { scanf("%d",&val[i]); } build(1,1,n); char op; int x,y,z; for(int i=1;i<=q;++i) { getchar(); scanf("%c",&op); //cout<<"log: "<<op<<endl; if(op=='Q') { scanf("%d%d",&x,&y); printf("%lld\n",query(1,x,y)); }else { scanf("%d%d%d",&x,&y,&z); update(1,x,y,z); } } } return 0;}
阅读全文
0 0
- 线段树(区间修改,区间查询)
- 线段树区间修改+区间查询
- [模板]-线段树-区间修改 + 区间查询
- 线段树 (区间修改 区间查询 延迟标记)
- 线段树(单点修改,区间查询)
- 线段树(区间修改,单点查询)
- [线段树][区间修改&&查询]
- 【线段树】区间求和+区间修改(区间加)
- 【洛谷】线段树 树状数组区间修改区间查询
- 【模板】线段树区间修改、区间求和、查询最值
- 线段树点修改 区间查询
- 【数据结构】【线段树】单点修改区间查询
- HDU1166 线段树 单点修改、区间查询
- *线段树(区间修改)
- 线段树(区间修改)
- HDOJ-3974(线段树,区间修改点查询)
- 线段树区间修改与查询(求和)
- hdu1556(树状数组/线段树,区间修改,点查询)
- Web安全-XSS
- Spring bean的作用域(scope)
- 梯度检验---实例代码
- iOS蓝牙搜索
- H264码流中SPS PPS详解
- 线段树(区间修改,区间查询)
- 网络配置——undo 命令
- QT Socket编程过程问题
- pm2 相关命令
- Rikka with Competition(HDU 609)
- python爬虫——写出最简单的网页爬虫
- 设计模式初探-访问者模式
- wireshark 提取RTSP中的UDP data数据
- 关于Struts2上传问题代码实现