题目地址:POJ 3468

打了个篮球回来果然神经有点冲动。

无脑的狂交了8次WA。。竟然是更新的时候把r-l写成了l-r。。。

这题就是区间更新裸题。

区间更新就是加一个lazy标记,延迟标记,仅仅有向下查询的时候才将lazy标记向下更新。其它的均按线段树的来即可。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <stack>
using namespace std;
#define LL long long
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int MAXN=1e5+10;
LL sum[MAXN<<2];
LL lazy[MAXN<<2];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt, int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l, int r, int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int ll, int rr, int x, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
lazy[rt]+=x;
sum[rt]+=(LL)x*(r-l+1);
return ;
}
PushDown(rt, r-l+1);
int mid=(l+r)>>1;
if(ll<=mid) update(ll,rr,x,lson);
if(rr>mid) update(ll,rr,x,rson);
PushUp(rt);
}
LL query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt,r-l+1);
LL ans=0;
int mid=(l+r)>>1;
if(ll<=mid) ans+=query(ll,rr,lson);
if(rr>mid) ans+=query(ll,rr,rson);
return ans;
}
int main()
{
int n, q, i, a, b, c;
char ch[3];
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
getchar();
scanf("%s",&ch);
if(ch[0]=='Q')
{
scanf("%d%d",&a,&b);
LL ans=query(a,b,1,n,1);
printf("%lld\n",ans);
}
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(线段树区间更新,模板题,求区间和)

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

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

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

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

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

随机推荐

  1. 华硕(ASUS)X554LP笔记本在64位win7下无线网络连接问题

    还是那台华硕(ASUS)X554LP笔记本,无线网卡为 Qualcomm Atheros AR956x,某天换了个环境,WIFI(此处简称为WIFI网A)就连不上网了.手机.其它笔记本用无线连接都没问 ...

  2. Java二分法查找

    二分法查找 /** * 二分法查找 找不到返回-1 * @author yangzi * */ public class TwoFind { public static int twoFind(int ...

  3. Codeforces_761_E_(dfs)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Java线程的sleep方法

    sleep方法的签名: public static void sleep (long millis) sleep方法是Thread类的一个方法,作用是:在指定的毫秒内让正在执行的线程休眠(暂停执行) ...

  5. MySql学习笔记(二) —— 正则表达式的使用

    前面介绍利用一些关键字搭配相应的SQL语句进行数据库查找过滤,但随着过滤条件的复杂性的增加,where 子句本身的复杂性也会增加.这时我们就可以利用正则表达式来进行匹配查找. 1.基本字符匹配 ' o ...

  6. Linux(Centos7) 设置静态IP

    关于虚拟机 这里使用Centos7为例,因为linux是安装在在虚拟机中,这里先看一下虚拟机的网络适配器: 这里我使用的NAT模式,接着配置虚拟机的虚拟网络: 这里主要看一下VMnet8的设置: 这里 ...

  7. 面向对象程序设计--Java语言第一周编程题:分数

    分数 题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这 ...

  8. java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS

    解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...

  9. Error LNK2019: unresolved external symbol C++模板类声明与定义链接错误问题

    编译器在编译模板时,并不会生成代码,只有遇到实例化的时候才会生成代码.因此,当我们只引用模板声明文件的时候,在实例化的对象时候,模板的定义问文件是不可见的,于是出现链接错误.例如: //A.h #pr ...

  10. Mybatis-generator插件

    Mybatis-generator插件 1.下载地址 https://github.com/mybatis/generator/releases mybatis-generator是一款在使用myba ...