题目连接:

  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. 多线程调用COM组件的体会(CoInitialize)

    调用任何COM组件之前,你必须首先初始化COM套件环境,即调用CoInitialize或CoInitializeEx.COM套件环境在线程的生存周期内有效,线程退出前需要调用CoUninitializ ...

  2. 【python】搜索引擎方面的资料

    http://blog.csdn.net/hguisu/article/category/1230933

  3. 【spring+websocket的使用】

    一.spring配置文件Java代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  4. grep命令最经常使用的功能总结

    1. grep最简单的用法,匹配一个词:grep word filename 2. 能够从多个文件里匹配:grep word filename1 filenam2 filename3 3. 能够使用正 ...

  5. web前端开发 代码规范 及注意事项

    web前端开发 代码规范 及注意事项 外部命名规范 html .js .css文件名称命名规范 my_script.js my_camel_case_name.css my_index.html 路径 ...

  6. 2016/05/16 UEditor 文本编辑器 使用教程与使用方法

    第一:百度UEditor编辑器的官方下载地址 ueditor 官方地址:http://ueditor.baidu.com/website/index.html 开发文档地址:http://uedito ...

  7. 【bzoj1071】[SCOI2007]组队

    sum= A*h+B*s排序 然后枚举height和speed的最小值 然后用两个指针:先枚举speed最小值,然后一边枚举v的最小值一边查询符合条件的人数. #include<algorith ...

  8. Koa2学习(三)GET请求

    Koa2学习(三)GET请求 GET请求是前后端交互最常用的请求之一,常常用来进行查询操作. 那么Koa是如何接收并处理GET请求呢? 创建一个服务 // 引入Koa const Koa = requ ...

  9. 有关MAC、PHY和MII

    这是一篇转载,原文链接:http://www.cppblog.com/totti1006/archive/2008/04/22/47829.html 以太网(Ethernet)是一种计算机局域网组网技 ...

  10. Ubuntu 12.04 make menuconfig 出现 Unable to find the ncurses libraries or the required header files.

    问题: *** Unable to find the ncurses libraries or the *** required header files. *** 'make menuconfig' ...