A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 67511   Accepted: 20818
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.
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = ;
int n , m ;
LL sum[N<<] , lazy[N<<] , e[N] , tot ; void Up( int rt ) {
sum[rt] = sum[lr] + sum[rr];
}
void Down( int l , int r , int rt ) {
if( lazy[rt] != ) {
int mid = (l+r)>>;
sum[lr] += lazy[rt]*(mid-l+) , sum[rr] += lazy[rt]*(r-mid);
lazy[lr] += lazy[rt] , lazy[rr] += lazy[rt];
lazy[rt] = ;
}
}
void build( int l , int r , int rt ){
lazy[rt] = ;
if( l == r ) {
sum[rt] = e[tot++];
return ;
}
int mid = (l+r)>>;
build(lson),build(rson);
Up(rt);
}
void update( int l , int r , int rt , int L , int R , LL val ) {
if( L == l && r == R ) {
sum[rt] += val*(r-l+) ;
lazy[rt] += val;
return ;
}
Down( l , r , rt );
int mid = (l+r)>>;
if( R <= mid ) update(lson,L,R,val);
else if( L > mid ) update(rson,L,R,val);
else update(lson,L,mid,val) , update(rson,mid+,R,val);
Up(rt);
}
LL query( int l , int r , int rt , int L , int R ) {
if( L == l && r == R ) {
return sum[rt];
}
Down(l,r,rt);
int mid = (l+r)>>;
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+,R);
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
char s[]; int x , y ; LL c ;
while( ~scanf("%d%d",&n,&m ) ) {
tot = ;
for( int i = ; i < n ; ++i ) {
scanf("%I64d",&e[i]);
}
build(root);
while(m--) {
scanf("%s",s);
if( s[] == 'Q' ) {
scanf("%d%d",&x,&y);
printf("%I64d\n",query(root,x,y));
}
else {
scanf("%d%d%I64d",&x,&y,&c);
update(root,x,y,c);
}
}
}
}

POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  4. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

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

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

  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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  9. poj 3468 A Simple Problem with Integers 线段树加延迟标记

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

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

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

随机推荐

  1. Java 8实战之读书笔记四:高效Java 8编程

    三.高效Java 8编程 第8章 重构.测试和调试 Java 8的新特性也可以帮助提升代码的可读性:      使用Java 8,你可以减少冗长的代码,让代码更易于理解      通过方法引用和S ...

  2. 攻防世界--re1

    练习文件下载:https://www.lanzous.com/i5lufub 1.使用IDA打开,进入main函数. 2.转为C代码 可以看到,输入v9之后,与v5比较,判断我们输入的flag是否正确 ...

  3. Vue PC端图片预览插件

    *手上的项目刚刚搞完了,记录一下项目中遇到的问题,留做笔记: 需求: 在项目中,需要展示用户上传的一些图片,我从后台接口拿到图片url后放在页面上展示,因为被图片我设置了宽度限制(150px),所以图 ...

  4. gvfs错误导致tilda和thunar启动缓慢问题的解决

    tilda是一个非常轻便的下拉终端,但是安装之后启动发现要过十几秒才会出现界面.命令行启动发现报错如下: 用这条信息到处搜索也找不到有用的解答. 后来终于发现这是一个dbus超时的问题,虽然原因和这个 ...

  5. restful接口风格

    一.定义 REST全称是Representational State Transfer, 中文意思是表述性状态转移. REST指的是一组架构约束条件和原则,如果一个架构符合REST的约束条件和原则,我 ...

  6. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  7. Apache Flink 的迁移之路,2 年处理效果提升 5 倍

    一.背景与痛点 在 2017 年上半年以前,TalkingData 的 App Analytics 和 Game Analytics 两个产品,流式框架使用的是自研的 td-etl-framework ...

  8. 08-图7 公路村村通(30 分)Prim

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N) ...

  9. Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能

    一.部署Zabbix监控平台 目标: 本案例要求部署一台Zabbix监控服务器,一台被监控主机,为进一步执行具体的监控任务做准备: 在监控服务器上安装LAMP环境    修改PHP配置文件,满足Zab ...

  10. BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树分治+并查集)

    传送门 解题思路 可以离线,然后确定每个边的出现时间,算这个排序即可.然后就可以线段树分治了,连通性用并查集维护,因为要撤销,所以要按秩合并,时间复杂度\(O(nlog^2 n)\) 代码 #incl ...