A Simple Problem with Integers (线段树区间更新)
来源:互联网 发布:python源码下载 编辑:程序博客网 时间:2024/06/06 09:50
这题是上一次的扩展有关区间更新的问题,不能用for循环的,因为每次要更新的,复杂度变高
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.
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.
You need to answer all Q commands in order. One answer in a line.
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
455915
The sums may exceed the range of 32-bit integers.
#7691097 zhuyinghui's solution for [Problem B]
Select Code
#include <iostream>#include <cstdio>#include <cstring>#define maxn 100010using namespace std;struct node{ int l,r; long long val,add;}tree[maxn<<2];int a[maxn];long long sum;void pushup(int rt){ tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val;}void pushdown(int rt){ if(tree[rt].add) { tree[rt<<1].add+=tree[rt].add; tree[rt<<1|1].add+=tree[rt].add; tree[rt<<1].val+=tree[rt].add*(tree[rt<<1].r-tree[rt<<1].l+1); tree[rt<<1|1].val+=tree[rt].add*(tree[rt<<1|1].r-tree[rt<<1|1].l+1); //务必记得这些都是+=而不是等于这是个不容易发现的小错误 tree[rt].add=0; }}void build(int rt,int p,int q){ tree[rt].l=p; tree[rt].r=q; tree[rt].add=0; if(p==q) { tree[rt].val=a[p]; return ; } build(rt<<1,p,(p+q)>>1); build(rt<<1|1,(p+q)/2+1,q); pushup(rt); return ;}void update(int rt,int p,int q,int c){ if(p<=tree[rt].l&&tree[rt].r<=q) { tree[rt].add+=c; tree[rt].val+=c*(tree[rt].r-tree[rt].l+1); return ; } pushdown(rt); int mid=(tree[rt].r+tree[rt].l)>>1; if(p<=mid) { update(rt<<1,p,q,c); } if(q>mid) { update(rt<<1|1,p,q,c); } pushup(rt);}void query(int rt,int p,int q){ int mid=(tree[rt].r+tree[rt].l)>>1; if(p==tree[rt].l&&q==tree[rt].r) { sum+=tree[rt].val; } else{ pushdown(rt);//此处务必加上这个因为在更新的时候并没有全部更新叶子节点有遗漏,询问的时候需要再遍历一遍 if(q<=mid) { query(rt<<1,p,q); } else if(p>mid) { query(rt<<1|1,p,q); } else { query(rt<<1,p,mid); query(rt<<1|1,mid+1,q); } } return ;}int main(){ int n,t,x,y,z; scanf("%d %d",&n,&t); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } build(1,1,n); char ch; while(t--) { getchar(); scanf("%c",&ch); if(ch=='C') { scanf("%d %d %d",&x,&y,&z); update(1,x,y,z); } if(ch=='Q') { sum=0; scanf("%d %d",&x,&y); query(1,x,y); printf("%I64d\n",sum); } } return 0;}
0 0
- A Simple Problem with Integers (线段树区间更新)
- A Simple Problem with Integers +poj+线段树区间更新
- poj3468 A Simple Problem with Integers 线段树区间更新
- A Simple Problem with Integers (POJ_3468) 线段树+区间更新
- Poj3468 A Simple Problem with Integers 线段树、区间更新
- 【poj3468-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 A Simple Problem with Integers (线段树区间更新区间查询模板)
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
- A Simple Problem with Integers (线段树,区间求和+区间更新)
- 线段树 (更新区间查询区间)poj 3468 A Simple Problem with Integers
- 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(线段树区间更新+求和)
- PKU A Simple Problem with Integers (线段树区间更新求和)
- openfire + sqlserver ,以及现有系统的账户集成
- mysql自带加密解密字符集问题
- Eclipse保存console调试及日志信息
- scrapy 出现key error 出错解决方法
- 数组和链表区别
- A Simple Problem with Integers (线段树区间更新)
- unity3D-游戏/AR/VR在线就业班 蓝鸥C#入门泛型学习笔记
- GIT 命令收集
- Navicat 连接oracle 11g突然蹦出了一个错误“ORA-28547:connection to server failed,probable Oracle Net admin error”
- json中的单引号
- fragment懒加载小总结
- Futaba S-BUS controlled by mbed(使用mbed控制Futaba S-BUS)
- java学集合前准备知识object类中的常用方法
- 对Verilog 初学者比较有用的整理(转自它处)