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<< ...
随机推荐
- java 序列化和反序列化数据
使用ObjectOutputStream 序列号原始数据和对象数据,使用ObjectInputStream 反序列化 使用字节存储数据,可以将序列化的数据存储到硬盘上,或输出到网络上 package ...
- Xml学习笔记(2)
不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...
- 揭开WebService的神秘面纱
一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...
- hibernate关联关系查询
关联关系 一对一 A中包含B的对象,B中包含A的对象 一对多 A中包含B的集合,B中包含A的对象 多对多 A中包含B的集合,B中包含A的集合 1,一对多配置 一名老师可以对应多名学生 2,模型类 老师 ...
- 《CSS世界》学习笔记(一)
<CSS世界>,张鑫旭著,人民邮电出版社,2017年12月第一版. 1.P9二维码链接文章的demo值得一看,可以实现有关“某些区域颜色始终保持一致”的效果. P9二维码所链接文章的一个d ...
- Pycharm消除波浪线
PyCharm使用了较为严格的PEP8检查规则,稍微有点错误就会出现波浪线提示.那么怎么消除这些波浪线呢?一个简单粗暴的方法就是:在编辑器的右下角有个小人形状的按钮,点开之后有个滚动条,将滚动条滑动到 ...
- 关于串通京东接口的demo
public string Get(int id) { JObject o = new JObject( new JProperty("billNo", "ESL1363 ...
- codeforces_302D
D. Yaroslav and Time time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 分析器错误消息: 该配置节不能包含 CDATA 或文本元素。
原因当然是web.config配置文件中,有字符串文本了,估计不小心加上的一些字符,所以会报错,去掉就行,例如13行的s
- 05Microsoft SQL Server 表创建,查看,修改及删除
Microsoft SQL Server 表创建,查看,修改及删除 创建表 创建普通表 use 数据库名称 go create table 表名称( 列1 ) not null, 列2 ) not n ...