A Simple Problem with Integers~POJ - 3468
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<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 100005
long long sum[maxn<<],add[maxn<<];
void pushup(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt ,int l) {
if (add[rt]) {
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(l-(l>>));
sum[rt<<|]+=add[rt]*(l>>);
add[rt]=;
}
}
void build(int l,int r,int rt) {
if (l==r) {
scanf("%lld",&sum[rt]);
return ;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void updata(int x,int y,int z,int l,int r,int rt) {
if (x<=l && r<=y) {
add[rt]+=z;
sum[rt]+=(long long)z*(r-l+);
return ;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if (x<=m) updata(x,y,z,l,m,rt<<);
if (y>m) updata(x,y,z,m+,r,rt<<|);
pushup(rt);
}
long long query(int x,int y,int l,int r,int rt) {
long long ans=;
if (x<=l && r<=y ) return sum[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
if (x<=m) ans+=query(x,y,l,m,rt<<);
if (y>m ) ans+=query(x,y,m+,r,rt<<|);
return ans;
}
int main() {
int n,q;
while(scanf("%d%d",&n,&q)!=EOF) {
build(,n,);
char b[];
int x,y,z;
while(q--) {
scanf("%s",b);
if (b[]=='Q') {
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,,n,));
} else {
scanf("%d%d%d",&x,&y,&z);
updata(x,y,z,,n,);
}
}
}
return ;
}
A Simple Problem with Integers~POJ - 3468的更多相关文章
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- C - A Simple Problem with Integers
C - A Simple Problem with Integers POJ - 3468 思路:线段树区间修改区间查询.又出现了 C++ WA G++ AC的尴尬局面. #include& ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
随机推荐
- SDOI2017 Round1
SDOI2017 Round1 在回去的车上写的 cnblog的markdown貌似有bug,空行都没有了 Day -several [清明节] 没想到在省选之前还会有一次放假 放假前一天晚上走到校门 ...
- 2018/2/11 ELK技术栈之ElasticSearch学习笔记二
终于有时间记录一下最近学习的知识了,其实除了写下的这些还有很多很多,但懒得一一写下了: ElasticSearch添加修改删除原理:ElasticSearch的倒排索引和文档一旦生成就不允许修改(其实 ...
- URL中特殊符号的处理
问题描述 我们在对接第三方系统的时候通常需要get或post来传输数据,但此时如果参数中存在&% #*!包括空格等特殊符号的时候就无法正常请求具体表现在参数获取不正确或者获取不到参数,甚至有时 ...
- .net 远程调试
1.我们在调试支付接口的时候很多情况下需要请求网关等,这时候需要在服务器上进行调试 首先找到服务器上的msvsmon文件,打开运行,得到对应的端口 然后打开vs,附加进程到远程地址加上端口,然后附加w ...
- linear-grident的属性和使用
css3新增Gradient属性,用来增加渐变的效果,渐变分为线性渐变 linear-grident 和 径向渐变 radial-grident,这篇文章主要介绍线性渐变linear-grident ...
- java处理json与对象的转化 递归
整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换! 对于对象json转换中遇到的问题我参考了一篇博客,http://blo ...
- [Python Study Notes]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- php实现301跳转
php实现301跳转代码 <?php Header("HTTP/1.1 301 Moved Permanently"); Header("Location: htt ...
- Window Server 布署 WCF 服务 , 权限配置问题
起因: 客户服务器运行环境要求提高安全性,建议数据连接串采取 加密措施 ,或改用 Window 验证 连接数据库服务 .于是我们打算选择后着,将后台服务(Window Server)数据库连接串调整为 ...
- Maven中解决依赖冲突的问题
1.短路优先:谁离得最近就使用谁的依赖jar包 C到达A为C->B->A C到达B为C->B 例如: A中的 commons-io的版本为2.4 B中的commons-io的版本为2 ...