Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 2173  Solved: 951
[Submit][Status][Discuss]

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

裸题

 #include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio> #define N 100007
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,q;
char ch[]; #define ls p<<1
#define rs p<<1|1
struct seg
{
ll sum[N<<],add[N<<];
void build(int p,int l,int r)
{
if (l==r)
{
sum[p]=read();
return;
}
int mid=(l+r)>>;
build(ls,l,mid),build(rs,mid+,r);
sum[p]=sum[ls]+sum[rs];
}
void push_down(int p,int l,int r)
{
if (!add[p]) return;
ll ad=add[p],mid=(l+r)>>;add[p]=;
sum[ls]+=1ll*(mid-l+)*ad;
sum[rs]+=1ll*(r-mid)*ad;
add[ls]+=ad,add[rs]+=ad;
}
ll query(int p,int l,int r,int x,int y)
{
if (l==x&&y==r) return sum[p];
push_down(p,l,r);
int mid=(l+r)>>;
if (y<=mid) return query(ls,l,mid,x,y);
else if (x>mid) return query(rs,mid+,r,x,y);
else return query(ls,l,mid,x,mid)+query(rs,mid+,r,mid+,y);
}
void modify(int p,int l,int r,int x,int y,int z)
{
sum[p]+=1ll*(y-x+)*z;
if (l==x&&y==r)
{
add[p]+=z;
return;
}
int mid=(l+r)>>;
if (y<=mid) modify(ls,l,mid,x,y,z);
else if (x>mid) modify(rs,mid+,r,x,y,z);
else modify(ls,l,mid,x,mid,z),modify(rs,mid+,r,mid+,y,z);
}
}seg;
#undef ls
#undef rs
int main()
{
n=read(),q=read();
seg.build(,,n);
for (int i=;i<=q;i++)
{
scanf("%s",ch);
if (ch[]=='Q')
{
int x=read(),y=read();
printf("%lld\n",seg.query(,,n,x,y));
}
else
{
int x=read(),y=read(),z=read();
seg.modify(,,n,x,y,z);
}
}
}

bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作的更多相关文章

  1. bzoj 3212 Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MB Description You ...

  2. bzoj3212 Pku3468 A Simple Problem with Integers 线段树

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2046  So ...

  3. 3212: Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  So ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  5. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  7. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  8. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  9. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. 3D动态人脸识别技术分析——世纪晟人脸识别实现三维人脸建模

    - 目录 - 国内3D动态人脸识别现状概况 - 新形势下人脸识别技术发展潜力 - 基于深度学习的3D动态人脸识别技术分析 1. 非线性数据建模方法 2. 基于3D变形模型的人脸建模 - 案例结合——世 ...

  2. 【转载】Android 内存溢出如何发生的。

    [转载]Android 内存溢出如何发生的. 且谈Android内存溢出 前言 关于android的内存溢出在创新文档库中也有不少,网络上也有很多这方面的资料.所以这遍文章不算是正真意义上的创新,仅仅 ...

  3. 【转】MySQLroot用户忘记密码解决方案(安全模式,修改密码的三种方式)

    文章出自:http://www.2cto.com/database/201412/358128.html 1.关闭正在运行的MySQL2.启动MySQL的安全模式,命令如下: ? 1 mysqld - ...

  4. File Searching

    Description Have you ever used file searching tools provided by an operating system? For example, in ...

  5. Java学习个人备忘录之文档注释

    文档注释 单行注释用 // 多行注释有两种,第一种是 /* 内容 */,第二种是/** 内容 */. 这两种多行注释的区别是/** 内容 */这种注释可以生成一个该文件的注释文档,下面是演示代码. A ...

  6. node.js安装部署

    node  js  安装部署学习 CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,链接: http ...

  7. android入门 — ListView的优化

    ListView的运行效率是比较低的,因为在getView()中每次都会将整个布局重新加载一遍,当ListView快速滚动的时候就会成为性能瓶颈. 调用View中的findViewById()方法获取 ...

  8. iOS开发改变字符串中指定字符颜色,大小等等

    NSString *strJTGZ = [NSString stringWithFormat:@"交通管制%d处 ",[jtgz intValue]]; NSMutableAttr ...

  9. 3dContactPointAnnotationTool开发日志(六)

      一种可行的思路就是枚举一个模型的三角面片,然后判断三角形是否与另一个物体相交即可.为了让效果更好我想只渲染模型的线框.   在网上查了半天好像Unity里都没有自带的方便的渲染线框的方式,我又自己 ...

  10. hdu3625-Rooms

    题目 有\(n\)个房间,\(n\)个钥匙,每个钥匙随机出现在一个房间里,一个房间里有且仅有一个钥匙.我们现在手上没有钥匙,但我们要搜索所有的房间,所以我们有\(k\)次机会把一个房间炸开.一号房间里 ...