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. Spring Boot 系列教程7-EasyUI-datagrid

    jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...

  2. Struts中的数据处理的三种方式

    Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...

  3. linux通用邻居基础架构

    1.为每一个协议提供一个缓存来存放L3到L2的转换结果. 2.提供在缓存中添加.删除.改变和查找一个特定映射项的函数.查找函数必须要快,因为它会影响整个系统的性能. 3.为每一个协议缓存的数据项提供一 ...

  4. div里面的margin-top失效

    div标签中的元素margin-top失效的解决方法 元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效,下面有个不错的解决方法, ...

  5. HDU1233--还是畅通工程(最小生成树)

    Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直 ...

  6. cc2530-----串口透明传输分析

    Zigbee协议栈学习之串口透明传输实验(SerialApp)流程分析   第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络 //第一步:Z-Stack 由 main()函数开始执行, ...

  7. FZU 2086 餐厅点餐(枚举)

    ///个人感觉vector好看一点 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  8. CentOS服务端口开放

    #例如开放81端口iptables -I INPUT -i eth0 -p tcp --dport 81 -j ACCEPTiptables -I OUTPUT -o eth0 -p tcp --sp ...

  9. PHP资源类型

    在PHP中,我们经常使用到资源类型变量.例如:mysql连接.文件句柄等. 这些变量无法使用标量来表示,那么在Zend内核中是如何将PHP中的资源变量与C语言中的资源衔接的呢? 一.资源变量在PHP中 ...

  10. filters

    http://www.cloudera.com/content/cloudera/en/documentation/core/latest/topics/admin_hbase_filtering.h ...