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

题意

区间加,区间查询和

题解:

入门模板题,学了这么久的线段树,竟然打了20分钟,而且还debug了,果然还是太菜了。

忘记建树导致re,忘记return ans。虽然是细节错误,但是说明自己还是不够熟练。什么时候才可以一遍打完,不用编译直接交,不需看结果,帅气切题啊。

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 200050
#define ll long long
int n,m; ll w[N];
struct Tree{int l,r;ll j,sum;}tr[N<<];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void push_up(int x)
{
ll len=tr[x].r-tr[x].l+;
tr[x].sum=len*tr[x].j;
if (len==)return;
tr[x].sum+=tr[x<<].sum+tr[x<<|].sum;
}
void push_down(int x)
{
Tree &a=tr[x<<],&b=tr[x<<|];
a.j+=tr[x].j;
b.j+=tr[x].j;
push_up(x<<);
push_up(x<<|);
tr[x].j=;
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].j=;
if (l==r)
{
tr[x].j=w[l];
push_up(x);
return;
}
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid+,r);
push_up(x);
}
void update(int x,int l,int r,ll tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].j+=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if(l<=mid)update(x<<,l,r,tt);
if (mid<r)update(x<<|,l,r,tt);
push_up(x);
}
ll query(int x,int l,int r)
{
if(l<=tr[x].l&&tr[x].r<=r)
return tr[x].sum;
ll ans=,mid=(tr[x].l+tr[x].r)>>;
push_down(x);
if (l<=mid)ans+=query(x<<,l,r);
if (mid<r)ans+=query(x<<|,l,r);
push_up(x);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n); read(m);
for(int i=;i<=n;i++)read(w[i]);
bt(,,n);
for(int i=;i<=m;i++)
{
char id;
int x,y; ll tt;
read_char(id);read(x);read(y);
if (id=='C')read(tt),update(,x,y,tt);
if (id=='Q')printf("%lld\n",query(,x,y));
}
}

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. MaxScript 学习笔记【有转载】

    1. string string类型的变量是一个数组,下列操作合法:strName = $.name -- output: "Shemmy_03" strName[1] --得到字 ...

  2. innobackupex 备份数据搭建 MySQL Slave

    简介: 数据量比较大时,使用 innobackupex 备份数据新增 MySQL Slave 节点. 安装 innobackupex 工具,我这里写过一次:http://www.cnblogs.com ...

  3. MongoDB常用增删改查语句

    数据库database 创建及查看库 1.有则使用这个数据库,没有就创建 use DATABASE_NAME 2. 查看当前选择的数据库,默认是test db 3.查看数据库,默认有admin.loc ...

  4. plsql中的光标

    操作oracle数据库效率最高的语言就是plsql程序,故而把访问数据库的代码写成plsql的执行效率要高于java,c ,c++等代码

  5. java算法 第七届 蓝桥杯B组(题+答案) 9.取球博弈

    9.取球博弈  (程序设计) 两个人玩取球的游戏.一共有N个球,每人轮流取球,每次可取集合{n1,n2,n3}中的任何一个数目.如果无法继续取球,则游戏结束.此时,持有奇数个球的一方获胜.如果两人都是 ...

  6. Vue+WebPack游戏设计:自动背景贴图和游戏主循环的实现

  7. 142. Linked List Cycle II (List; Two-Pointers)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  8. PHP异常处理详解

      PHP异常处理详解 异常处理(又称为错误处理)功能提供了处理程序运行时出现的错误或异常情况的方法. 异常处理通常是防止未知错误产生所采取的处理措施.异常处理的好处是你不用再绞尽脑汁去考虑各种错误, ...

  9. 线上服务内存OOM问题定位

    转自:架构师之路,http://mp.weixin.qq.com/s/iOC1fiKDItn3QY5abWIelg 相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的 ...

  10. XStream(xml/bean转换)

    XStream 1. 什么作用  * 可以把JavaBean转换为(序列化为)xml 2. XStream的jar包  * 核心JAR包:xstream-1.4.7.jar:  * 必须依赖包:xpp ...