暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)
题目连接:
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 (线段树+区间更新)的更多相关文章
- 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 [线段树区间更新求和]
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(线段树区间更新)
题目地址: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 , 线段树+区间更新。
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 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- 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 ...
随机推荐
- eclipse bug之'<>'operator is not allowed for source level below 1.7
eclipse中导入工程,报这个错'<>'operator is not allowed for source level below 1.7,把jdk改成1.7后,提示Android r ...
- linux 下的特殊文件 /dev/null 和 /de/zero
生成一个100Mb的文件 : time dd of=2Gb.file if=/dev/zero bs=1024 count=100000 ubuntu 下测试磁盘的读写性能: 测试写: time d ...
- 【转载】TCP,IP,HTTP,SOCKET区别和联系
网络由下往上分为: 对应 物理层-- 数据链路层-- 网络层-- IP协议 传输层-- TCP协议 ...
- (WPF)附加属性
<Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...
- 解决CentOS java环境不生效的问题
查看当前java版本 [root@localhost jdk1.6.0_45]# java -version openjdk version "1.8.0_65" OpenJDK ...
- Oracle11g表空间导入dmp数据
如果你的表数据没有附带表空间和用户名,那么只要一句话 Imp {u_name}/{u_pwd}@{local_svrname} fromuser={from_user} touser={u_name} ...
- 基于开源项目的在线网络视频直播项目---一个很好的电视直播开源项目Sopcast
http://blog.csdn.net/roy_xu/article/details/2216559 http://115.com/?ct=rar&pickcode=ew52634xr2cr ...
- struts2多图片上传实例【转】
原文地址:http://blog.csdn.net/java_cxrs/article/details/6004144 描述: 通过struts2实现多图片上传. 我使用的版本是2.2.1,使用的包有 ...
- getHibernateTemplate()(Spring中常用的hql查询方法)
Spring中常用的hql查询方法(getHibernateTemplate()) --------------------------------- 一.find(String queryStrin ...
- 单点登录原理及实现sso
WEB的登录那些事 说道账户登录和注册,其实我们每天都在亲身感受着,像微博.知乎还有简书等等.我们总是需要定期的去重新登录一下,对于这种认证机制,我们都能说出来两个名词,Cookie.Session. ...