A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 141093   Accepted: 43762
Case Time Limit: 2000MS

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 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 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

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

题意

线段树区间更新求和模板题。

给出n个数和m次操作,Q表示查询该区间内的数的和并输出,C表示把该区间内的所有数都加上一个值。

AC代码

抄的师傅的板子,还不是太理解

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=2e5+10;
const int mod=1e9+7;
using namespace std;
struct wzy
{
ll left,right,len;
ll value;
ll lazy;
}p[maxn<<2];
void push_up(ll o)
{
p[o].value=p[lson].value+p[rson].value;
}
void push_down(ll o)
{
if(p[o].lazy)
{
p[lson].lazy+=p[o].lazy;
p[rson].lazy+=p[o].lazy;
p[lson].value+=p[o].lazy*p[lson].len;
p[rson].value+=p[o].lazy*p[rson].len;
p[o].lazy=0;
}
}
void build(ll o,ll l,ll r)
{
p[o].left=l;p[o].right=r;
p[o].len=r-l+1;
p[o].lazy=0;
if(l==r)
{
ll x;
scanf("%lld",&x);
p[o].value=x;
return ;
}
ll mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
push_up(o);
}
void update(ll o,ll l,ll r,ll v)
{
if(p[o].left>=l&&p[o].right<=r)
{
p[o].lazy+=v;
p[o].value+=v*p[o].len;
return ;
}
push_down(o);
ll mid=(p[o].left+p[o].right)>>1;
if(r<=mid)
update(lson,l,r,v);
else if(l>mid)
update(rson,l,r,v);
else
{
update(lson,l,mid,v);
update(rson,mid+1,r,v);
}
push_up(o);
}
ll query(ll o,ll l,ll r)
{
if(p[o].left>=l&&p[o].right<=r)
return p[o].value;
ll mid=(p[o].left+p[o].right)>>1;
push_down(o);
if(r<=mid)
return query(lson,l,r);
else if(l>mid)
return query(rson,l,r);
else
return query(lson,l,mid)+query(rson,mid+1,r);
}
int main(int argc, char const *argv[])
{
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
char ch[5];
ll a,b,c;
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",query(1,a,b));
}
else
{
scanf("%lld%lld%lld",&a,&b,&c);
update(1,a,b,c);
}
}
return 0;
}

POJ 3468:A Simple Problem with Integers(线段树区间更新模板)的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  8. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. POJ 3468 A Simple Problem with Integers (线段树多点更新模板)

    题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...

  10. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. Utf-8+Bom编码导致的读取数据部分异常问题

    项目中经常会有这种场景,在配置文件中配置对应关系,项目启动的时候从中读取数据存入map缓存起来,这样使用的时候就可以直接从map找到对应的映射关系. 然后遇到了这么一个诡异的问题,一个简单的映射关系如 ...

  2. 转【面向代码】学习 Deep Learning(二)Deep Belief Nets(DBNs)

    [面向代码]学习 Deep Learning(二)Deep Belief Nets(DBNs) http://blog.csdn.net/dark_scope/article/details/9447 ...

  3. MySQL(一) 初识MySQL

    数据库基础 数据库是由一批数据构成的有序的集合,这些数据被存放在结构化的数据表里.数据表之间相互联系,反映了客观事物间的本质联系.数据库系统提供对数据的安全控制和完整性控制. 什么是数据库 数据库的发 ...

  4. Java IO流01-总叙

     Java IO包体系结构图: 1.流式部分――IO的主体部分: 2.非流式部分——主要包含一些辅助流式部分的类,如:File类.RandomAccessFile类和FileDescriptor等类: ...

  5. Win10系列:VC++绘制几何图形1

    本小节主要介绍如何使用Direct2D来绘制几何图形,其中会使用到FillGeometry函数和FillEllipse函数,FillGeometry函数用于填充几何图形的内部区域,而FillEllip ...

  6. Vue + Element UI 实现权限管理系统(国际化实现)

    国际化支持 1.安装依赖 执行以下命令,安装 i18n 依赖. yarn add vue-i18n $ yarn add vue-i18n yarn add v1.9.4 warning packag ...

  7. Uva LA 3177 - Beijing Guards 贪心,特例分析,判断器+二分,记录区间内状态数目来染色 难度: 3

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  8. 银联支付 Asp.Net 对接开发内容简介

    银联对接开发主要包含测试环境以及生产环境两部分. 其中程序开发部分测试以及生产是相同的. 不同的是,测试环境与生产环境请求支付的Url地址,以及分别使用的证书不同. 一.配置部分 1,测试环境证书获取 ...

  9. spoj220

    题解: 后缀数组 把所有串连接起来 二分答案 代码: #include<cstdio> #include<cstring> #include<algorithm> ...

  10. day043 前端css样式

    标签嵌套规则 块级标签能够嵌套某些块级标签和内敛标签(行内标签) 内敛标签不能块级标签,只能嵌套内联标签 块级标签能够设置高度和宽度 内敛标签不能设置,设置没有效果 Css样式 高度宽度: Width ...