POJ 3468 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
#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(线段树,区间更新,区间求和)的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- 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 ...
- 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 ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- CSS flex 布局学习笔记
写在前面 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局. 采用 Flex 布局的元素 ...
- java crm 系统 进销存 springmvc SSM项目项目源码
统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM 普 ...
- 搭建阿里云服务器(centos,jdk和Tomcat版本)
1.购买服务器(登录阿里云,购买服务器,并进入控制台,查看自己的服务器实例 2.域名注册(这步可以省略,直接IP地址访问,因为域名需要备案),购买域名的需要进行解析以及绑定自己的服务器 3.可以准备一 ...
- vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
场景: . webpack2.4.*集成vue-loader@15.7.2报错 原因: 参考官方文档 https://vue-loader.vuejs.org/migrating.html#a-plu ...
- Sass:字符串函数-To-upper-case()、To-lower-case()
1.To-upper-case() To-upper-case() 函数将字符串小写字母转换成大写字母.如: //SCSS .test { text: to-upper-case(aaaaa); te ...
- Vue-列表渲染 非变异方法
变异方法 (mutation method),顾名思义,会改变被这些方法调用的原始数组.相比之下,也有非变异 (non-mutating method) 方法,例如:filter(), concat( ...
- 再谈lmbench
摸了一轮ltp-ddt 再回头来看lmbench bandwidth & latency合集小王子 用起来确实方便. 只是官网显示的用法是: Go to the top directory, ...
- GSL+DevC++使用
在DEV C++中配置GSL1.8库 前面写了如何在vs2005中添加gsl,本文所所述为在dev c++中使用gsl库,由实践总结而得. 准备软件: 1.Orwell Dev C++ 5.6.2 N ...
- vfs的super block
super block这个数据结构,乃至super block在磁盘上的位置,是哪里的规定? 没规定,1k偏移只是ext文件系统.但是像fat,它们第0扇区后就是保留扇区,但linux一样要识别它们. ...
- Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用
1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...