A Simple Problem with Integers

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<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
const int MAXN=;
char s[],c;
long long tree[MAXN*]={},add[MAXN*]={};
int t,num,x,n,y,m,z,a[MAXN]={};
using namespace std;
void build(int l,int r,int p){
if (l==r) tree[p]=a[l];
else {
int mid=(l+r)>>;
build(l,mid,p*);
build(mid+,r,p*+);
tree[p]=tree[p*]+tree[p*+];
}
}
long long query(int l,int r,int p,int x,int y){
if (l==x&&r==y) return tree[p]+add[p]*(r-l+);
else {
add[p*+]+=add[p];
add[p*]+=add[p];
tree[p]+=add[p]*(r-l+);
add[p]=;
int mid=(l+r)>>;
if (y<=mid) return query(l,mid,p*,x,y);
else if(x>=mid+) return query(mid+,r,p*+,x,y);
else return query(l,mid,p*,x,mid)+query(mid+,r,p*+,mid+,y);
}
}
void change(int l,int r,int p,int x,int y,int zhi)
{
if (l==x&&r==y) add[p]+=zhi;
else
{
tree[p]+=zhi*(y-x+);
int mid=(l+r)>>;
if (y<=mid) change(l,mid,p*,x,y,zhi);
else if (x>mid) change(mid+,r,p*+,x,y,zhi);
else
{
change(l,mid,p*,x,mid,zhi);
change(mid+,r,p*+,mid+,y,zhi);
}
}
}
int main(){
while (~scanf("%d%d",&n,&m)){
for (int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,n,);
for (int i=;i<=m;i++){
scanf("%s%d%d",&s,&x,&y);
if (s[]=='Q') printf("%lld\n",query(,n,,x,y));
else {
scanf("%d",&z);
change(,n,,x,y,z);
}
}
}
}

线段树虽然简单,但是树状数组就没有这么简单了,差分思想。

转换应该在树状数组blog上已经讲过了,会在blog上持续更新,最新的理解。

 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iostream>
#define N 100007
using namespace std; int n,m;
long long c[N],d[N];
char ch[]; int lowbit(int x)
{
return x&(-x);
}
void change(int x,int y,int z)
{
for (int i=x;i<=n;i+=lowbit(i))
c[i]+=z;
for (int i=y+;i<=n;i+=lowbit(i))
c[i]-=z; for (int i=x;i<=n;i+=lowbit(i))
d[i]+=(long long)z*x;
for (int i=y+;i<=n;i+=lowbit(i))
d[i]-=(long long)z*(y+);
}
long long query(int x)
{
long long res=,ans=;
for (int i=x;i>=;i-=lowbit(i))
res+=c[i];
ans+=res*(x+);
res=;
for (int i=x;i>=;i-=lowbit(i))
res+=d[i];
ans-=res;
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
int last=,x,y,z;
for (int i=;i<=n;i++)
{
scanf("%d",&x);
y=x-last;
change(i,n,y);
last=x;
}
for (int i=;i<=m;i++)
{
scanf("%s",ch);
if (ch[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(y)-query(x-));
}
else
{
scanf("%d%d%d",&x,&y,&z);
change(x,y,z);
}
}
}

poj3468(一维)(区间查询,区间修改)的更多相关文章

  1. Libre OJ 130、131、132 (树状数组 单点修改、区间查询 -> 区间修改,单点查询 -> 区间修改,区间查询)

    这三题均可以用树状数组.分块或线段树来做 #130. 树状数组 1 :单点修改,区间查询 题目链接:https://loj.ac/problem/130 题目描述 这是一道模板题. 给定数列 a[1] ...

  2. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  3. HZAU 1207 Candies(线段树区间查询 区间修改)

    [题目链接]http://acm.hzau.edu.cn/problem.php?id=1207 [题意]给你一个字符串,然后两种操作:1,将区间L,R更新为A或者B,2,询问区间L,R最长的连续的B ...

  4. POJ3468(线段树 区间修改 lazy-tag)

    我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...

  5. 线段树 区间查询区间修改 poj 3468

    #include<cstdio> #include<iostream> #include<algorithm> #include<string.h> u ...

  6. POJ - 2528 区间离散化,线段树区间修改,区间询问

    这个题非常有意思的地方是,我们发现区间[1,4]和[5,8]是紧挨着的,因为这个的数代表的是一段区间,原本我们对于普通的离散, a[1]=1,a[2]=5,a[3]=6,a[4]=8;数组下标就是重新 ...

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

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

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

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

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

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

  10. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

随机推荐

  1. LVS之DR模式实战及高可用性

    author:JevonWei 版权声明:原创作品 LVS-DR实现同网段调度web模式 拓扑环境 网络环境 RS1 RIP 192.168.198.138/24 VIP 192.168.198.10 ...

  2. 10个经典的Java面试题集合(转载)

    1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  3. java中super关键字

    1.子类的构造函数如果要引用super的话,必须把super放在函数的首位,如果想用super继承父类构造的方法,但是没有放在第一行的话,那么在super之前的语句,肯定是为了满足自己想要完成某些行为 ...

  4. Java基础学习 —— io

    /** 解决数据与数据之间的传输问题. 字节流: 输入字节流: -------| InputStream 所有输入字节流的基类.抽象类. -----------| FileInputStream 读取 ...

  5. 如何实现一个 Virtual DOM 及源码分析

    如何实现一个 Virtual DOM 及源码分析 Virtual DOM算法 web页面有一个对应的DOM树,在传统开发页面时,每次页面需要被更新时,都需要手动操作DOM来进行更新,但是我们知道DOM ...

  6. Prism for Xamarin.Forms

    一.使用环境 OS:Win 10 16273 VS:VS2017- 15.3.4 Xamarin:4.6.3.4,nuget:2.4 Android Emulator:Visual Studio fo ...

  7. python-分页代码

    page.py ''' django内使用方式: all_count = models.UserInfo.objects.all().count() # path_info 当前页的url # all ...

  8. springboot配置swagger2

    .在pom.xml里添加jar包: <dependency> <groupId>io.springfox</groupId> <artifactId>s ...

  9. 线程高级篇-Lock锁和Condition条件

    浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...

  10. 201521123012 《Java程序设计》第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...