POJ 3468 线段树入门题
来源:互联网 发布:apache cxf 下载 编辑:程序博客网 时间:2024/06/01 08:24
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define N 110000#define LL long long #define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1LL sum[N<<2],add[N<<2];struct point{int lc,rc;}tree[N<<2];void Pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];//向上更新父亲节点}void Pushdown(int rt,int m){if(add[rt])//如果add还有值没有更新,则向下更新{add[rt<<1]+=add[rt];add[rt<<1|1]+=add[rt];sum[rt<<1]+=add[rt]*(m-(m>>1));//将区间分成2部分,这里字节点区间长度分别为m-(m>>1);和m>>1;sum[rt<<1|1]+=add[rt]*(m>>1);add[rt]=0;//将add[rt]的0制空}}void build(int L,int R,int rt){tree[rt].lc=L;tree[rt].rc=R;add[rt]=0;if(L==R){scanf("%lld",&sum[rt]);return ;}int mid=(tree[rt].lc+tree[rt].rc)>>1;build( L,mid,rt<<1);build( mid+1,R,rt<<1|1);//构造左右子树Pushup(rt);//更新完子节点后再向上更新父节点}void update(LL c,int L,int R,int rt){if(tree[rt].lc==L&&tree[rt].rc==R)//如果更新区间恰好是当前区间则更新{add[rt]+=c;sum[rt]+=long long ((R-L+1)*c);return ;}if(tree[rt].lc==tree[rt].rc){return;}Pushdown(rt,tree[rt].rc-tree[rt].lc+1);//向下更新(如果add[rt]有值可以更新)int mid=(tree[rt].lc+tree[rt].rc)>>1;if(R<=mid)update(c,L,R,rt<<1);//右端点比mid小,则直接更新左子树else if(L>mid)update (c,L,R,rt<<1|1);//左端点比mid大,则直接<span style="font-family: Arial, Helvetica, sans-serif;">更新右子树</span>else //左右分开在2个不同字数,所以分步更新{update(c,L,mid,rt<<1);update(c,mid+1,R,rt<<1|1);}Pushup(rt);}LL query(int L,int R,int rt){if(L==tree[rt].lc&&R==tree[rt].rc)//查找到更新区间直接返回{return sum[rt];}Pushdown(rt, tree[rt].rc-tree[rt].lc+1);//否则先向下更新LL res=0;//以下内容同上update函数的更新思想int mid=(tree[rt].lc+tree[rt].rc)>>1;if(R<=mid)res+=query(L,R,rt<<1);else if(L>mid)res+=query(L,R,rt<<1|1);else{res+=query(L,mid,rt<<1);res+=query(mid+1,R,rt<<1|1);}return res;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifchar s[10];int n,m,a,b;LL c;while(~scanf("%d%d",&n,&m)){build(1,n,1);while(m--){scanf("%s",s);if(s[0]=='C'){scanf("%d%d%lld",&a,&b,&c);update(c,a,b,1);}else if(s[0]=='Q'){scanf("%d%d",&a,&b);printf("%lld\n",query(a,b,1));}}}return 0;}
对着模板敲得,正在理解中。经典的模板题,经典的模板,题解见注释。
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
Hint
Source
- POJ 3468 线段树入门题
- 线段树之区间求和 POJ 3468 线段树入门
- poj 2352(入门线段树)
- poj 3261 线段树入门
- poj 3264 线段树 入门
- 线段树入门(poj 3274 3468 2528)
- POJ 3667 Hotel 线段树 区间合并 入门题
- POJ 2528 线段树+离散化 入门题
- POJ 2777 Count Color 线段树入门题
- POJ 3468 A Simple Problem with Integers【线段树区间更新入门题】
- POJ-3468 A Simple Problem with Integers (线段树 入门题 区间修改 区间查询)
- 线段树入门 POJ 2182 Lots Cows
- poj 2528 经典线段树入门
- 【POJ】 2182 - Lost Cows 【线段树入门】
- 线段树 POJ 3468 A Simple Problem with Integers 线段树 成段更新入门
- poj 3468 线段树
- POJ 3468 线段树
- poj 3468 线段树
- js实现复选框全选和反选功能
- 魔兽军团前端项目的一些总结
- 3D引擎:Horde3D:App类
- 【跟我学Apache Commons】【二】Lang&Math
- 关于win2003 iis6 不显示验证码的解决方法
- POJ 3468 线段树入门题
- uvaoj 1352Colored Cubes
- 58架构演进
- 【跟我学apache-commons(三)】DBCP2.2---JavaSE配置DBCP连接池
- Xcode 7.1在xib和storyboard经常意外退出求解
- 方法(函数)的声明与实现、方法(函数)的调用
- centos 6.5 配置samba 服务器
- 从头认识java-6.2 继承(Generalization)(1)
- [Django数据库模型]理解Django里的MTV开发模式