A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 163977   Accepted: 50540
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 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 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.
 
题意:  Q查询区间和;C,将区间[x,y]的数都加上z
 
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#define ll long long
using namespace std;
ll tree[], lazy[], len[];//tree[num]存的是节点num所在区间的区间和
void pushdown(ll num)
{
if (lazy[num] != )
{
tree[num * ] = tree[num * ] + lazy[num] * len[num * ];
tree[num * + ] = tree[num * + ] + lazy[num] * len[num * + ];
lazy[num * ] = lazy[num * ] + lazy[num];
lazy[num * + ] = lazy[num * + ] + lazy[num];
lazy[num] = ;
}
} void build(ll num, ll le, ll ri)
{
len[num] = ri - le + ;//区间长度
if (le == ri)
{
scanf("%lld", &tree[num]);
return;
}
ll mid = (le + ri) / ;
build(num * , le, mid);
build(num * + , mid + , ri);
tree[num] = tree[num * ] + tree[num * + ];
} void update(ll num, ll le, ll ri, ll x, ll y, ll z)
{
if (x <= le && ri <= y)
{
lazy[num] = lazy[num] + z;
tree[num] = tree[num] + len[num] * z;//更新区间和
return;
}
pushdown(num);
ll mid = (le + ri) / ;
if (x <= mid)
update(num * , le, mid, x, y, z);
if (y > mid)
update(num * + , mid + , ri, x, y, z);
tree[num]=tree[num*]+tree[num*+];
} ll query(ll num, ll le, ll ri, ll x, ll y)
{
if (x <= le && ri <= y)//查询区间在num节点所在区间内
return tree[num];
pushdown(num);
ll mid = (le + ri) / ;
ll ans = ;
if (x <= mid)
ans = ans + query(num * , le, mid, x, y);
if (y > mid)
ans = ans + query(num * + , mid + , ri, x, y);
return ans;
}
int main()
{
ll n, m;
scanf("%lld%lld", &n, &m);
build(, , n);
while (m--)
{
char c[];
scanf("%s", c);
if (c[] == 'Q')
{
ll x, y;
scanf("%lld%lld", &x, &y);
printf("%lld\n", query(, , n, x, y));
}
else
{
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
update(, , n, x, y, z);
}
}
return ;
}

POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers的更多相关文章

  1. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

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

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

  3. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

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

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

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

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

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

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

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

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

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

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

  9. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

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

随机推荐

  1. 第2节 Scala中面向对象编程:1、类的定义;2、类的构造器;3、object和伴生对象;4、apply和main方法

    6.    类.对象.继承.特质 Scala的类与Java.C++的类比起来更简洁,学完之后你会更爱Scala!!! 6.1.   类 6.1.1.    类的定义 package cn.itcast ...

  2. 【PAT甲级】1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)

    题意: 输入四个正数C,DIS,D,N(C<=100,DIS<=50000,D<=20,N<=500),分别代表油箱容积,杭州到目标城市的距离,每升汽油可以行驶的路程,加油站数 ...

  3. Linux CentOS7 VMware LAMP架构Apache用户认证、域名跳转、Apache访问日志

    一.Apache用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <Virtu ...

  4. Redis的安装配置及简单集群部署

    最近针对中铁一局项目,跟事业部讨论之后需要我们的KF平台能够接入一些开源的数据库,于是这两天研究了一下Redis的原理. 1. Redis的数据存储原理及简述 1.1Redis简述 Redis是一个基 ...

  5. h5 穿透滚动

    引子 h5 页面有弹窗浮层时,浮层之下若产生了滚动,滑动浮层时会让其产生滚动.这是示例页面,移动端访问如下: Origin My GitHub 原因 找到的信息里面有两种说法: 使用了 -webkit ...

  6. 「CF1004E」Sonya and Ice Cream

    题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...

  7. 7.12 Varnish体系结构

    备注:应用比较小,采用的架构模式  Varnish + 基本业务功能 但是一个问题是所有的资源在一台服务器上,反向代理特别多,缓存数据特别大,导致一台机器资源不够,考虑机器的拆分 Nginx 的反向代 ...

  8. Design and History FAQ for Python3

    Source : Design and History FAQ for Python3 Why is there no goto? 你可以通过异常来获得一个可以跨函数调用的 "goto 结构 ...

  9. C. Magic Grid 构造矩阵

    C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. android使用友盟实现第三方登录、分享以及微信回调无反应问题解决办法

    这里介绍微信和新浪登录.微信登录和新浪登录都需要申请第三方账号.可以参考官方文档http://dev.umeng.com/social/android/operation#2还是很清晰的. 新浪微博开 ...