题目链接

#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. maven项目右键快捷方式,然后点击Run As

    在某一个maven项目右键快捷方式,然后点击Run As就可以发现几个Maven的命令: 1.Maven Build: 这个命令用于编译Maven工程,执行命令后会在target文件夹中的classe ...

  2. Deep Learning Libraries by Language

    Deep Learning Libraries by Language Tweet         Python Theano is a python library for defining and ...

  3. theano提示:g++ not detected的解决办法

    导入theano包后出现如下警告: WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execu ...

  4. css隐藏元素的六类13种方法

    隐藏元素的方法 隐藏元素的方法可以总结为六类:直接隐藏.对溢出内容隐藏.对元素透明度进行调整.将元素移除当前屏幕.对元素的层级关系进行调整.对元素进行裁剪 只有对元素的透明度进行调整才可以点击,其余都 ...

  5. (排班表三)导出列名不固定的Grid表格到Excel

    将班表信息导出Excel表格保存到本地 要求:文档名称为[XXXX]年X月值班表 文档显示的效果: 实现代码: //导出Excel值班表 private void btn_export_1_Click ...

  6. 转 MYSQL 命令行大全 (简洁、明了、全面)

    MYSQL常用命令 .导出整个数据库 mysqldump -u 用户名 -p –default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1 ...

  7. Linux文件服务器实战(系统用户)

    ftp匿名用户设置完成之后任何人都可以访问服务器端文件,目录,甚至可以修改删除文件和目录,,那如何存放私密文件并保证文件或者目录专属于拥有者呢,就需要使用vsftp系统用户来实现了. 1.在linux ...

  8. 协议 - DNS

    目录 1 DNS 1.1 域名解析的历史:/etc/hosts, DNS, FQDN 1.2 域名解析流程: 域名架构 查询流程, DNS端口 1.3 合法 DNS :申请域查询授权 1.4 主机名交 ...

  9. python中正则表达式在中文字符串匹配时的坑

    之前一直有使用python 正则表达式来做中文字符串或者中英文数字混合的字符串的匹配,发现有不少情况下会匹配失灵或者结果混乱,并且在不同操作系统上匹配结果也不一致,查了很久都不知道是什么原因.今天终于 ...

  10. JS之执行上下文

    执行上下文(execution context),是JS中的一个很重要的概念.它对于我们理解函数定义,执行时都做了什么有着很大的意义.理解它我们才能明白我们常说的函数声明提升,作用域链,闭包等原理. ...