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. 关于Python3中函数:

    # 关于Python3中函数: - 定义 定义函数使用关键字def,后接函数名和放在圆括号()中的可选参数列表,函数内容以冒号起始并且缩进.一般格式如下:``` def 函数名(参数列表): &quo ...

  2. 20届的阿里 头条 网易 滴滴 百度 小米等Java面经

    转载连接 个人博客:junxuelian.cn 总结:个人感觉回答面试官问题不必太官方和书面化,腾讯sng招实习被发现照着百度百科念.结果可想而知.用自己的话和理解去回答就好.可能应届生会抱怨设计题, ...

  3. DAY3敏捷冲刺

    站立式会议 工作安排 (1)服务器配置 (2)数据库配置 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录

  4. OSG学习:移动/缩放/旋转模型

    移动和缩放以及旋转都是对矩阵进行操作,这些操作如果要叠加直接矩阵相乘就可以了. 下面的示例代码中,加入了四个bignathan,一个是默认加入在最中间,一个向上移2单位,一个是向下移2单位且缩放0.5 ...

  5. C# WebBrowser控件详解

     作者:827969653     0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...

  6. AjaxPro怎么用

    AjaxPro是首家支持以各种方式通过javascript访问服务端.net的免费库,类似于SAJAX.它能把Javascript请求发送到.NET方法,服务端传回给Javascript,甚至包括串行 ...

  7. 3dContactPointAnnotationTool开发日志(一)

      周日毕设开题报告结束后浪了一天,今天又要开始回归正轨了.毕设要做一个人和物体的接触点标注工具,听上去好像没啥难度,其实实现起来还是挺麻烦的.   今天没做啥,就弄了个3d场景做样例.把界面搭了一下 ...

  8. Dom的样式操作和属性操作

    如果说web的研究对象是html和css,那么整个dom结构,包含html树和dom树的dom结构才是研究对象,而在整个页面呈现上面,js起到的作用则是异步的用户行为. 按照上面整个思路,获取dom元 ...

  9. 【Linux】- rm命令

    Linux rm命令用于删除一个文件或者目录. 语法 rm [options] name... 参数: -i 删除前逐一询问确认. -f 即使原档案属性设为唯读,亦直接删除,无需逐一确认. -r 将目 ...

  10. opencv里vector的内存的申请和释放的问题

    改成: ); CvSeq * m_contour=; cvFindContours( &IPlImage(img), m_storage, &m_contour, ,)); //释放内 ...