题意:你有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 【线段树,区间更新】的更多相关文章

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

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

  2. (简单) 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 ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

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

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  8. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. HDU 5295 Unstable 计算几何

    Unstable 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5295 Description Rasen had lost in labyrint ...

  2. Shell基础学习(六) 流程控制

    1.if if的语法格式 if conditon then command1 command2 ``` commandn fi 2.if else if conditon then command1 ...

  3. PHP 基础函数(三)数组和变量之间的转换

    extract($arr);用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值注:(第二个参数很重要,可以看手册使用)使用方法 echo $a;compact(var1,var ...

  4. Centos 6/ 7下通过yum安装php7环境

    本文转自:云溪社区 2015年12月初PHP7正式版发布,迎来自2004年以来最大的版本更新.PHP7最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM.在WordPr ...

  5. mysql中如何比较日期

    做项目,需求是要做一个统计的功能,首次进入默认显示今天以及七天前的数据,这个很好解决. 然后就是用户点击日历插件选择日志,根据日期来统计当天的情况,我数据库里存的时间是使用的时间戳 前台获取到的日期是 ...

  6. stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用

    http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h头文件的作用 Standard Application Fram Exte ...

  7. rc_80 tomcat 日志

    1 #!/bin/sh 2 cd /mnt/tomcat/tomcat_8082/logs; 3 tail -f catalina.out;

  8. OpenShift 如何获取bearer Token以便进行各种API调用

    Openshift 需要通过bearer token的方式和API进行调用,比如基于Postman就可以了解到,输入bearer token后 1.如何获取Bearer Token 但Bearer T ...

  9. C语言不定参数

    最近,遇到一个c语言的不定参数问题.其实,对于c语言的不定参数问题,只需要三个函数就可以搞定了.这三个函数的头文件是<stdarg.h>,其实下面的三个函数都是一个宏定义(macro).  ...

  10. SPSS Clementine 数据挖掘入门1

    SPSS Clementine是Spss公司收购ISL获得的数据挖掘工具.在Gartner的客户数据挖掘工具评估中,仅有两家厂商被列为领导者:SAS和SPSS.SAS获得了最高ability to e ...