线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
#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的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- veritas.com常用资源汇总
NetBackup 8.1.2文档(合集) https://www.veritas.com/support/en_US/article.100044086 NetBackup产品组停止支持生命周期 ...
- java重定向与请求转发
重定向是不能直接访问WEB-INF下的资源的,因为重定向是浏览器二次请求,众所周知,客户端是不能直接访问WEB-INF下的资源的. 而请求转发却可以直接访问. 然而重定向却可以间接访问WEN-INF下 ...
- quartz的持久化任务调度使用应用的dataSource
Quartz提供两种基本作业存储类型--->第一种类型叫做RAMJobStore: 最佳的性能,因为内存中数据访问最快 不足之处是缺乏数据的持久性,当程序路途停止或系统崩溃时,所 ...
- Apache.Tomcat 调用Servlet原理之Class类的反射机制,用orc类解释
有一个兽人类 package com.swift.servlet; public class OrcDemo { private int hp; private int mp; private int ...
- scrapy--ipproxy
不要急于求成,你只要做的是比昨天的你更优秀一点 --匿名 今天给大家讲一下--IpProxy,由于从"http://www.xicidaili.com/nn"爬取,以下是我转载的博 ...
- dts--framework(一)
dts 大体框架 framework 定义类 定义方法 tests framework调用所需要的函数 ./dpdk/usertools/cpu_layout.py /sys/devices/syst ...
- Python学习笔记:单例模式
单例模式:一个类无论实例化多少次,返回的都是同一个实例,例如:a1=A(), a2=A(), a3=A(),a1.a2和a3其实都是同一个对象,即print(a1 is a2)和print(a2 is ...
- Flask初学者:视图函数/方法返回值(HTML模板/Response对象)
返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...
- 37-生成 JWT Token
接到上篇文章 安装扩展插件nuget package方法安装包 使用 ctrl+shift+p打开命令面板 增加这个包, Microsoft.AspNetCore.Authentication.Jw ...
- 笔记-unittest实战
笔记-unittest实战 1. 框架图 2. 用例 编写自己的测试用例类,继承于基类 class ApiTestCase(unittest.TestCase): setUp方法会 ...