(简单) 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 ...
随机推荐
- linux视频学习3(shell和网络)
1.shell的学习. shell的种类比较多,主要有三种: /bin/sh, /bin/csh, /bin/ksh. 查看当前使用的是哪种shell : 命令env (显示当前操作系统的环境变量). ...
- 让横向ul在页面中水平居中的方法
在导航的布局中,导航条会用横向布局的ul li.如果要让其居中,怎么办呢? 第一种方法: ul{text-align:center;} li{display:inline} 这种方法不适合ie低版本. ...
- android 定时器总结
1:handler实现定时器的功能 Handler handler=new Handler(); //立即执行Runnable对象 public final boolean post(Runnab ...
- hb_gui配置heartbeat做MariaDB的高可用
系统平台:CentOS release 6.5 (Final) Kernel:2.6.32-431.el6.x86_64 一.启动hb_gui hb_gui & 添加资源组 添加MySQL_I ...
- C++中运行外部程序
关于三个SDK函数: WinExec, ShellExecute,CreateProcess 的其他注意事项: [1]定义头文件 必须定义以下两个头文件: #include <shlobj.h& ...
- AJAX(XMLHttpRequest)进行跨域请求方法详解(三)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 3,带验证信息的请求 身份验证是Web开发中经常遇到的问题,在跨 ...
- JSP标准标签库(JSTL)--函数标签库 fn
和String的方法类似,就是对String的一种封装. No. 函数标签名称 描述 1 ${fn:contains()} 查询某字符串是否存在,区分大小写 2 ${fn:containsIgnore ...
- 在线更新问题 HDU5877 线段树
题目大意:给你一棵树,有n-1条边,每条边都有方向,每个顶点有权值,给出weak pair的定义是val[u]*val[v] <=k,u是v的祖先,问有多少对这样的顶点 思路:创建线段树,通过d ...
- virtualbox, vt-s, rmmod kvm-intel
http://blog.sina.com.cn/s/blog_4e7b97f00100fltu.html装完后,去下个jdk,坑爹的Oracle非要让老子注册,试了几次都没有成功,密码不合格拉,邮箱已 ...
- FusionCharts使用问题及解决方法(四)-FusionCharts常见问题大全
在前3篇文章中,我们总结了FusionCharts图表的一些常见问题(FAQ)及解决方法,本文继续讨论FusionCharts使用者常见的一些复杂的报错及解决方法. 问题描述:使用FusionChar ...