博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列...
阅读量:4622 次
发布时间:2019-06-09

本文共 2420 字,大约阅读时间需要 8 分钟。

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 AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const int maxn=1e5+5;typedef long long ll;using namespace std;struct node{ ll l,r,sum;}tree[maxn<<2];ll lazy[maxn<<2];void pushup(int m){ tree[m].sum=tree[m<<1].sum+tree[m<<1|1].sum;}void pushdown(int m,int l){ if(lazy[m]!=0) { lazy[m<<1]+=lazy[m]; lazy[m<<1|1]+=lazy[m]; tree[m<<1].sum+=lazy[m]*(l-(l>>1)); tree[m<<1|1].sum+=lazy[m]*(l>>1); lazy[m]=0; }}void build(int m,int l,int r){ tree[m].l=l; tree[m].r=r; if(l==r) { scanf("%lld",&tree[m].sum); return; } int mid=(l+r)>>1; build(m<<1,l,mid); build(m<<1|1,mid+1,r); pushup(m);}void update(int m,int l,int r,int val){ if(tree[m].l==l&&tree[m].r==r) { lazy[m]+=val; tree[m].sum+=(ll)val*(r-l+1); return; } if(tree[m].l==tree[m].r) return; int mid=(tree[m].l+tree[m].r)>>1; pushdown(m,tree[m].r-tree[m].l+1); if(r<=mid) { update(m<<1,l,r,val); } else if(l>mid) { update(m<<1|1,l,r,val); } else { update(m<<1,l,mid,val); update(m<<1|1,mid+1,r,val); } pushup(m);}ll query(int m,int l,int r){ if(tree[m].l==l&&tree[m].r==r) { return tree[m].sum; } pushdown(m,tree[m].r-tree[m].l+1); int mid=(tree[m].l+tree[m].r)>>1; ll res=0; if(r<=mid) { res+=query(m<<1,l,r); } else if(l>mid) { res+=query(m<<1|1,l,r); } else { res+=(query(m<<1,l,mid)+query(m<<1|1,mid+1,r)); } return res; }int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); int n,m; cin>>n>>m; build(1,1,n); char op[2]; int l,r,val; for(int t=0;t

 

转载于:https://www.cnblogs.com/Staceyacm/p/10781756.html

你可能感兴趣的文章
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>
android编译遇到问题修改
查看>>
解决Ubuntu18.04.2远程桌面Xrdp登录蓝屏问题
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>
j2ee爬坑行之二 servlet
查看>>
Python语言编程
查看>>
[poj 1469]Courses
查看>>
vue+element-ui实现表格checkbox单选
查看>>
测试开发学习进阶教程 视频&PDF
查看>>