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. Zabbix 源码编译安装

    简介: Zabbix 分布式监控系统,源码编译安装记录 ( 记不得是第多少次了 ) 下载地址:http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX ...

  2. 火狐浏览器的RestClient,接口测试,Post提交数据

    昨天需要测试接口是不是调通,api中本身已经集成了测试,但加了OAuth,api有没有添加头文件,Headers的地方,所以想用RESTClient的Post提交重新测试下,但是,调了好几个小时都没有 ...

  3. unit_2_homework

    随记2018/4/23 # 找元祖中的元素,移除每个元素的空格,并查找以a或A开头,c结尾的所有元素. # 思路:将i取出来,求得li列表中有多少个元素for i in range(len(li)): ...

  4. 使用BlendingTexture实现简单的Terrain

    [使用BlendingTexture实现简单的Terrain] BlendingTexture,就是一张纹理,纹理内存储的数据用于其它去处的Alpha通道value.例如以下四张图需要混合在Terra ...

  5. Writing Surface Shaders

    [Writing Surface Shaders] Writing shaders that interact with lighting is complex. There are differen ...

  6. JAVA中List的三个子类。

    JAVA中List的三个子类分别是:ArrayList,Vector,LinkList.下面就来比较一下他们的不同. ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全,效率高. Ve ...

  7. malloc realloc calloc free

    自上次发现自己对这几个C函数不熟悉,就打算抽空整理一下,也就现在吧.这几个函数都是跟堆内存打交道的,还有一个好玩的函数--alloca,它是跟栈内存打交道的,我想留在以后研究出好玩点的来,再专门为其写 ...

  8. unity在安卓中横屏闪退

    竖屏没问题,横屏闪退 配置文件的AndoridManifest.xml横竖屏设置要和UNITY设置的一致,否则就会强退 UNITY横竖屏设置

  9. 花了好几个小时的奇葩Mat为0问题

    问题 1. Mat mserMat = adaptive_image_from_points(contour, rect); CCharacter character; character.setCh ...

  10. Python 中的 is 和 id-乾颐堂

    (ob1 is ob2) 等价于 (id(ob1) == id(ob2)) 首先id函数可以获得对象的内存地址,如果两个对象的内存地址是一样的,那么这两个对象肯定是一个对象.和is是等价的.Pytho ...