好题,初看以为只要差分然后维护相同的段数目
但是请注意下面的情况
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的更多相关文章

  1. 【BZOJ1558】等差数列(线段树)

    [BZOJ1558]等差数列(线段树) 题面 BZOJ 题解 可以说这道题已经非常毒瘤了 怎么考虑询问操作? 如果直接将一段数分解为等差数列? 太麻烦了.... 考虑相邻的数做差, 这样等差数列变为了 ...

  2. BZOJ1558 [JSOI2009]等差数列 【线段树】

    题目链接 BZOJ1558 题解 等差数列,当然是差分一下 差分值相同的连续位置形成等差数列,我们所选的两个等差数列之间可以有一个位置舍弃 例如: \(1 \; 2 \; 3 \; 6 \; 8 \; ...

  3. [bzoj1558][JSOI2009]等差数列

    题目:给定n个数,m个操作,每次给一段区间加一个等差数列或者询问一段区间至少要用多少个等差数列来表示.$n,m\leqslant 10^{5}$ 题解:老套路,维护差分数组,修改操作变成了两个单点加和 ...

  4. 洛谷P4243/bzoj1558 [JSOI2009]等差数列(线段树维护差分+爆炸恶心的合并)

    题面 首先感谢这篇题解,是思路来源 看到等差数列,就会想到差分,又有区间加,很容易想到线段树维护差分.再注意点细节,\(A\)操作完美解决 然后就是爆炸恶心的\(B\)操作,之前看一堆题解的解释都不怎 ...

  5. BZOJ1558 等差数列

    题目链接:戳我 实话实话,看了几篇题解真的没看懂,我觉得讲的都有问题.这里对于线段树维护的s写了一点我自己的理解. 看到等差数列,我们考虑对数列做差,这样如果是等差数列,那么值应该相等.(比较容易维护 ...

  6. 线段树 by yyb

    线段树 by yyb Type1 维护特殊信息 1.[洛谷1438]无聊的数列 维护一个数列,两种操作 1.给一段区间加上一个等差数列 2.单点询问值 维护等差数列 不难发现,等差数列可以写成\(ad ...

  7. OI题目类型总结整理

    ## 本蒟蒻的小整理qwq--持续更新(咕咕咕) 数据结构 数据结构 知识点梳理 数据结构--线段树 推荐yyb dalao的总结--戳我 以后维护线段树还是把l,r写到struct里面吧,也别写le ...

随机推荐

  1. php中怎么实现后台执行?

    http://www.cnblogs.com/zdz8207/p/3765567.html php中实现后台执行的方法: ignore_user_abort(true); // 后台运行set_tim ...

  2. 广告系统中weak-and算法原理及编码验证

    wand(weak and)算法基本思路 一般搜索的query比较短,但如果query比较长,如是一段文本,需要搜索相似的文本,这时候一般就需要wand算法,该算法在广告系统中有比较成熟的应 该,主要 ...

  3. (三)映射对象标识符(OID)

    所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 用对象标识符(OID)来区分对象 例子: h ...

  4. ASP.NET 预编译

    ASP.NET 预编译概述 https://msdn.microsoft.com/zh-cn/library/bb398860%28v=VS.90%29.aspx 如何:预编译 ASP.NET 网站以 ...

  5. oc文件基本读写及操作

    代码: #import <Foundation/Foundation.h> //NSString 写文件 void stringWriteToFile(){ NSString *path ...

  6. C++文件逐字节对比

    作为一个OIer,装逼的本事是必不可少的.最近,为了展示自己CPP水平,为同学们做了一个Cena(当然,命令行界面,很迷你).但是,却在文件对比这个最重要的环节卡住了. 我想了很多办法,首先想到的就是 ...

  7. java_UML:继承/泛化、实现、依赖、关联、聚合、组合的联系与区别 (2016-07-12)

    分别介绍这几种关系: UML关系:继承(泛化).实现.依赖.关联.聚合.组合的联系与区别 一.表示符号上的区别 二.具体区别与联系 1. 继承/泛化(Generalization) [泛化关系]:是一 ...

  8. (转)UIColor 的使用

    os开发-UIColor的使用. 在ios开发中,经常遇到对UIColor的相关操作. 比如这样 self.backgroundColor = [UIColorredColor]; 这里的redCol ...

  9. leetcode problem 6 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. html标签data大写获取不到值:只能小写+横杠命名

    html标签data大写获取不到值:只能小写+横杠命名 例如: <i class="glyphicon glyphicon-question-sign" data-tip-t ...