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<< ...
随机推荐
- [C++ STL] list使用详解
一.list介绍: List由双向链表(doubly linked list)实现而成,元素也存放在堆中,每个元素都是放在一块内存中,他的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得 ...
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- centos服务器/dev/xvda1空间占满的解决方法
突然线上Centos的机器磁盘空间占满报警,第一反映是日志文件很大,占用了较多的磁盘空间.于是简单的上去看了一下.但是发现线上不是的地址对应的空间占的并不多.用:df -h 命令看了一下,/dev/x ...
- Python学习日记之快捷键
Alt+Enter 自动添加包Ctrl+t SVN更新Ctrl+k SVN提交Ctrl + / 注释(取消注释)选择的行Ctrl+Shift+F 高级查找Ctrl+Enter 补全Shift + En ...
- python __slots__ 详解(上篇)
转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...
- LR中日志参数的设置
LR中日志参数的设置 1.Run-Time Setting日志参数的设置 在loadrunner的vuser菜单下的Run-Time Setting的General的LOG选项中可以对在执行脚本时Lo ...
- 梦想CAD控件网页版线型
增加线型 主要用到函数说明: _DMxDrawX::AddLinetype 增加一个线型定义.详细说明如下: 参数 说明 BSTR pszName 线型名 BSTR pszLineDefine 线定义 ...
- oracle中的冷热备份
oracle有四种备份方法:冷备份.热备份.RMAN备份.逻辑备份. 其中冷备份和热备份都是用操作系统命令对oracle文件直接进行拷贝, 不同的是冷备份是把数据库关闭后再备份,备份过程中也要关闭数据 ...
- SQL删除重复数据(根据多个字段),pandas的nan存入数据库报错
delete from M_FACTOR_DATA_TEST a where (a.factor_id,a.data_date,a.stock_code) in (select factor_id,d ...
- unbuntu 安装软件
下载ubutun镜像---------------------用win32diskimager将镜像文件写入u盘,使用u盘启动安装系统. 安装软件--------------------- 0,基本工 ...