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.

  题意很好理解,就是标准的线段树区间更新问题,可以说是模板。

  不过因为写的不熟练,被坑了两个WA,错的地方在代码里标出来了。。。

代码如下:

#include<iostream>
#include<cstdio> #define lson L,M,po*2
#define rson M+1,R,po*2+1 using namespace std; long long BIT[*];
long long COL[*]; void pushUP(int po)
{
BIT[po]=BIT[po*]+BIT[po*+];
} void pushDown(int po,int len)
{
if(COL[po])
{
BIT[po*]+=(long long)COL[po]*(len-(len/));
BIT[po*+]+=(long long)COL[po]*(len/); COL[po*]+=COL[po]; //注意是+=,不是= !!!
COL[po*+]+=COL[po];
COL[po]=;
}
} void build_tree(int L,int R,int po)
{
if(L==R)
{
COL[po]=;
cin>>BIT[po];
return;
} int M=(L+R)/; build_tree(lson);
build_tree(rson); pushUP(po);
} long long query(int ql,int qr,int L,int R,int po)
{
if(ql<=L&&qr>=R)
return BIT[po]; pushDown(po,(R-L+)); int M=(L+R)/; if(qr<=M)
return query(ql,qr,lson);
if(ql>M)
return query(ql,qr,rson); return query(ql,qr,rson)+query(ql,qr,lson);
} void update(int ul,int ur,int add,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
BIT[po]+=(long long)add*(R-L+);
COL[po]+=(long long)add; return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update(ul,ur,add,lson);
if(ur>M)
update(ul,ur,add,rson); pushUP(po);
} int main()
{
int N,Q;
char C;
int a,b,c; while(~scanf("%d %d",&N,&Q))
{
build_tree(,N,); for(int i=;i<Q;++i)
{
cin>>C; if(C=='Q')
{
scanf("%d %d",&a,&b); cout<<query(a,b,,N,)<<endl;
}
else
{
scanf("%d %d %d",&a,&b,&c);
update(a,b,c,,N,);
}
}
} return ;
}

(简单) 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 [线段树区间更新求和]

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

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

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

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

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

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

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

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

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

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

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

  9. 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. Struts2值栈详解

    1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的.  2). ...

  2. centos yum源问题

    在配置CentOS-6.0-x86_64-bin-DVD2.iso作为本地yum源的时候,碰到相当多的问题: -----------------------------------------  问题 ...

  3. PAT (天梯)L2-004. 这是二叉搜索树吗?

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...

  4. unit正交相机Size的计算公式

    如:相机的大小为800*480,要使相机适应800*480像素的图,则 Size = 相机高/2/像素单位 = 480/2/100 = 2.4

  5. 使用PreListener与InteractionListener的一个小发现

    如果两个刚体使用PreListener忽略了它的碰撞时,如果这时你使用InteractionListener来侦听它们的碰撞还是可以侦听到的: package { import com.bit101. ...

  6. 转:通过ant来批量执行jmeter脚本,并生成报告(附: 生成报告时报“Content is not allowed in prolog”这个错误的解决方案)

    最近在使用jmeter写脚本来进行测试,最终写了很多份脚本,然后,就在想,这么多脚本,我不可能一个一个的手动去点啊,有没有什么办法来批量运行Jmeter脚本呢? 这个时候,自然而然地想到了万能的ant ...

  7. rabbitmq用于分布式系统

    上文介绍了RabbitMQ在linux下的安装,这里就简单的介绍一下基于RabbitMQ的开发.RabbitMQ已经提供了一大坨材料. Java – http://www.RabbitMQ.com/j ...

  8. springMVC简单的安全防御配置

    1,使用 spring form 标签 防 csrf 攻击 2,标明请求方法:RequestMethod.GET,RequestMethod.POST, PATCH, POST, PUT, and D ...

  9. C++零食:使用Unicode版的预定义宏__FUNCTION__

    在C++中支持一种预定义宏.比如: __FILE__: 就是当前源代码文件名 __LINE__: 就是当前源代码的行号 这些宏可以在printf等语句中直接作为字符串使用,调试的时候很方便. 如下代码 ...

  10. DOM4j 操作XML

    <?xml version="1.0" encoding="GBK"?> <persons> <men> <perso ...