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. jQuery常用操作(待续)

    1. input清空内容 <1> $("#选择器id").val(""); <2> $("input[name='input框 ...

  2. Spring开发踩坑记录

    #1 @EnableEurekaServer无法正常import原因是spring-cloud-dependencies版本太低,改成高版本的Edgware.SR4即可.参考:https://www. ...

  3. Spring @Async之一:实现异步调用示例

    什么是“异步调用”? “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用的语句返回结果 ...

  4. 【快学springboot】8.JPA乐观锁OptimisticLocking

    介绍 当涉及到企业应用程序时,正确地管理对数据库的并发访问是至关重要的.为此,我们可以使用Java Persistence API提供的乐观锁定机制.它导致在同一时间对同一数据进行多次更新不会相互干扰 ...

  5. 2017 青岛现场赛 Suffix

    Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of them, you need ...

  6. 【剑指Offer面试编程题】题目1372:最大子向量和--九度OJ

    题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天JOBDU测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但 ...

  7. JS echarts统计

    柱状图 function drawbarFunc(xs, ys) { //var xs1 = []; //var ys1 = []; require.config({ paths: { echarts ...

  8. 第3节 storm高级应用:1、上次课程回顾,今日课程大纲,storm下载地址、运行过程等

    上次课程内容回顾: ConcurrentHashMap是线程安全的,为什么多线程的时候还不好使,为什么还要加static关键字 1.storm的基本介绍:strom是twitter公司开源提供给apa ...

  9. Java-android使用GridView布局的电子相册&服务器获取图片

    转  http://www.tuicool.com/articles/B7JNv2 电子相册的思路: 1.先是考虑布局,我用的是GridView布局 2.GridView中又该怎么显示图片,其实我的这 ...

  10. django的404,500错误自定义页面的配置

    django404,500错误自定义页面: 1.设置settings文件 DEBUG = False ALLOWED_HOSTS = ['127.0.0.1', 'localhost']或者ALLOW ...