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的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
随机推荐
- window dos 设置网络
->netsh ->pushd interface ip ->set address "本地连接" static 192.168.1.2 255.255.255. ...
- Oracle中的 UPDATE FROM 解决方法
转:http://www.cnblogs.com/JasonLiao/archive/2009/12/23/1630895.html Oracle中的 UPDATE FROM 解决方法 在表的更新操作 ...
- DelayedOperationPurgatory之purgatory的实现
purgatory的超时检测 当一个DelayedOpeartion超时(timeout)时,它需要被检测出来,然后调用它的回调方法.这个事情看起来很简单,但做好也并不容易. 0.8.x的Kafka的 ...
- 让32位Eclipse和64位Eclipse同时在64的Windows7上运行
转自让32位Eclipse和64位Eclipse同时在64的Windows7上运行 参考这篇文章:http://wenku.baidu.com/view/57994c270066f5335a81214 ...
- 学习笔记--Git安装 创建版本库 图文详解
一.Git下载 在Windows上安装git,一般为msysgit,官网地址:http://git-scm.com/ 我下载的是Git-1.9.2-preview20140411.exe 二.Git安 ...
- 用django-tinymce搞个富文本编辑器
玩过一圈之后,这些应用慢慢变得简单: 步骤如下: 一,安装: pip install django-tinymce 二,配置APP: INSTALLED_APPS = ( ... 'tinymce', ...
- Spring整合Ibatis
Spring整合Ibatis javaibatisspring 所需jar清单 ibatis-2.*.jar *为任意版本,下同,ibatis工作包 sp ...
- http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html
http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html http://www.cnblogs.com/langtianya/a ...
- Project Euler 100 : Arranged probability 安排概率
Arranged probability If a box contains twenty-one coloured discs, composed of fifteen blue discs and ...
- linux快速修改文件夹及文件下所有文件与文件夹权限
分两部分改属主和权限: 更改权限,递归方式 chmod -R 755 /var/www/html/test.com 更改属主,递归 chown -R apache:apache /var/www/ht ...