POJ 3468 A Simple Problem with Integers 【线段树,区间更新】
题意:你有N个整数,A1,A2,…,一个。你须要处理两种类型的操作。一种类型的操作是加入了一些给定的数字,每一个数字在一个给定的时间间隔。
还有一种是在给定的时间间隔要求数量的总和。
难点:主要是lazy标记,不好弄懂, 事实上lazy标记就是当前改变的值不所有更新。等到用的时候再更新,这样就节省了好多时间。
题目链接: id=3468">http://poj.org/problem?id=3468
代码:
#include<stdio.h>
#include<string.h>
#define MAXN 100005
#define LC l, m, rt<<1
#define RC m+1, r, rt<<1|1
#define LL __int64
LL sum[MAXN<<2], add[MAXN<<2];//add数组就是临时储存要添加的
void pushup(int rt)
{
sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt, int m)//这个是难点。理解了这个这道题就差点儿相同了
{
if(add[rt]){
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += add[rt]*(m-(m>>1));
sum[rt<<1|1] += add[rt]*(m>>1);
add[rt] = 0;
}
}
void creat(int l,int r,int rt) {
add[rt] = 0;
if (l == r) {
scanf("%I64d",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
creat(LC);
creat(RC);
pushup(rt);
}
void update(int le, int ri, int num, int l, int r, int rt)
{
if(le <= l&&r<=ri){
add[rt] += num;
sum[rt] += (LL)num*(r-l+1);
return;
}
pushdown(rt, r-l+1);
int m = (l+r)>>1;
if(le <=m) update(le, ri, num, LC);
if(ri > m) update(le, ri, num, RC);
pushup(rt);
}
LL query(int le, int ri, int l, int r, int rt)
{
if(le <= l&&r<=ri){
return sum[rt];
}
pushdown(rt, r-l+1);
LL res = 0;
LL m = (l+r)>>1;
if(le <= m) res += query(le, ri, LC);
if(ri > m) res += query(le, ri, RC);
return res;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
creat(1, n, 1);
char s[2];
int a, b, c;
while(m --){
scanf("%s", s);
if(s[0] == 'Q'){
scanf("%d%d", &a, &b);
printf("%I64d\n", query(a, b, 1, n, 1));
}
else{
scanf("%d%d%d", &a, &b, &c);
update(a, b, c, 1, n, 1);
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers 【线段树,区间更新】的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #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 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- ExtJs ComboBox 在IE 下 自动完成功能无效的解决方案
使用 ComboBox 来作为自动完成的组件,就像google suggestion ,可是在IE下怎么也无法输入字符,是处于不可编辑状态,而firefox和chrome都正常显示.我在2个ExtJs ...
- The useful App Paths registry key : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths how to load BPLs without writ ...
- X11 FRAMEBUFFER QT
之前对X11 FRAMEBUFFER理解的不够,现在总结一下Qt Embedded是挪威Trolletch公司的图形化界面开发工具Qt的嵌入式版本,它通过QtAPI与LinuxI/O以及Framebu ...
- django的动态url,url里含有参数,含有参数的url
#!/usr/bin/env python # coding:utf- from django.conf.urls import url,include from django.contrib imp ...
- 关于一道面试题,使用C#实现字符串反转算法
关于一道面试题,使用C#实现字符串反转算法. 题目见http://student.csdn.net/space.php?do=question&ac=detail&qid=490 详细 ...
- go语言基础之基匿名函数本语法和闭包
一.匿名函数 示例1: package main import "fmt" func main() { a := 10 str := "mike" //匿名函数 ...
- JavaScript实现瀑布流
前端内容: 使用JavaScript和四个div,将照片放入四个div中 <!DOCTYPE html> <html lang="en"> <head ...
- SQL Server 2008新特性——策略管理
策略管理是SQL Server 2008中的一个新特性,用于管理数据库实例.数据库以及数据库对象的各种属性.策略管理在SSMS的对象资源管理器数据库实例下的“管理”节点下,如图: 从图中可以看到,策略 ...
- 设置 IE 默认模式为 IE8
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
- JVM垃圾回收(GC)整理总结学习
基本回收算法 1. 引用计数(Reference Counting)比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数.垃圾回收时,只用收集计数为0的对象.此算法最 ...