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);
}
}
}

poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

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

  3. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

  6. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. webconfig的配置解析

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  2. SQL 变量 条件查询 插入数据

    (本文只是总结网络上的教程) 在操作数据库时 SQL语句中难免会用到变量 比如 在條件值已知的情況下 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值 ...

  3. Appium环境搭建说明(包括报错处理)

    Appium环境搭建说明 一.环境配置 前提是windows系统已安装以下软件: 1.jdk 我装的是1.8.0 2.android-sdk 3.python,3.4-3.6 5.Node.js,v8 ...

  4. PGSql

    http://www.yiibai.com/postgresql/ http://www.postgresql.org/ http://blog.csdn.net/wulex/article/deta ...

  5. centos上git搭建

    1 git的安装需要一些包: yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-Ex ...

  6. Linux 基础目录和命令

    Linux 标准目录结构   初学Linux,首先需要弄清Linux 标准目录结构 / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存 ...

  7. 修改系统时间为UTC时间

    1 拷贝时区文件 cp /usr/share/zoneinfo/Etc/GMT /etc/localtime 2 修改/etc/profile 在最后添加 TZ="Etc/GMT" ...

  8. LightOJ 1370- Bi-shoe and Phi-shoe (欧拉函数)

    题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一个幸运数字, ...

  9. [ JS 进阶 ] Repaint 、Reflow 的基本认识和优化

    你是不是经常听师兄或一些前端前辈说不能用CSS通配符 *,CSS选择器层叠不能超过三层,CSS尽量使用类选择器,书写HTML少使用table,结构要尽量简单-DOM树要小....等这些忠告,以前我就大 ...

  10. MVC – 9.mvc整体请求流程

    1.请求管道 2~5微软自己的验证,我们一般不用. 在全局配置文件中-已经配置一个路由过滤器-为第7个事件注册了路由方法 1.在application_start中向静态路由表注册了路由数据,在管道第 ...