POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream>
#include <stdio.h>
#include <string.h>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R using namespace std;
const int maxn=;
int n,q;
long long num[maxn];
struct Node{
long long sum,add;
bool lazy;
}tree[maxn<<]; void pushUp(int rt){
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushDown(int rt,int m){
if(tree[rt].lazy){
tree[rt<<].add+=tree[rt].add;
tree[rt<<|].add+=tree[rt].add;
tree[rt<<].sum+=(m-m/)*tree[rt].add; //是加上父亲的add
tree[rt<<|].sum+=m/*tree[rt].add;
tree[rt<<].lazy=tree[rt<<|].lazy=true;
tree[rt].add=;
tree[rt].lazy=false;
}
}
void build(int rt,int L,int R){
if(L==R){
tree[rt].sum=num[L];
tree[rt].add=;
tree[rt].lazy=false;
return ;
}
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(rt);
} void update(int rt,int L,int R,int l,int r,long long val){
if(l<=L&&R<=r){
tree[rt].sum+=(R-L+)*val;
tree[rt].add+=val;
tree[rt].lazy=true;
return;
}
pushDown(rt,R-L+);
int mid=(L+R)>>;
if(l<=mid)
update(lson,l,r,val);
if(r>mid)
update(rson,l,r,val);
pushUp(rt);
} long long query(int rt,int L,int R,int l,int r){
long long ret=;
if(l<=L&&R<=r){
return tree[rt].sum;
}
pushDown(rt,R-L+); //额,好吧,忘记这里也要初始化了
int mid=(L+R)>>;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
}
int main()
{
int a,b;
long long c; //c用int也能AC,用long long是防止当(R-L+1)*c的时候超出int范围
char str[];
long long ans;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%I64d",&num[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%s",str);
if(str[]=='C'){
scanf("%d%d%I64d",&a,&b,&c);
update(,,n,a,b,c);
}
else{
scanf("%d%d",&a,&b);
ans=query(,,n,a,b);
printf("%I64d\n",ans);
} }
return ;
}
POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)的更多相关文章
- 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(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- 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 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [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 (线段树区间更新求和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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- C++ 11 之初始化
1.4中不同初始化的形式 a.string s("zhl").int i(3); //括号初始化 b.string s="zhl".int ...
- 《boot分区监控的小脚本》
#!/bin/bash TEST=`df | grep "boot" |awk '{print $5}' |cut -f1 -d"%"` if [ $TEST ...
- matlab之图像处理(2)
diagram = imread('lena1.png') diagram = rgb2gray(diagram);%------------------------------将图片转换为灰度图 N ...
- c# 取得ip地址和网关
/// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...
- sql server 中 bigint 和 datetime 性能比较
-- 创建表 create table Test_tbl ( ID varchar(40) primary key nonclustered, IntCol int, DateCol datetime ...
- kdbchk: the amount of space used is not equal to block size
一.对数据文件检查 注意:应该在关闭数据库模式下进行bbed的操作 [oracle@ora10 controlfile]$ dbv file=/u01/app/oracle/oradata/ORCL/ ...
- 3. opencv进行SIFT特征提取
opencv中sift特征提取的步骤 使用SiftFeatureDetector的detect方法检测特征存入一个向量里,并使用drawKeypoints在图中标识出来 SiftDescriptorE ...
- Allegro中板子边框不封闭导致的z-copy无法用的问题
画一个不规则的边框,有半圆形状,导致边框不封闭,无法使用Z-COPY命令,下边是解决办法: 1 画好Outline后,选择 shape -> Compose Shape , options选项卡 ...
- linux c 实现大数相乘
#include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...
- apache配置VirtualHost(windows)
以下方式适合原生 Apache, XAMPP 和WAMP 套件. 1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件. 2. 仿照例 ...