(简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
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.
题意很好理解,就是标准的线段树区间更新问题,可以说是模板。
不过因为写的不熟练,被坑了两个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 , 线段树+区间更新。的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- 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 ...
- 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 ...
随机推荐
- ubuntu环境下安装Tomcat
tomcat 是javaweb开发的本地服务器,tomcat是目前比较流行的一款. 1.下载Tomcat:http://tomcat.apache.org 2.进入下载文件夹解压Tomcat:sudo ...
- Android自动化测试之环境搭建
Android自动化测试之环境搭建 一.Android-sdk介绍 SDK(Software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台. ...
- CodeForces 383D Antimatter
线性DP. dp[i][j]表示以第i个数字为结尾的,字串和为j的有几种. #include<cstdio> #include<cstring> #include<cma ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 不停止MySQL服务增加从库的两种方式【转载】
现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...
- Centos yum 安装mysql报错 No package mysql-server available.
这是因为大多数mysql-*的资源名称被mariadb-*重命名了 所以换成 yum install mariadb-server 就可以了 PS[摘自网络] MariaDB不仅仅是Mysql的一个替 ...
- docker rmi all
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q)
- jpda
http://www.ibm.com/developerworks/cn/java/j-lo-jpda1/ 远程调试用rmi通信,被调试端需要启动一个调试服务器,用命令jsadebugd. java/ ...
- 在ubuntu 上创建 ssl 证书
soap webservice 调试工具: soap UI, 可以下载下来玩一玩. Introduction TLS, or transport layer security, and its pre ...
- hadoop Yarn运行机制