A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3468

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

HINT

题意

区间加,区间查询和

题解:

线段树,记住懒操作~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
struct node
{
int l,r;
ll sum,add;
void fun(ll tmp)
{
add+=tmp;
sum+=(r-l+)*tmp;
}
}a[maxn*];
ll d[maxn];
void relax(int x)
{
if(a[x].add)
{
a[x<<].fun(a[x].add);
a[x<<|].fun(a[x].add);
a[x].add=;
}
}
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
if(l==r)
{
a[x].sum=d[l];
}
else
{
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].sum=a[x<<].sum+a[x<<|].sum;
}
}
void update(int x,int st,int ed,ll c)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
a[x].fun(c);
else
{
relax(x);
int mid=(l+r)>>;
if(st<=mid)update(x<<,st,ed,c);
if(ed>mid) update(x<<|,st,ed,c);
a[x].sum=a[x<<].sum+a[x<<|].sum;
}
}
ll query(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].sum;
else
{
relax(x);
int mid=(l+r)>>;
ll sum1=,sum2=;
if(st<=mid)
sum1=query(x<<,st,ed);
if(ed>mid)
sum2=query(x<<|,st,ed);
return sum1+sum2;
}
}
int main()
{
int n=read(),m=read();
for(int i=;i<=n;i++)
d[i]=read();
build(,,n);
char s[];
int bb,cc,dd;
for(int i=;i<m;i++)
{
scanf("%s",s);
if(s[]=='Q')
{
bb=read(),cc=read();
printf("%lld\n",query(,bb,cc));
}
else
{
bb=read(),cc=read(),dd=read();
update(,bb,cc,dd);
}
}
}

最新文章

  1. 通用数据库操作类,前端easyui-datagrid,form
  2. Netbeans快捷键
  3. “Unable to resolve target &#39;android-9&#39;”的问题
  4. Win7 服务优化个人单机版
  5. 谈谈Android系统启动时的那点事儿
  6. QQ音乐API分析记录
  7. 细说Lucene源码(一):索引文件锁机制
  8. javascript改变背景/字体颜色(Through the javascript to change the background and font color)
  9. css基本属性
  10. 在非gui线程使用QMessageBox
  11. MapReduce 异常 LongWritable cannot be cast to Text
  12. python 字典的常见操作
  13. 设置Nginx+php-fpm显示错误信息
  14. dimens.xml详解
  15. ArrayBlockQueue源码解析
  16. [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III
  17. python-支付宝支付示例
  18. Spring理解IOC,DI,AOP作用,概念,理解。
  19. dubbo 异步回调
  20. 一键查看IE密码!IE PassView简易教程

热门文章

  1. flask插件系列之SQLAlchemy基础使用
  2. Linux内核中的常用宏container_of其实很简单【转】
  3. makefile里PHONY的相关介绍
  4. python的sorted函数对字典按value进行排序
  5. CSS3中的矩阵
  6. [BZOJ4942][Noi2017]整数 线段树+压位
  7. 使用qt+visa实现程控
  8. YAHOO 35条前端优化建议(转)
  9. flutter android沉浸式状态栏
  10. 洛谷P2471 [SCOI2007] 降雨量 [RMQ,模拟]