POJ 3468 A Simple Problem with Integers
来源:互联网 发布:百度搜索合作网络 编辑:程序博客网 时间:2024/06/02 03:28
题意:
给你n个数,q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c
可以用线段树,树状数组,伸展树
线段树:
/*线段树*/#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;#define ll long longconst int N=100005;int root;struct tree{ ll val,lazy;}node[N*4];void pushDown(int rt,int l,int r){ if(node[rt].lazy!=0){ int mid=(l+r)>>1; node[rt<<1].val+=node[rt].lazy*(mid-l+1); node[rt<<1].lazy+=node[rt].lazy; node[rt<<1|1].val+=node[rt].lazy*(r-mid); node[rt<<1|1].lazy+=node[rt].lazy; node[rt].lazy=0; }}void pushUp(int rt,int l,int r){ node[rt].val=node[rt<<1].val+node[rt<<1|1].val;}void buildTree(int rt,int l,int r){ node[rt].lazy=0; if(l==r){ scanf("%I64d",&node[rt].val); return ; } int mid=(l+r)>>1; buildTree(rt<<1,l,mid); buildTree(rt<<1|1,mid+1,r); pushUp(rt,l,r);}void update(int rt,int l,int r,int L,int R,int v){ if(L<=l&&R>=r){ node[rt].lazy+=v; node[rt].val+=v*(r-l+1); return ; } pushDown(rt,l,r); int mid=(l+r)>>1; if(L<=mid){ update(rt<<1,l,mid,L,R,v); } if(R>mid){ update(rt<<1|1,mid+1,r,L,R,v); } pushUp(rt,l,r);}ll query(int rt,int l,int r,int L,int R){ if(L<=l&&R>=r){ return node[rt].val; } pushDown(rt,l,r); int mid=(l+r)>>1; ll ans=0; if(L<=mid)ans+=query(rt<<1,l,mid,L,R); if(R>mid)ans+=query(rt<<1|1,mid+1,r,L,R); return ans;}int main(){ int m,n; scanf("%d%d",&n,&m); buildTree(1,1,n); while(m--) { getchar(); char s; int a,b,c; s=getchar(); if(s=='Q') { scanf("%d%d",&a,&b); printf("%I64d\n",query(1,1,n,a,b)); } else { scanf("%d%d%d",&a,&b,&c); update(1,1,n,a,b,c); } } return 0;}
0 0
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- PyQt5 来写一个评论框
- html5 based noVNC trial
- 素数分解 dp
- 【Github教程】史上最全github使用方法:github入门到精通
- I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之u-boot移植
- POJ 3468 A Simple Problem with Integers
- 弹出层
- 栈,堆,全局,文字常量,代码区总结
- 递归回溯法实战(一)——(Red and Black)红黑砖迷阵(POJ1979)
- java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得
- 最长回文子序列 区间dp
- 安卓 像素 和dp的转换
- C语言中,为什么字符串可以赋值给字符指针变量
- 有关阿里云云服务器ECS Windows Server 2012 R2 64位标准版无法使用远程桌面管理登录解决办法