题目链接

#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. DOM(一):节点层次-Node类型

    Node类型DOM1级定义了一个Node接口,该接口将由DOM中的所有节点类型实现,每个节点都有一个nodeType属性,用于表明节点的类型.节点类型由在Node类型中定义的下列12个数值常量来表示, ...

  2. 简单介绍Spring是什么?

    对于面试者回答什么是Spring,这个问题占6分分值,分值点分布:1.Spring的核心是一个轻量级(Lightweight)的容器(Container).2.Spring是实现IoC(Inversi ...

  3. ztree3.5中checkbox无法取消选中的问题解决

    使用jquery的tree插件ztree,用到复选框的功能 var setting = { check: { enable: true }, data: { simpleData: { enable: ...

  4. NPM 学习笔记整理

    NPM 学习笔记整理 阅读 550,2017年06月04日 发布,来源:blog.ihoey.com 什么是 NPM npm 之于 Node ,就像 pip 之于 Python , gem 之于 Ru ...

  5. 图的m着色

    图的m着色 #include <bits/stdc++.h> using namespace std; int n, k, m, ans; struct node{ int m, colo ...

  6. egg的使用方法

    1.controller const {ctx,service} = this: let id = ctx.query.id // 获取GET的参数 let body = ctx.request.bo ...

  7. MySQL5.6基于GTID的主从复制配置

    全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性. GTID实际上是由UUID+TID组成的.其中UUID是一个MySQL实例的唯一标 ...

  8. Angular环境配置

    1.安装node.js 访问官方网站:https://nodejs.org/en/下载node.js,直接下一步安装即可.安装完成打开cmd命令窗口输入node -v出现node版本号安装成功. 2. ...

  9. centos下LVM配置与管理

    centos下LVM配置与管理 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层, ...

  10. ethereum(以太坊)(十一)--字节数组(二)

    pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...