暑期训练狂刷系列——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 ...
随机推荐
- MongoDB使用入门
1.MongoDB的安装 步骤一:下载MongoDB 下载安装包:http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz 步骤二:设置 ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
- 谁是性能杀手?Kafka多Topic下启用SSL时延增大问题分析
问题背景 项目中将Kafka接口进行RESTful封装,在使用RESTful接口进行性能测试时,发现Topic数增多后,开启SSL与非SSL进行测试,发现开启SSL后性能下降得厉害.例如600个Top ...
- c中常用的关键字static const volatile
在C语言中,关键字static有三个明显的作用:1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.2). 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数 ...
- 2016/2/25 html+css学习资源
html+css学习资源 1.Position is Everything,一个描述和展示在各种浏览器中发现的bug,并提供css解决方法的网站,顶! 2.一个国外的网页设计论坛 3.http://c ...
- Google Gson使用简介
1.Google Gson在android studio的使用 gradle:compile 'com.google.code.gson:gson:2.2.4' 2.Gson 注解 @Expose 注 ...
- Delphi的goto语法
今天第一次主要到Delphi也有goto语法,特别是其奇怪的label声明.估计主要是用来跳出多重循环,而且还真有人使用这种方式.记个笔记: procedure TForm1.btn3Click(Se ...
- Swift入门(十)——循环引用、弱引用和无主引用
近期看到swift里面不仅有循环引用和弱引用(weak),还加入了无主引用(unowned),于是写了一些demo,这里总结一下. 和OC一样.Swfit默认也是基于ARC进行内存管理的,因此尽管简单 ...
- java语法基础(三)
类和对象 面向对象语言概述 java是一种面向对象的语言,什么是面向对象的语言? 要搞清楚什么是面向对象语言,我们需要相对的了解一下面向过程的语言. java入门阶段,我们又给大家说过一些语言的分类, ...
- html5--项目实战-仿360囧图
html5--项目实战-仿360囧图 实例: 代码 <!doctype html> <html> <head> <meta charset="utf ...