题目连接:

  http://poj.org/problem?id=3468

题目大意:

  给出n个数,有两种操作:

    1:"C a b c",[a,b]中的每一个数都加上c。

    2:"Q a b",求[a,b]中每个数相加的和。

解题思路:

  线段树更新到每一个节点的话,由于节点数目和查询次数原因会tle,所以在每一个节点内定义一个标志变量表示当前节点的下一层为更新,每次查询时候有需要的话在更新到下一层。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
#define LL __int64
struct node
{
LL L, R;
LL sum, add;
LL Mid()
{
return (L + R) / ;
}
};
node tree[maxn];
LL res; void build(LL root, LL l, LL r)
{
tree[root].L = l;
tree[root].R = r;
tree[root].sum = tree[root].add = ; if (l == r)
return ;
build (*root+, l, tree[root].Mid());
build (*root+, tree[root].Mid()+, r);
}
void insert (LL root, LL s, LL e, LL x)
{
tree[root].sum += x * (e - s + );
if (tree[root].L == s && e == tree[root].R)//更新到区间
{
tree[root].add += x;
return ;
}
if (e <= tree[root].Mid())
insert (*root+, s, e, x);
else if (tree[root].Mid() < s)
insert (*root+, s, e, x);
else
{
insert (*root+, s, tree[root].Mid(), x);
insert (*root+, tree[root].Mid()+, e, x);
}
}
void query (LL root, LL s, LL e)
{ if (tree[root].L == s && e == tree[root].R)
{
res += tree[root].sum;
return ;
}
if (tree[root].add)
{//向下继续更新
tree[*root+].add += tree[root].add;
tree[*root+].add += tree[root].add;
tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
tree[root].add = ;
}
if (e <= tree[root].Mid())
query (*root+, s, e);
else if (tree[root].Mid() < s)
query (*root+, s, e);
else
{
query (*root+, s, tree[root].Mid());
query (*root+, tree[root].Mid()+, e);
}
}
int main ()
{
LL n, m, num;
while (scanf ("%I64d %I64d", &n, &m) != EOF)
{
build (, , n);
for (int i=; i<=n; i++)
{
scanf ("%I64d", &num);
insert (, i, i, num);
}
char str[];
LL s, e;
while (m --)
{
scanf ("%s %I64d %I64d", str, &s, &e);
if (str[] == 'Q')
{
res = ;
query (, s, e);
printf ("%I64d\n", res);
}
else
{
scanf ("%I64d", &num);
insert (, s, e, num);
}
}
}
return ;
}

暑期训练狂刷系列——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 [线段树区间更新求和]

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

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

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

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

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

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

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

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

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

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

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

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

  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. 解决WIN7下VMWARE虚拟机无法上网问题

    一.Win7 虚拟机centos NAT联网 链接地址:http://www.cr173.com/html/19808_1.html,也不知道是哪位大神弄的,实践过,可以使用,但是重启之后却不能用了, ...

  2. IOS开发 APP提交程序上传流程

    由于苹果的机制,在非越狱机器上安装应用必须通过官方的App Store,开发者开发好应用后上传App Store,也需要通过审核等环节.AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传A ...

  3. Centos 6.x 安装Nagios及WEB管理nagiosql实现windows及linux监控指南

    一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...

  4. HTML的DIV如何实现垂直居中

    外部的DIV必须有如下代码 display:table-cell; vertical-align:middle;   这样可以保证里面的东西,无论是DIV还是文本都可以垂直居中

  5. Web—CSS概述

    一.概念: 它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言 的一个子集)等文件样式的计算机语言. 二.特点: 1.实现网页内容与样式的分离     2.降低图形文件的 ...

  6. HBuilder开发App教程05-滴石和websql

    滴石 介绍 滴石是用HBuilder开发的一款计划类app. 用到HBuilder,mui.nativejs以及h5一些特性. 预期 眼下仅仅开发到todolist级别, 以后计划做成日计划,月计划, ...

  7. JQuery实现表格行的上移、下移、删除、增加

    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%&g ...

  8. Spring Task 定时任务

    所谓定时任务.就是依据我们设定的时间定时运行任务,就像定时发邮件一样,设定时间到了.邮件就会自己主动发送. 在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task.同 ...

  9. Android ListView分页,动态添加数据

    1.ListView分页的实现,重点在于实现OnScrollListener接口,判断滑动到最后一项时,是否还有数据可以加载, 我们可以利用listView.addFootView(View v)方法 ...

  10. qemu所支持的网卡

    1 命令 -net nic 创建一个network interface card,即创建一个网卡,默认是e1000网卡. 2 qemu所支持的网卡类型 2.1 rtl8139 Realtek 10/1 ...