bzoj1558
好题,初看以为只要差分然后维护相同的段数目
但是请注意下面的情况
2 3 5 8 9
1 2 3 4 这显然答案是3而不是4
因此我们还要再维护ld,rd表示左右单独的段长度
和s表示不包括左右单独的段,中间部分最少划分成几个等差数列
具体维护见程序,比较复杂,但其实不难
这题还有一个坑爹的地方,我一开始忘开int64本地测数据竟然全能过
但是交上去就WA……感人肺腑
type node=record
l,r:int64;
ld,rd,s:longint;
end; var tree:array[..*] of node;
lazy:array[..*] of int64;
a:array[..] of longint;
n,m,i,x0,y0,x,y,p,q:longint;
z:int64;
ans:node;
ch:char; procedure update(var a:node;x,y:node);
begin
a.l:=x.l;
a.r:=y.r;
a.ld:=x.ld;
if x.s= then //x.s=说明左部分全是单独的(没有连续出现2个及以上的)
begin
if x.r<>y.l then inc(a.ld,y.ld)
else dec(a.ld);
end;
a.rd:=y.rd;
if y.s= then
begin
if x.r<>y.l then inc(a.rd,x.rd)
else dec(a.rd);
end;
a.s:=x.s+y.s;
if x.s= then //以下请自行理解
begin
if x.r=y.l then
begin
if y.s= then inc(a.s)
else if (y.ld>) then inc(a.s,(y.ld-) shr +);
end;
end
else if x.rd> then
begin
if x.r=y.l then
begin
inc(a.s,(x.rd-) shr );
if y.s= then inc(a.s)
else if (y.ld>) then inc(a.s,(y.ld-) shr +);
end
else if y.s> then
inc(a.s,(x.rd+y.ld) shr );
end
else begin
if x.r=y.l then
begin
if (y.s>) and (y.ld>) then inc(a.s,(y.ld-) shr );
if (y.s>) and (y.ld=) then dec(a.s);
end
else if (y.s>) and (y.ld>) then inc(a.s,y.ld shr );
end;
end; procedure push(i:longint);
begin
inc(lazy[i*],lazy[i]);
inc(tree[i*].l,lazy[i]);
inc(tree[i*].r,lazy[i]);
inc(lazy[i*+],lazy[i]);
inc(tree[i*+].l,lazy[i]);
inc(tree[i*+].r,lazy[i]);
lazy[i]:=;
end; procedure build(i,l,r:longint);
var m:longint;
begin
if l=r then
begin
tree[i].l:=a[l+]-a[l];
tree[i].r:=a[l+]-a[l];
tree[i].ld:=;
tree[i].rd:=;
end
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
update(tree[i],tree[i*],tree[i*+]);
end;
end; procedure add(i,l,r:longint);
var m:longint;
begin
if (x<=l) and (y>=r) then
begin
inc(tree[i].l,z);
inc(tree[i].r,z);
lazy[i]:=lazy[i]+z;
end
else begin
if lazy[i]<> then push(i);
m:=(l+r) shr ;
if x<=m then add(i*,l,m);
if y>m then add(i*+,m+,r);
update(tree[i],tree[i*],tree[i*+]);
end;
end; function ask(i,l,r:longint):node;
var s,s1,s2:node;
m:longint;
begin
if (x<=l) and (y>=r) then exit(tree[i])
else begin
m:=(l+r) shr ;
if lazy[i]<> then push(i);
if y<=m then exit(ask(i*,l,m));
if x>m then exit(ask(i*+,m+,r));
s1:=ask(i*,l,m);
s2:=ask(i*+,m+,r);
update(s,s1,s2);
exit(s);
end;
end; begin
readln(n);
for i:= to n do
readln(a[i]);
dec(n);
build(,,n);
readln(m);
for i:= to m do
begin
read(ch);
if ch='A' then
begin
readln(x0,y0,p,q);
if x0> then //修改要分三种情况
begin
x:=x0-; y:=x0-; z:=p;
add(,,n);
end;
if y0<=n then
begin
x:=y0; y:=y0; z:=-int64(p)-int64(y0-x0)*int64(q);
add(,,n);
end;
if x0<y0 then
begin
x:=x0; y:=y0-; z:=q;
add(,,n);
end;
end
else begin
readln(x,y);
if y=x then writeln()
else begin
dec(y);
ans:=ask(,,n);
if ans.s= then writeln((y-x+) shr ) //全是单独的可以直接算出来
else writeln(ans.s+(ans.ld+) shr +(ans.rd+) shr );
end;
end;
end;
end.
bzoj1558的更多相关文章
- 【BZOJ1558】等差数列(线段树)
[BZOJ1558]等差数列(线段树) 题面 BZOJ 题解 可以说这道题已经非常毒瘤了 怎么考虑询问操作? 如果直接将一段数分解为等差数列? 太麻烦了.... 考虑相邻的数做差, 这样等差数列变为了 ...
- BZOJ1558 [JSOI2009]等差数列 【线段树】
题目链接 BZOJ1558 题解 等差数列,当然是差分一下 差分值相同的连续位置形成等差数列,我们所选的两个等差数列之间可以有一个位置舍弃 例如: \(1 \; 2 \; 3 \; 6 \; 8 \; ...
- [bzoj1558][JSOI2009]等差数列
题目:给定n个数,m个操作,每次给一段区间加一个等差数列或者询问一段区间至少要用多少个等差数列来表示.$n,m\leqslant 10^{5}$ 题解:老套路,维护差分数组,修改操作变成了两个单点加和 ...
- 洛谷P4243/bzoj1558 [JSOI2009]等差数列(线段树维护差分+爆炸恶心的合并)
题面 首先感谢这篇题解,是思路来源 看到等差数列,就会想到差分,又有区间加,很容易想到线段树维护差分.再注意点细节,\(A\)操作完美解决 然后就是爆炸恶心的\(B\)操作,之前看一堆题解的解释都不怎 ...
- BZOJ1558 等差数列
题目链接:戳我 实话实话,看了几篇题解真的没看懂,我觉得讲的都有问题.这里对于线段树维护的s写了一点我自己的理解. 看到等差数列,我们考虑对数列做差,这样如果是等差数列,那么值应该相等.(比较容易维护 ...
- 线段树 by yyb
线段树 by yyb Type1 维护特殊信息 1.[洛谷1438]无聊的数列 维护一个数列,两种操作 1.给一段区间加上一个等差数列 2.单点询问值 维护等差数列 不难发现,等差数列可以写成\(ad ...
- OI题目类型总结整理
## 本蒟蒻的小整理qwq--持续更新(咕咕咕) 数据结构 数据结构 知识点梳理 数据结构--线段树 推荐yyb dalao的总结--戳我 以后维护线段树还是把l,r写到struct里面吧,也别写le ...
随机推荐
- 安卓百度地图开发so文件引用失败问题研究
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 问题 首先,下面的问题基本都是在Android Studio下使用不当导致,eclipse是百 ...
- Js中的appenChild,insertBefore--createDocumentFragment
平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...
- IOS中的NSTimer定时器详解
/* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...
- Oracle存储过程的理解
在大专时候学的专业是数据库管理专业,在学校学了各种各样的数据 MSSQL.ORACLE.MySQL. 那时候学数据大部分只学到了些皮毛,仅仅只会按照书上SQL语句,输入计算机得出结果,就很有成就感. ...
- asp.net WebService异步
1 #region 异步测试 2 //委托 3 public delegate void PrintDelegate(string s); 4 [WebMethod] 5 public string ...
- javascript面向对象程序设计系列(一)---创建对象
javascript是一种基于对象的语言,但它没有类的概念,所以又和实际面向对象的语言有区别,面向对象是javascript中的难点之一.现在就我所理解的总结一下,便于以后复习: 一.创建对象 1.创 ...
- 【Java咬文嚼字】关键字(二):八个基本数据类型
本次谈谈java中的基本数据类型.java中8个基本数据类型说多不多说少也不少,相对简单的记忆方式:整数型(byte,short,int,long).浮点型(float,double).逻辑型(boo ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- hdu 1286 找新朋友 (欧拉函数)
Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的 ...
- shell中的条件判断
read命令 命令的语法: read [参数] 变量名 常用的参数如下: -t timeout 设定超时时间. -p prompt 设定提示信息,该提示信息将会显在光标前. 条件测试: 条件测试可以判 ...