POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468
题目大意很简单,有两个操作,一个
Q a, b 查询区间[a, b]的和
C a, b, c让区间[a, b] 的每一个数+c
第一次线段树的延时标记,花了好大的功夫才写好==!
很容易看出来使用使用线段树记录区间的和,但是难点在于每次修改的是一个区间而不是一个点
所以采用的方法就是每次做修改操作时,只将区间[a,b]的标记+c,而不是真正意义上的将区间[a, b] 的每一个值+c。
而当我们做查询操作时,就只需要将区间[a, b]在从[1, N]开始查找到查找到时所经过的区间的标记往下传递就可以了(同时记得更新当前节点的值)

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct Node { LL sum, add; } tree[MAXN<<];
int a, b, N, Q;
LL c; void updataChild(int k, int L, int R)//将编号为k的标记传到他的子节点去
{
tree[k].sum += (R-L+) * tree[k].add;//先更新它自己的sum值
tree[k<<].add += tree[k].add;//将左右子节点的add值更新
tree[k<<|].add += tree[k].add;
tree[k].add = ;//去掉标记
} void update(int k, int L, int R)//更新区间的值
{
if(R<a || b<L) return ;//不再区间内 if(a<=L && R<=b) { tree[k].add += c; return; }//实际上只更新这个区间的add值 int mid = (L+R)>>; update(lson); update(rson); //往左和往右更新 tree[k].sum = tree[k<<].sum + tree[(k<<)+].sum//更新当前节点的sum
+ (mid-L+)*tree[k<<].add + (R-mid)*tree[k<<|].add;
} LL query(int k, int L, int R)
{
if(R<a || b<L) return ; if(a<=L && R<=b) return tree[k].sum + (R-L+)*tree[k].add;//注意加上当前节点的标记 int mid = (L+R)>>; updataChild(k, L, R);//吧标记传到子节点 return query(lson) + query(rson);
} int main()
{
while(~scanf("%d %d", &N, &Q))
{
mem0(tree); char ch;
for(a=;a<=N;a++)
{
scanf("%lld%*c", &c); b = a;//区间是[a, a]
update(, , N);
}
for(int i=;i<Q;i++)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d %d%*c", &a, &b);
printf("%lld\n", query(,,N));
}
else
{
scanf("%d %d %lld%*c", &a, &b, &c);
update(,,N);
}
}
}
return ;
}
POJ3468 A Simple Problem with Integers(线段树延时标记)的更多相关文章
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- 用canvas实现图片滤镜效果详解之视频效果
这是一个很有意思的特效,模拟摄像机拍摄电视屏幕画面时出现点状颗粒的效果.颗粒的大小通过变换矩阵实现,可以任意调节,有兴趣研究的朋友可以尝试更多的效果,代码没有经过优化,只是一个粗糙的Demo,大家可以 ...
- MIPI DSI 和 D-PHY 初始化序列
MIPI DSI 和 D-PHY 初始化序列 -- 深圳 南山平山村 曾剑锋 参考文档: i.MX 6Dual/6Quad Multimedia Applications Processor Refe ...
- matlab中,在灰度解剖图上叠加阈值图,by by DR. Rajeev Raizada
1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...
- 响应式设计中几个class区别
table-responsive:在小屏幕时不对内容做任何额外排版,只是允许左右滑动 scrollable-area:先尝试挤压起来,实在不行再左右滑动
- vs2010 please select a valid location in the repository
vs2010 please select a valid location in the repository AnkhSvn版本有问题,我后来使用2.1就ok了记录一下
- Windows服务调用Quartz.net 实现消息调度
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- How do I list all tables/indices contained in an SQLite database
How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...
- Oracle数据库中有关记录个数的查询
一.查询表中全部的记录个数 可用两种方法,一种是在oracle的系统表中统计,另一种需要写存储过程统计,方法分别如下. 1.系统表中统计: SELECT sum(num_rows) FROM user ...
- Linux系统上安装Python
1.下载Python安装包,官网下:http://www.python.org/getit/ http://jingyan.baidu.com/article/eae07827f7f2d01fec54 ...
- 将Tomcat注册为Windows服务
1.从官网http://tomcat.apache.org/下载Tomcat. 2.将Tomcat压缩文件解压到相应的路径下(例如E:\TomcatServer) 3.从bin目录下找到service ...