POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 141093 | Accepted: 43762 | |
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
The sums may exceed the range of 32-bit integers.
题意
线段树区间更新求和模板题。
给出n个数和m次操作,Q表示查询该区间内的数的和并输出,C表示把该区间内的所有数都加上一个值。
AC代码
抄的师傅的板子,还不是太理解
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=2e5+10;
const int mod=1e9+7;
using namespace std;
struct wzy
{
ll left,right,len;
ll value;
ll lazy;
}p[maxn<<2];
void push_up(ll o)
{
p[o].value=p[lson].value+p[rson].value;
}
void push_down(ll o)
{
if(p[o].lazy)
{
p[lson].lazy+=p[o].lazy;
p[rson].lazy+=p[o].lazy;
p[lson].value+=p[o].lazy*p[lson].len;
p[rson].value+=p[o].lazy*p[rson].len;
p[o].lazy=0;
}
}
void build(ll o,ll l,ll r)
{
p[o].left=l;p[o].right=r;
p[o].len=r-l+1;
p[o].lazy=0;
if(l==r)
{
ll x;
scanf("%lld",&x);
p[o].value=x;
return ;
}
ll mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
push_up(o);
}
void update(ll o,ll l,ll r,ll v)
{
if(p[o].left>=l&&p[o].right<=r)
{
p[o].lazy+=v;
p[o].value+=v*p[o].len;
return ;
}
push_down(o);
ll mid=(p[o].left+p[o].right)>>1;
if(r<=mid)
update(lson,l,r,v);
else if(l>mid)
update(rson,l,r,v);
else
{
update(lson,l,mid,v);
update(rson,mid+1,r,v);
}
push_up(o);
}
ll query(ll o,ll l,ll r)
{
if(p[o].left>=l&&p[o].right<=r)
return p[o].value;
ll mid=(p[o].left+p[o].right)>>1;
push_down(o);
if(r<=mid)
return query(lson,l,r);
else if(l>mid)
return query(rson,l,r);
else
return query(lson,l,mid)+query(rson,mid+1,r);
}
int main(int argc, char const *argv[])
{
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
char ch[5];
ll a,b,c;
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",query(1,a,b));
}
else
{
scanf("%lld%lld%lld",&a,&b,&c);
update(1,a,b,c);
}
}
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 线段树区间更新
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 (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- 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 ...
随机推荐
- CentOS下安装mysql5.7和mysql8.x
5.7和8.15版本亲测.centos版本为:CentOS-7-x86_64-Minimal-1810. 1.下载mysql57-community-release-el7-9.noarch.rpm. ...
- idea中deBug方法
1 2设置controller层断点鼠标左键点击,断点在哪里,就会deBug到哪里 3刷新页面 4查看 5service层设置断点 6 7查看返回信息是否错误
- 在shell脚本里执行sudo 命令
可以 : echo "yourpasswd" |sudo -S yourcommand 但是不安全,因为密码都显示在shell脚本里面了-_- 引自http://hi.baid ...
- C++11 并发之std::thread std::mutex
https://www.cnblogs.com/whlook/p/6573659.html (https://www.cnblogs.com/lidabo/p/7852033.html) C++:线程 ...
- Ie11 的改变
摘录地址: http://www.4fang.net/content.jsp?id=30537 微软在上周刚刚发布了用于Windows 8.1上的首个Internet Explorer 11的 ...
- am335x system upgrade uboot ethernet(二)
系统可以通过SD卡引道之后,为了之后了调试方便 通过查看网卡的硬件设计 正常来说需要注意的有如下几点: 1) 网口 的接线方式: RMII 2) 网口的PHY地址两张网口,这里我们只需先初始化一张网卡 ...
- unity中自制模拟第一人称视角
public float sensitivityX = 5f; public float sensitivityY = 5f; public float sensitivetyKeyBoard = 0 ...
- 用老毛桃U盘安装:[3]Ghost版Win7系统
用老毛桃自动安装Ghost版Win7的步骤: 1,到网上先下载Ghost版Win7映像文件到硬盘,我放到的是U盘,盘符为Z,如果你愿意,可直接放到硬盘即可,放到硬盘安装速度会快一点. 2,把制作好的老 ...
- 谷歌浏览器慎用有道词典插件(<audio></audio>) (转载)
谷歌浏览器慎用有道词典插件(<audio></audio>) 原文 :http://blog.csdn.net/u010556394/article/details/7112 ...
- mabatis学习(四)----解决字段名与实体类属性名不同的冲突
在项目开发中,数据库中的字段名不一定和实体类的类名完全相同(当然大小写忽略),那么就可以在sql映射文件中解决此问题 一.创建需要的数据库和表 编写sql脚本,在navicat for mysql中执 ...