题意:你有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. JavaScript设计模式与开发实践——读书笔记1.高阶函数(下)

    上部分主要介绍高阶函数的常见形式,本部分将着重介绍高阶函数的高级应用. 1.currying currying指的是函数柯里化,又称部分求值.一个currying的函数会先接受一些参数,但不立即求值, ...

  2. mvc 从客户端 中检测到有潜在危险的 Request.Form 值

    天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&qu ...

  3. 把json格式的字符串转换成javascript对象或数组

      第一种 JSON.parse(jsonString) 第二种 eval("("+jsonString+")") 第三种 var obj=(function ...

  4. IOS发送Email的两种方法

    IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. ...

  5. Restrict form resize -- Delphi

    http://www.delphipages.com/forum/showthread.php?t=58391 Hi, How would I restrict a form from being r ...

  6. TPS61040/61041 开关电源稳压器(DC-DC) ADJUST

    Variable Control Voltage Output Voltage Adjust This method is accomplished by connecting a variable ...

  7. (转)SQL Server创建索引

    什么是索引拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音.笔 ...

  8. jdbc分页

    分页是一个被讲到烂掉的话题,今天我再拾起来踹几脚吧 (Hibernate的分页做得很好很强大,用的人都知道 ,这个就不用再说了) 1.为什么要分页?    首先是数据量太大会影响查询和传输的性能,关键 ...

  9. (转)找回vss超级管理员密码

    原文:http://www.cnblogs.com/446557021/archive/2011/01/05/1926213.html 如果忘记了VSS管理员密码,打开vss数据库所在的文件夹,打开d ...

  10. Struts2数据验证机制

    1. 手动验证的实现 只需要在继承ActionSupport类的情况下,直接重写validate()方法即可.使用validate()方法可以对用户请求的多个Action方法进行验证,但其验证的逻辑是 ...