bzoj2482
还是像以前那样维护下次出现位置,计算影响
其实不难,思维盲点,受到做最大子段和的影响
其实这里可以直接维护当前每个位置的子段和,再记录一个历史最大和
当然tag也需要记录当前tag和历史(距离上次push)最大累加
type node=record
x,y,id:longint;
end; var lazy,tree:array[..*,..] of longint;
ans,next,a:array[..] of longint;
last:array[-..] of longint;
q:array[..] of node;
i,j,n,m:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=q[(l+r) shr ].x;
repeat
while q[i].x>x do inc(i);
while x>q[j].x do dec(j);
if i<=j then
begin
swap(q[i],q[j]);
inc(i);
dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; procedure get(i,x0,x1:longint);
begin
tree[i,]:=max(tree[i,],tree[i,]+max(x0,x1));
lazy[i,]:=max(lazy[i,],lazy[i,]+max(x0,x1));
inc(lazy[i,],x1);
inc(tree[i,],x1);
end; procedure update(i:longint);
begin
tree[i,]:=max(tree[i*,],tree[i*+,]);
tree[i,]:=max(tree[i*,],tree[i*+,]);
end; procedure push(i:longint);
begin
if (lazy[i,]=) and (lazy[i,]=) then exit;
get(i*,lazy[i,],lazy[i,]);
get(i*+,lazy[i,],lazy[i,]);
lazy[i,]:=;
lazy[i,]:=;
end; procedure add(i,l,r,x,y,z:longint);
var m:longint;
begin
if (x<=l) and (y>=r) then get(i,,z)
else begin
m:=(l+r) shr ;
push(i);
if x<=m then add(i*,l,m,x,y,z);
if y>m then add(i*+,m+,r,x,y,z);
update(i);
end;
end; function ask(i,l,r,x:longint):longint;
var m:longint;
begin
if l=r then exit(tree[i,])
else begin
m:=(l+r) shr ;
push(i);
if x<=m then exit(ask(i*,l,m,x))
else exit(max(tree[i*,],ask(i*+,m+,r,x)));
end;
end; begin
readln(n);
for i:= to n do
read(a[i]);
for i:=n downto do
begin
next[i]:=last[a[i]];
last[a[i]]:=i;
end;
readln(m);
for i:= to m do
begin
readln(q[i].x,q[i].y);
q[i].id:=i;
end;
sort(,m);
j:=;
for i:=n downto do
begin
if next[i]= then next[i]:=n+;
add(,,n,i,next[i]-,a[i]);
while (j<=m) and (q[j].x=i) do
begin
ans[q[j].id]:=ask(,,n,q[j].y);
inc(j);
end;
if j=m+ then break;
end;
for i:= to m do
writeln(ans[i]);
end.
bzoj2482的更多相关文章
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
- BZOJ2482: [Spoj1557] Can you answer these queries II
题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
随机推荐
- oracle——DDL
一.一些概念 定义: 主键--唯一标识一条记录,不能有重复的,不允许为空 外键--表的外键是另一表的主键, 外键可以有重复的, 可以是空值 索引--该字段没有重复值,但可以有一个空值 作用: 主键-- ...
- 2012 Asia JinHua Regional Contest
Draw Something http://acm.hdu.edu.cn/showproblem.php?pid=4450 o(n)统计输入每个数的平方和. #include<cstdio> ...
- phonegap file操作
phonegap中,有时候需要操作到手机中的文件,这个时候就需要用到phonegap官方提供的插件 file ,英文好的可以直接参考官方文档 首先是安装插件:(需要phonegap 3.0 以上,不止 ...
- NC保存报dirty解决方法
在NC UI端测试保存的时候报 " The data whose initcode is 6033 is dirty! " 错误,其解决方式是 在IHrPf接口中添加你的单据模板编号 和 参数模板 ...
- java给图片加水印代码
try { String targetImg = "D:/Blue hills.jpg"; // String pressImg = "D:/20130311220300 ...
- NET Framework 4 中的新 C# 功能
http://msdn.microsoft.com/zh-cn/magazine/ff796223.aspx C# 编程语言自 2002 年初次发布以来已经有了极大的改善,可以帮助程序员编写更清晰易懂 ...
- hbase操作的问题
写了一个java程序,需要向hbase中写入大量的数据,但是这个程序执行一半就报错, 问题是,此时已经写入了很多数据. 查看jps,发现hmaster进程崩溃了. 基于以上信息,发现是在程序中,链接h ...
- mac os 下如何清除/切换svn eclipse插件的用户
以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al ... drwxr-xr-x 6 linxyz staff ...
- jQuery对象与Dom对象的相互转换
1.jQuery对象转换为Dom对象 [index] var $d = $("#id"); ]; get(index) var $d = $("#id"); ) ...
- Project Euler 87 :Prime power triples 素数幂三元组
Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...