A Simple Problem with Integers 线段树 区间更新 区间查询
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 115624 | Accepted: 35897 | |
| 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
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100009
#define L 31
#define INF 1000000009
#define eps 0.00000001
/*
线段树 区间更新区间查询
*/
LL a[MAXN],pre[MAXN];
struct node
{
LL l, r;
LL data, sum, laz;
}T[MAXN*];
void build(LL p, LL l, LL r)
{
T[p].data = T[p].sum = T[p].laz = ;
T[p].l = l, T[p].r = r;
if (l == r) return;
LL mid = (l + r) / ;
build(p * , l, mid);
build(p * + , mid + , r);
}
void update(LL p, LL l, LL r, LL v)
{
//cout << p << ' ' << l << ' ' << r << ' ' << v << endl;
if (T[p].l >= l && T[p].r <= r)
{
T[p].data += v;
T[p].laz = ;
T[p].sum += (T[p].r - T[p].l + ) * v;
return;
}
LL mid = (T[p].l + T[p].r) / ;
if (T[p].laz)
{
T[p].laz = ;
update(p * , T[p].l, mid, T[p].data);
update(p * + , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
update(p * , l, r, v);
else if (l > mid)
update(p * + , l, r, v);
else
{
update(p * , l, mid, v);
update(p * + , mid + , r, v);
}
T[p].sum = T[p * ].sum + T[p * + ].sum;
}
LL query(LL p, LL l, LL r)
{
if (l == T[p].l&&r == T[p].r)
return T[p].sum;
LL mid = (T[p].l + T[p].r) / ;
if (T[p].laz)
{
T[p].laz = ;
update(p * , T[p].l, mid, T[p].data);
update(p * + , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
return query(p * , l, r);
else if (l > mid)
return query(p * + , l, r);
else
return query(p * , l, mid) + query(p * + , mid + , r);
}
LL n, q;
int main()
{
scanf("%lld%lld", &n, &q);
for (LL i = ; i <= n; i++)
scanf("%lld", &a[i]), pre[i] = pre[i - ] + a[i];
char c[];
LL a, b, d;
build(,,n);
while (q--)
{
scanf("%s", c);
if (c[] == 'Q')
scanf("%lld%lld", &a, &b), printf("%lld\n", query(, a, b) + pre[b] - pre[a-]);
else
scanf("%lld%lld%lld", &a, &b, &d), update(, a, b, d);
}
}
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 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 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标记,延迟标记, ...
- A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- IBatis.NET 的配置
http://www.cnblogs.com/xiaogangqq123/archive/2011/06/29/2093250.html http://www.cnblogs.com/hjf1223/ ...
- NLog简单配置与使用
对项目添加NLog 安装完成后,在项目里面会自动引入该引入的dll,并且会添加如下两个文件 NLog的配置主要是在这个config文件里.当然也可以将这个文件里面的nlog节点复制到项目配置文件App ...
- Android 性能优化(14)网络优化( 10)Determining and Monitoring the Connectivity Status
Determining and Monitoring the Connectivity Status This lesson teaches you to Determine if you Have ...
- hbase本地调试环境搭建
1,前言 想要深入的了解hbase,看hbase源码是必须的.以下描述了搭建hbase本地调试环境的经历 2,安装步骤 2.1,启动hbase 1,安装java和IDE IntelliJ,下载源码等. ...
- 16 继续讲C#中的条件执行。if...else if...else
if...else...语句可以让我们判断两种情况.当条件为真的时候,执行一部分:当条件为假的时候,执行另一部分.如果我们需要判断3种,4种,5种情况,那我们应该怎么办呢? 在C#中我们可以 使用if ...
- Position属性四个值:static、fixed、absolute和relative的区别
1.static(静态定位):默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明). 2.relative(相对定位):生成相对 ...
- FCC 基础JavaScript 练习4
1.另一种数据类型是布尔(Boolean).布尔 值要么是true要么是false, 它非常像电路开关, true 是“开”,false是“关”.这两种状态是互斥的 2.伪代码 if(条件为真){ 语 ...
- 联想 S5 Pro(L78041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.123
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- Android yuv转Bitmap
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null); if(image!=nu ...
- Spring scheduled cron 表达式
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...