线段树专题 POJ3468 A Simple Problem with Integers
题意:n个点。m个操作。两种操作类型。C X Y K 表示区间[x,y]上每一个点值加k。Q X Y 求区间[x,y]的和
分析:线段树区间求和,裸模板
注意:结果会超int,要用long long 表示,假设是在hust上交结果要用%I64d。poj的话则用%lld
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include<vector>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 1e5+5;
long long sum[maxn*4];
long long lazy[maxn*4];
int num[maxn];
void up(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void down(int l,int r,int rt){
if(!lazy[rt]) return;
int mid=(l+r)/2;
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(mid-l+1);
sum[rt<<1|1]+=lazy[rt]*(r-mid);
lazy[rt]=0;
}
void build(int l,int r,int rt){
lazy[rt]=sum[rt]=0;
if(l==r){
sum[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
up(rt);
}
void update(int L,int R,int k,int l,int r,int rt){
if(L<=l&&r<=R){
lazy[rt]+=k;
sum[rt]+=(r-l+1)*k;
return;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,k,l,mid,rt<<1);
if(R>mid) update(L,R,k,mid+1,r,rt<<1|1);
up(rt);
}
long long query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return sum[rt];
down(l,r,rt);
int mid=(l+r)>>1;
long long ret = 0;
if(L<=mid) ret+=query(L,R,l,mid,rt<<1);
if(R>mid) ret+=query(L,R,mid+1,r,rt<<1|1);
up(rt);
return ret;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
build(1,n,1);
while(q--){
char c;
int x,y,k;
scanf(" %c",&c);
if(c=='C'){
scanf("%d%d%d",&x,&y,&k);
update(x,y,k,1,n,1);
}
else{
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1,n,1));
}
}
}
}
线段树专题 POJ3468 A Simple Problem with Integers的更多相关文章
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
随机推荐
- 扩展Snackbar 使其支持居中显示
https://github.com/nispok/snackbar 默认Snackbar支持底部或者顶部显示,不支持居中显示 查看Snackbar.java的源码可以看到createMarginLa ...
- 请将你的App签名文件放进保险箱
这是一篇以我自己的实际经历写的一篇文章. 当下移动开发正值火爆,由于门槛低,任何一个程序员都可以比较容易的进入移动开发领域,作为App或者游戏等移动开发者,当你开发完成一个作品后,准备上线时,需要对你 ...
- android studio java.io.IOException:setDataSourse fail.
这一次是针对Android开发中的一个小问题,权限获取的问题. 在写了一个一个小Android程序的时候,有时候普需要获取本机的文件(Audio&Video),这时候如果不加权限就会出现这种情 ...
- SpringBoot+Mybatis 自动创建数据表(适用mysql)
Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...
- sqlalchemy.exc.InvalidRequestError: Entity '<class 'model.TestCase'>' has no property 'project'
原因: 修改表结构,但没有更新数据模型造成的 解决办法: 在sqlalchemy提供的表模型中增加project字段的描述信息 这次修改测试框架我有点想不起来,在测试代码中,是怎么通过sqlalche ...
- MxCAD5.2 20181022更新
下载地址: http://www.mxdraw.com/ndetail_10108.html 1. 开放VIP功能,无需购买即可使用 2. 修正一些图纸打开和保存出错的问题 3. 修改填充命令,对某些 ...
- 在添加新内容时,creatat没有数据
首先找到controller中的add方法,然后是执行了Service.insert()方法 然后找到service对应的impl方法, impl方法中的 public void insert(Cus ...
- Error:Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope
原因:html标签和struts标签混用,或者表单元素外面没有<html:form>标签包裹. 解决:统一标签 <html:form action="/login.do&q ...
- linux tail-在屏幕上显示指定文件的末尾若干行
博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 tail命令用于输入文件中的尾部内容.tail命令默认在屏幕上显示指定文件的末尾10行.如果给定的文件不止一个,则在显示的每个 ...
- DAS、NAS、SAN、iSCSI 存储方案概述
(转自https://blog.csdn.net/tianlesoftware/article/details/6201346) 目前服务器所使用的专业存储方案有DAS.NAS.SAN.iSCSI几种 ...