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. ORACLE 判断首字母大小写问题

    1.对判断的字段进行拆分 select  substr(要区分的字段,0,1)  from 表 : 得到一个 首字母 2.对这个字符进行大小写判断 查出以小写字符为开头的 select  substr ...

  2. [经验] Java Web 项目怎么部署到 Linux 系统上

    废话少说, 直奔主题 第一步: 将 web 项目打成 war 包 1: 打开项目的 pom.xml 文件 如果是迭代后的项目, 记得修改项目的版本号, 这里我的是第二版所有就把 1 改成了 2 2: ...

  3. 笔记||Python3进阶之调用外部程序

    像wget可以下载文件 ffmpeg可以切割.合并.转换.录制视频 free命令可以查看linux内存使用信息 python提供了库来调用外部程序.命令?> 最常见的两种方法:       ①o ...

  4. robot framework 命令行执行用例与自带的run configurations运行用例

    一.cmd中运行命令 1.执行整个项目下的所有用例: pybot 项目路径.例如: pybot F:\EC\RF_Api 2.执行某个suite中的所有用例: pybot -s 项目路径\suite文 ...

  5. 解决请求中的post、get乱码问题以及响应的乱码问题

    post: get: response:

  6. 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase简介

    实验目的 了解hbase的概念 通过安装hbase了解hbase的原理 了解hbase与hadoop的关系 复习hadoop和zookeeper的运行 实验原理 hbase是bigtable的开源山寨 ...

  7. Vue 中引入echarts

    安装依赖 npm install echarts -S 或者使用淘宝的镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org ...

  8. 【转】iPhone/IOS使用Fiddler抓包配置

    原文链接:https://blog.csdn.net/weixin_39198406/article/details/81123716 1. 安装 安装Fiddler软件2. 配置2.1 端口 点击 ...

  9. GoJS 友情链接

    目前GoJS官网是学习gojs的最佳选择 GOJS简单示例 GoJS API学习 GoJS组织结构图2 mind map思维导图 组织结构图 GoJS实例1 GoJS实例2 GoJS实例3 GoJS实 ...

  10. Eclipse - 常见问题 - Refresh

    有时候项目代码正确但运行后出现异常,是因为eclipse没有刷新 (如jar包添加了但没用),比较脑慢. 解决方法: clean缓存,或者要多点几次Refresh,或者重启 eclipse.