题目链接

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std;
const int N = ;
typedef long long int LL;
LL sum[N << ];
LL add[N << ];
struct node
{
int l, r;
int mid()
{
return (l + r) >> ;
}
}tree[N << ];
void PushUp(int rt)
{
sum[rt] = sum[rt << ] + sum[rt << | ];
}
void PushDown(int rt, int m)
{
if (add[rt]) {
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
sum[rt << ] += add[rt] * (m - (m >> ));
sum[rt << | ] += add[rt] * (m >> );
add[rt] = ;//更新后要还原,因为递归可能会多次用到这个
}
} void build(int l,int r,int rt)
{
tree[rt].l = l;
tree[rt].r = r;
add[rt] = ;
if (l == r) {
scanf("%lld", &sum[rt]);
return;
}
int m = tree[rt].mid();
build(l, m, rt << );
build(m + , r, rt << | );
PushUp(rt);
} void updata(int c, int l, int r, int rt)
{
if (tree[rt].l == l && r == tree[rt].r) {
add[rt] += c;
sum[rt] += (LL)(r - l + )*c;
return;//这里没有进行子区间更新,用到lazy标记
}
if (tree[rt].l == tree[rt].r) return;
PushDown(rt, tree[rt].r - tree[rt].l + );
int m = tree[rt].mid();
if (r <= m) updata(c,l, r, rt << );
else if (l > m) updata(c,l, r, rt << | );
else {
updata(c, l, m, rt << );
updata(c, m + , r, rt << | );
}
PushUp(rt); } LL query(int l, int r, int rt)
{
if (l == tree[rt].l&&r == tree[rt].r) {
return sum[rt];
}
PushDown(rt, tree[rt].r - tree[rt].l + );//标记的特点,用时才进行更新
int m = tree[rt].mid();
LL res = ;
if (r <= m) res += query(l, r, rt << );
else if (l > m) res += query(l, r, rt << | );
else {
res += query(l, m, rt << );
res += query(m + , r, rt << | );
}
return res;
}
int main()
{
int n, m;
while (scanf("%d %d", &n, &m) == ) {
memset(sum, , sizeof(sum));
memset(add, , sizeof(add));
build(, n, );
while (m--) {
char ch[];
scanf("%s", ch);
if (ch[] == 'Q') {
int a, b;
scanf("%d %d", &a, &b);
printf("%lld\n", query(a, b, ));
}
else {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
updata(c, a, b, );
}
}
} return ;
}

线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  2. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  3. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  4. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  5. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  6. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  7. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  8. CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询

    链接: I - 秋实大哥与花 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

  9. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  10. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. node入口文件分析和目录初始化

    1.需要安装的模块 npm install express npm install jade npm install mongoose npm install bower -g npm install ...

  2. spring使用bean

    ApplicationContext 应用上下文,加载Spring 框架配置文件 加载classpath: new ClassPathXmlApplicationContext(“applicatio ...

  3. Java继承和访问修饰符

    继承 概念:为了提取两个类中公共代码,可以使用继承抽取重复性的代码到一个公共类中,这个公共的类称为父类(super class).继承于父类的类称为子类(sub class). 关键字     ext ...

  4. Java 截屏工具类

    PrintScreenUtils.java package javax.utils; import java.awt.AWTException; import java.awt.Dimension; ...

  5. SecureCRT 设置

  6. R语言网络爬虫学习 基于rvest包

    R语言网络爬虫学习 基于rvest包 龙君蛋君:2015年3月26日 1.背景介绍: 前几天看到有人写了一篇用R爬虫的文章,感兴趣,于是自己学习了.好吧,其实我和那篇文章R语言爬虫初尝试-基于RVES ...

  7. sql插入临时表数据的方法

    方法有两种,主要看需求. 方法1:定义好临时表的字段和类型.插入对应的值 create table #Tmp --创建临时表#Tmp ( City varchar(), -- Country varc ...

  8. SQL随手记

    数据库改名 想要达到的效果,类似于将一个文件[复制粘贴重命名]. 0.首先得断开连接,复制一份备份.然后再连接上,进行下面的操作. 1.在树形上,选中要改名的数据库,右键重命名. 2.还是它,右键,属 ...

  9. JS学习笔记--变量类型

    1.js数据类型分为基本数据类型和引用数据类型 基本数据类型:string.number.boolean.null.undefined.symbol(ES6中新增) 引用数据类型:object.arr ...

  10. postgresql 免安装版使用

    免安装版 postgresql 使用 1.首先使用 initdb 初始化数据目录 initdb --pgdata=data --encoding=UTF8 --locale=C 2.启动 postgr ...