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 ...
随机推荐
- map的key 为指针
STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...
- Jenkins结合testng注意事项
1.在生成测试报告时,因为Jenkins自带的只有Junit的测试报告,不会显示testng的. 2.要想显示Publish TestNG Results这一项,首先需要在jenkins的首页-系统管 ...
- js评分
js评分 原理:给ele挂载一个自定义属性保存选中的星星数,鼠标经过时,显示所在星数的评价内容,以及给他星星亮起来,鼠标移开时显示的星星数时选择的星星数,没选的话是默认星星数,点击时,将选中的星 ...
- Eclipse集成Tomcat插件(特别简单)
. 只需要一个jar包 复制到eclipse/plugins文件夹下,重启Eclipse即可看到如下三只小猫 1.修改Tomcat (1)Tomcat version:版本 (2)Tomcat Hom ...
- JDBC连接数据库:单线程、多线程、批处理插入数据的对比
一.单线程(单条循环)插入50000条记录: 每执行一次就要访问一次数据库 import java.sql.Connection; import java.sql.DriverManager; imp ...
- Linux3.10.0块IO子系统流程(2)-- 构造、排序、合并请求
Linux块设备可以分为三类.分别针对顺序访问物理设备.随机访问物理设备和逻辑设备(即“栈式设备”) 类型 make_request_fn request_fn 备注 SCSI 设备等 从bio构 ...
- node 慕课笔记
global global.testVar2 = 200; 在别的文件中可以任意调用到 因为global是全局变量相当于js的window一样的
- mybatis 配置文件 配置别名
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC &q ...
- oracle 正则表达式 匹配
oracle 正则表达式 在实际应用中,想排除带有中文的字段值: select h.froomnumber from t_broker_house h where REGEXP_LIKE(froomn ...
- IIS应用程序池自动回收问题的解决办法
windows 2012 的w3wp.exe(IIS Worker Process)进程不及时释放导致占用内存过高,以下解决方法: IIS可以设置定时自动回收,默认回收是1740分钟,也就是29小时. ...