题目连接:

  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. 【Todo】秒杀系统 & 乐观锁 & Nginx反向代理

    http://www.csdn.net/article/2014-11-28/2822858 1. 单点帐号验证,不用读,而是用写入,Redis,看是否加watch 2. 抢宝的最终购买冲突.包装称“ ...

  2. Scala入门到精通——第十六节 泛型与注解

    本节主要内容 泛型(Generic Type)简单介绍 注解(Annotation)简单介绍 注解经常使用场景 1. 泛型(Generic Type)简单介绍 泛型用于指定方法或类能够接受随意类型參数 ...

  3. 【OpenGL】Shader实例分析(七)- 雪花飘落效果

    转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如 ...

  4. Angular2.x

    Angular版本 Angular1和Angular4分别是Angular的两个版本,也就是Angular1.x和Angular2.x(除了Angular1以外,其余都属于Angular2.x). 1 ...

  5. [LeetCode]Two Sum 【Vector全局指针的使用】

    无序数组返回两个元素和为给定值的下标. tricks:无序.返回下标增序.返回的是原始数组的下标. vector<int>*pa; bool cmp(int x,int y){ retur ...

  6. Supporting IPv6-only Networks

    Supporting IPv6-only Networks - Support - Apple Developer https://developer.apple.com/support/ipv6/ ...

  7. maven目录结构介绍篇

    bin  该目录包含了mvn运行的脚本,这些脚本用来配置java命令,准备好classpath喝相关的java系统属性 mvn是基于UNIX平台shell脚本,mvn.bat是基于Windows平台的 ...

  8. java中一个字符串是另外一个字符串的字串

    java中一个字符串是另外一个字符串的字串 String类中有一个方法 public boolean contains(Sting s)就是用来判断当前字符串是否含有参数指定的字符串例s1=“take ...

  9. html5--项目实战-仿天猫(移动端页面)

    html5--项目实战-仿天猫(移动端页面) 总结: 1.标准搜索栏的做法:这里是弹性布局,放大镜和小话筒是background img 2.手机尾部导航做法:这是一个个 li 标签,每个li标签占% ...

  10. 测试SQL基础知识

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...