poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 63565 | Accepted: 19546 | |
Case Time Limit: 2000MS |
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
给出n个数q次操作
C代表把a到b间的数分别加c
Q要求输出和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=111111;
long long sum[MAXN<<2];
long long lazy[MAXN<<2];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(l>=L&R>=r)
{
lazy[rt]+=c;
sum[rt]+=(long long)c*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(L,R,c,l,mid,rt<<1);
if(R>mid)
update(L,R,c,mid+1,r,rt<<1|1);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(l>=L&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
long long cnt=0;
if(L<=mid)
cnt=query(L,R,l,mid,rt<<1);
if(R>mid)
cnt+=query(L,R,mid+1,r,rt<<1|1);
return cnt;
}
int main()
{
int n,q;
char op[2];
int a,b,c;
while(scanf("%d %d",&n,&q)!=EOF)
{
build(1,n,1);
//printf("%d\n",sum[1]);
while(q--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d %d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else
{
scanf("%d %d %d",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
}
return 0;
}
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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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(线段树,区间更新,区间求和)
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 ...
随机推荐
- 《简明python教程》笔记三
图形软件(GUI工具) 可供选择的GUI: 一.PyQT 是Qt工具包的python绑定.Qt工具包是构建KDE的基石.linux下使用免费,windows下使用收费. 二.PyGTK 是GTK+工具 ...
- 移植WordPress到Ubuntu16.04
移植WordPress到Ubuntu16.04 新建 模板 小书匠 移植WordPress到Ubuntu16.04 搭建好LAMP环境后,可以按照以下方法,将本地站点移植到服务器上. 以WordPre ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(4)-素材管理
游戏中总是有大量的图像资源,我们通常的做法是把要用的图片做成图片集,这样做的好处就不多说了.直接来看怎么用. 这里我们使用自己的类Assets来管理它们,让这个工具类作为我们的资源管家,从而可以在任何 ...
- Asp.net中web.config配置文件详解
Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...
- Cocos2dx-Lua UIScrollView 和 UITableView 对比
为什么写这个 上面这个问题的答案也是我写这篇文章的初衷,在最近给游戏添加一些列表的时候,对比着应用了一下他们两个,在它们两个之间的优劣势之间进行取舍,就有了这个问题的答案. 按照我一个iOS开发而言, ...
- 使用IIFE(立即执行函数)让变量私有化
今天去看了一个GITHUB上的开源项目,在客户端JS的脚本编写的时候,代码中多次使用了IIFE. 一开始我是懵逼的,不知道这种函数的意义何在,小菜鸟嘛. 后面我去研究了一番.发现了它的主要作用就是:让 ...
- SSH框架的简单含义
典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...
- 【spfa】bzoj1295 [SCOI2009]最长距离
题意:给你一个n*m的点阵.有些点是障碍,求一个欧几里得距离最大的点对(A,B),使得在移走的障碍≤T的情况下,可以从A走到B. 建图,跑n*m次spfa,求出从 每个点 出发到 其他所有点 的 经过 ...
- 【三维偏序】【分块】bzoj3262 陌上花开
裸的三维偏序. 对x坐标排序,y.z坐标分块.复杂度O(n*sqrt(n*log(n))).代码很短. #include<cstdio> #include<cmath> #in ...
- jdk8中Spliterator的作用
文章前半部分转自:https://blog.csdn.net/lh513828570/article/details/56673804 之前的时候看集合部分源码没看完,今天又翻了一下,看到了个东西sp ...