A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 92632   Accepted: 28818
Case Time Limit: 2000MS

Description

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<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) (x<<1)|1
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
LL l,r,mid;
LL sum;
LL add;
}T[N<<2];
LL arr[N];
void pushup(int k)
{
T[k].sum=T[LC(k)].sum+T[RC(k)].sum;
}
void pushdown(int k)
{
T[LC(k)].add+=T[k].add;
T[LC(k)].sum+=T[k].add*(T[LC(k)].r-T[LC(k)].l+1);
T[RC(k)].add+=T[k].add;
T[RC(k)].sum+=T[k].add*(T[RC(k)].r-T[RC(k)].l+1);
T[k].add=0;
}
void build(int k,LL l,LL r)
{
T[k].l=l;
T[k].r=r;
T[k].add=0;
T[k].mid=(T[k].l+T[k].r)>>1;
if(l==r)
T[k].sum=arr[l];
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(LL l,LL r,LL val,int k)
{
if(r<T[k].l||l>T[k].r)
return ;
if(l<=T[k].l&&r>=T[k].r)
{
T[k].add+=val;
T[k].sum+=val*(T[k].r-T[k].l+1);
}
else
{
if(T[k].add)
pushdown(k);
update(l,r,val,LC(k));
update(l,r,val,RC(k));
pushup(k);
}
}
LL query(int k,LL l,LL r)
{
if(l<=T[k].l&&r>=T[k].r)
return T[k].sum;
if(T[k].add)
pushdown(k);
if(r<=T[k].mid)
return query(LC(k),l,r);
else if(l>T[k].mid)
return query(RC(k),l,r);
else
return query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r);
}
int main(void)
{
int tcase,n,i,m;
LL l,r,val;
char ops[3];
while (~scanf("%d%d",&n,&m))
{
MM(arr,0);
for (i=1; i<=n; i++)
scanf("%lld",&arr[i]);
build(1,1,n);
for (i=0; i<m; i++)
{
scanf("%s",ops);
if(ops[0]=='Q')
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",query(1,l,r));
}
else
{
scanf("%lld%lld%lld",&l,&r,&val);
update(l,r,val,1);
}
}
}
return 0;
}

POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)的更多相关文章

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

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

  2. (简单) 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 ...

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

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

  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(线段树区间更新)

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

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

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

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

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  10. 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 ...

随机推荐

  1. cVim—Chrome上更强大的vim插件

    cVim——Chrome上更强大的vim插件 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都差不多,就是在浏览器中提供一些类似vim的操作来提高效率 ...

  2. Android之查看网络图片和网页HTML

    网络编程是Android应用中很重要的一部分,本文主要讲述了利用HttpURLConnection获取网络图片和HTML的方法. 获取网络图片 public class MainActivity ex ...

  3. java 小知识点

    1.转Java中Vector和ArrayList的区别   首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存 ...

  4. php 面向对象之继承、多态和静态方法

    <?php //继承:子类可以继承父类的一切 //特点:单继承 //父类 class Ren { public $name; public $sex; public $yuyan; functi ...

  5. dbca no protocol support

    http://blog.itpub.net/26937943/viewspace-1325094/

  6. Xamarin.Android开发实践(十一)

    Xamarin.Android之使用百度地图起始篇 一.前言 如今跨平台开发层出不穷,而对于.NET而言时下最流行的当然还是 Xamarin,不仅仅能够让我们在熟悉的Vs下利用C#开发,在对原生态类库 ...

  7. 亿条数据在PHP中实现Mysql数据库分表100张

    当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度.笔者做了一个简单的尝试,1亿条数据,分100张表.具体实现过程如下: 首先创建100张表: $i=0; while($i<=9 ...

  8. Azure Mobile Services的REST API调用方式和自定义API

    Azure Mobile Services(移动服务)是微软在Azure平台中提供的一种跨平台的移动应用后端服务,即移动后端即服务.支持.NET和JavaScript(Node.js)写后端代码:支持 ...

  9. WebRTC源码分析三:视频处理流程

    转自:http://blog.csdn.net/neustar1/article/details/19480863 文本介绍视频的处理流程.图1中显示了两路视频会话视频信号流过程. 图1 视频流程示意 ...

  10. matlab练习程序(矩形变换为单连通形状)

    变换使用的模板必须是单连通的,而且模板中心必须在模板内,如果在模板中打个结或是月牙形,这里的程序就处理不了了. 虽然非单连通模板也有办法处理,不过不是这里要讨论的. 这里用到的方法和矩形变换为圆那片文 ...