A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions:147133   Accepted: 45718
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std;
int n,m;
const int N=1e5+;
long long s1[N],s2[N];
int lowbit(int x)
{
return x&(-x);
} void updata(int p,long long x)
{
for(int i=p;i<=n;i+=lowbit(i)){
s1[i]+=x;
s2[i]+=x*p;
}
}
long long sum(int p)
{
long long ans=;
for(int i=p;i>;i-=lowbit(i)){
ans+=s1[i]*(p+)-s2[i];
}
return ans;
}
int main()
{
while(scanf("%d %d",&n,&m)==){
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
for(int i=;i<=n;i++){
long long x;
scanf("%lld",&x);
updata(i,x);
updata(i+,-x);
}
while(m--){
char s[];
scanf("%s",s);
if(s[]=='C'){
int a,b;
long long c;
scanf("%d %d %lld",&a,&b,&c);
updata(a,c);
updata(b+,-c);
}
else{
int a,b;
scanf("%d %d",&a,&b);
printf("%lld\n",sum(b)-sum(a-));
}
}
}
return ;
}
 

poj 3468 (区间修改 区间查询)的更多相关文章

  1. A Simple Problem with Integers-POJ3468 区间修改+区间查询

    题意: 给你n个数和2个操作,C操作是将一个区间内的每个数都加上k,Q操作是询问一个区间的和 链接:http://poj.org/problem?id=3468 思路: 线段树区间修改+区间查询 代码 ...

  2. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  3. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

  4. 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询

    题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...

  5. bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...

  6. 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询

    题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...

  7. 题解报告:CODE[VS] 1082 线段树练习3(区间修改+区间查询)

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  8. [线段树]区间修改&区间查询问题

    区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...

  9. 1082 线段树练习 3 && 树状数组区间修改区间查询

    1082 线段树练习 3 题意: 给定序列初值, 要求支持区间修改, 区间查询 Solution 用树状数组, 代码量小, 空间占用小 巧用增量数组, 修改时在 \(l\) 处 $ + val$ , ...

随机推荐

  1. 在spring添加注解时,第一行package报错configure build path

    练习spring的ioc的注解的时候写上注解就会在第一行package报错configure build path. 用的spring4.2.4的jar包.经过上网查阅资料,可能是jar包冲突,解决办 ...

  2. vue入门学习示例

    鄙人一直是用angular框架的,所以顺便比较了一下. <!DOCTYPE html> <html lang="en"> <head> < ...

  3. one or more listeners failed to start问题解决思路

    今日搭建一个web应用的时候总是遇到tomcat报错:one or more listeners failed to start. Full detail balabale....而且还没有其他提示, ...

  4. 10.vue router 带参数跳转

    vue router 带参数跳转 发送:this.$router.push({path:'/news',query:{id:row.id}}) 接收:var id=this.$route.query. ...

  5. 产品 | What's产品经理

    如果想知道什么是产品,首先需要知道什么是缔造者.其名曰:"产品经理". PS:产品经理一词在国内大多时候泛指"互联网产品经理". 对于产品经理这一职位,说实在很 ...

  6. iOS7下Status Bar字体颜色修改

    原文来自这里:iOS7下Status Bar字体颜色修改. 旧项目在iOS7上遇到status bar字体颜色需要修改的问题,症状如下:导航栏设置为黑色后,iphone上status bar的字体颜色 ...

  7. c++string标准输入和getline()整行读入

    1.使用标准IO操作读写string对象 我们都知道,使用标准iostream操作来读写int ,double等内置类型的值,同样,我们也可以使用IO操作来读写string对象. c++ code: ...

  8. CentOS7.5二进制安装MySQL-5.6.40

    安装依赖 yum install -y gcc gcc-c++ automake autoconf yum -y install cmake bison-devel ncurses-devel lib ...

  9. 关于windows下安装mysql数据库出现中文乱码的问题

    首先需要在自己安装的mysql路径下新建一个my.ini文件,如下: 然后在my.ini文件中输入一下内容,主要控制编码问题的为红框部分,如下: 为了方便大家使用,可以复制以下代码: [WinMySQ ...

  10. Cannot send session cache limiter - headers already sent问题

    在php.ini中将“always_populate_raw_post_data ”设置为“-1”,并重启