POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 163977 | Accepted: 50540 | |
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<algorithm>
#include<string.h>
#include<string>
#define ll long long
using namespace std;
ll tree[], lazy[], len[];//tree[num]存的是节点num所在区间的区间和
void pushdown(ll num)
{
if (lazy[num] != )
{
tree[num * ] = tree[num * ] + lazy[num] * len[num * ];
tree[num * + ] = tree[num * + ] + lazy[num] * len[num * + ];
lazy[num * ] = lazy[num * ] + lazy[num];
lazy[num * + ] = lazy[num * + ] + lazy[num];
lazy[num] = ;
}
} void build(ll num, ll le, ll ri)
{
len[num] = ri - le + ;//区间长度
if (le == ri)
{
scanf("%lld", &tree[num]);
return;
}
ll mid = (le + ri) / ;
build(num * , le, mid);
build(num * + , mid + , ri);
tree[num] = tree[num * ] + tree[num * + ];
} void update(ll num, ll le, ll ri, ll x, ll y, ll z)
{
if (x <= le && ri <= y)
{
lazy[num] = lazy[num] + z;
tree[num] = tree[num] + len[num] * z;//更新区间和
return;
}
pushdown(num);
ll mid = (le + ri) / ;
if (x <= mid)
update(num * , le, mid, x, y, z);
if (y > mid)
update(num * + , mid + , ri, x, y, z);
tree[num]=tree[num*]+tree[num*+];
} ll query(ll num, ll le, ll ri, ll x, ll y)
{
if (x <= le && ri <= y)//查询区间在num节点所在区间内
return tree[num];
pushdown(num);
ll mid = (le + ri) / ;
ll ans = ;
if (x <= mid)
ans = ans + query(num * , le, mid, x, y);
if (y > mid)
ans = ans + query(num * + , mid + , ri, x, y);
return ans;
}
int main()
{
ll n, m;
scanf("%lld%lld", &n, &m);
build(, , n);
while (m--)
{
char c[];
scanf("%s", c);
if (c[] == 'Q')
{
ll x, y;
scanf("%lld%lld", &x, &y);
printf("%lld\n", query(, , n, x, y));
}
else
{
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
update(, , n, x, y, z);
}
}
return ;
}
POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers的更多相关文章
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- 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: 97217 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- 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 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- [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(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
随机推荐
- 学习笔记(15)- 保险行业的问答语料 insuranceqa_data
数据概览 ''' pool data are translated Chinese data with Google API from original English data ''' POOL_T ...
- Linux命令:ifconfig命令
ifconfig功能:配置网络接口,CentOS7最小化安装不包含此命令,需要安装net-tools包. ifconfig常见的用法: ifconfig :显示所有活动接口的相关信息 ifconfig ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- Xftp和Xshell
Xftp 用于跟云虚拟机文件传输的工具,Xftp官网 Xshell Xftp的兄弟工具,用于执行云虚拟机命令,Xshell官网 两个都是付费工具,各需要几百块钱,当然破解的版本也很多 登陆 这两个的连 ...
- Centos7 安装redis及简单使用
一.redis的介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...
- shell脚本中执行sql脚本并传递参数(mysql为例)
1.mysql脚本文件 t.sql insert into test.t values(@name,@age); exit 2.shell脚本文件 a.sh (为方便演示,与t.sql文件放在同一目 ...
- HTML相关知识点(2)
CSS: 字体: 网页默认字体16px; 网站通用字体大小14px 最小是12px,最大无限大 单位换算:1em=16px 选择器:标签选择器:选择页面中所有指定标签,权重为1 通配符选择器:选择所 ...
- ubuntu 用管理员身份进入系统
我们需要以管理员的身份进入系统,才能在系统中畅通无阻 以管理员的身份进入系统,往下看: Ubuntu安装好后,root初始密码(默认密码)不知道,需要设置. 1.先用安装Ubuntu的时候创建 ...
- HBase查询速度慢原因排查
问题:通过HBase访问服务在HBase中查询 ASSET_NORMAL 表速度很慢 如下,查询一条数据需要2.970s时间: 如下,统计总条数需要14.675s时间: HBase访问服务部署了3个节 ...
- storm正常任务数据量过大时造成定时任务不执行的问题
在执行storm的定时任务时,发现storm普通任务数据量过大时造成定时任务不执行, 同时设置了storm的普通任务和定时任务,定时任务设置5秒执行1次. 普通任务的数据时从spout中不停发射字符串 ...