POJ - 3468 A Simple Problem with Integers (线段树区间更新---间接修改)
题意:区间add,区间求和。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL a[MAXN];
LL sum[MAXN << 2];
LL lazy[MAXN << 2];
void build(int id, int L, int R){
if(L == R){
sum[id] = a[L];
}
else{
int mid = L + (R - L) / 2;
build(id << 1, L, mid);
build(id << 1 | 1, mid + 1, R);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
void pushdown(int id, int L, int R){
if(lazy[id]){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
int mid = L + (R - L) / 2;
sum[id << 1] += (mid - L + 1) * lazy[id];
sum[id << 1 | 1] += (R - mid) * lazy[id];
lazy[id] = 0;
}
}
void update(int l, int r, int id, int L, int R, LL v){
if(l <= L && R <= r){
lazy[id] += v;
sum[id] += (R - L + 1) * v;
}
else{
pushdown(id, L, R);
int mid = L + (R - L) / 2;
if(l <= mid) update(l, r, id << 1, L, mid, v);
if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
LL query(int l, int r, int id, int L, int R){
if(l <= L && R <= r){
return sum[id];
}
pushdown(id, L, R);
int mid = L + (R - L) / 2;
LL ans = 0;
if(l <= mid) ans += query(l, r, id << 1, L, mid);
if(r > mid) ans += query(l, r, id << 1 | 1, mid + 1, R);
return ans;
}
int main(){
int N, Q;
scanf("%d%d", &N, &Q);
for(int i = 1; i <= N; ++i){
scanf("%lld", &a[i]);
}
build(1, 1, N);
while(Q--){
char cc;
int a, b;
getchar();
scanf("%c%d%d", &cc, &a, &b);
if(cc == 'C'){
int c;
scanf("%d", &c);
update(a, b, 1, 1, N, (LL)c);
}
else{
printf("%lld\n", query(a, b, 1, 1, N));
}
}
return 0;
}
POJ - 3468 A Simple Problem with Integers (线段树区间更新---间接修改)的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #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 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- 设计模式课程 设计模式精讲 20-2 解释器模式coding
1 代码演练 1.1 代码演练1(解释器模式coding) 1.2 代码演练如何应用了解释器模式 1 代码演练 1.1 代码演练1(解释器模式coding)(该案例运用了栈的先进先出的特性) 需求: ...
- 单例设计模式和main方法
设计模式就是在大量的实践中总结和理论之后优选的代码结构.编程风格.以及解决问题的思考方式. 说白了设计模式就是在实际编程中逐渐总结出的解决问题的套路,类似于数学公式. 类的单例设计模式:在开发过程中有 ...
- 夯实Java基础(十五)——Java中Comparable和Comparator
1.前言 对于Java中的对象,我们只能使用基本运算符==.!=来判断一下地址是否相等,不能使用>.<来比较大小.但是在实际的开发中,我们需要对对象进行排序,也就是比较大小,那么应该如何实 ...
- ROS学习笔记8-rqt_console和roslaunch
本教程来自于:http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch rqt_console 和 rqt_logger_level 是ro ...
- RabbitMq学习笔记——配置
1.RabbitMq server官网下载地址:https://www.rabbitmq.com 2.Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安 ...
- @vue-cli的安装及vue项目创建
1.安装 Node.js & Vue CLI @vue/cli3,是vue-进行搭建的脚手架项目,它本质上是一个全局安装的 npm 包,通过安装它,可以为终端提供 vue 命令,进行vue项目 ...
- javaweb框架--自定义标签与freemaker结合
http://blog.csdn.net/myfmyfmyfmyf/article/details/8960299 很有用但是不不知道怎么说,写个例子,总之方便多了,并且容易管理,重复利用强 1.自定 ...
- 校园服务APP使用体验
校园服务APP软件使用体验 一.概况: 校园服务这款软件是针对大学生这一群体量身打造,具有简洁的界面,能让使用者快速上手,不存在第一次使用发懵,发怵等情况,是一款非常实用的功能软件. 二.功能特性: ...
- Navicat导入json文件到数据库
最近做小程序商城系统,物流管理这块需要存储国际和中国的物流地址. 所以,天哪,地址那么的多!!!! www.baidu.com,搜索大佬是如何做的.有的是通过访问阿里云快递物流api接口获取数据存入数 ...
- 三 进制、精度,Java的类型转换
进制的表示: 0b010 : 二进制表示形式:前面+0n 0100 : 八进制表示形式: 前面+0 0x001 : 16进制表示形式:前面+0x 计算机以补码的方式进行运算 进制的转换: 10进制 ...