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的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
随机推荐
- JS实现刷新iframe的方法
<iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe> ...
- 微信wap开发,页面显示元素不全-微信开发(asp.net)
最近在开发的微信的微商城,出现这样一种情况: pc上浏览正常,但是一到手机上浏览就会缺少部分元素 解决办法: 找了很多原因,还通过uc浏览器把网页到存下来了,发现并没有缺少元素,只是没有显示出来,后来 ...
- arcgis for server 登陆manager失败解决办法
版本是 arcgis for server 10.02 症状 1. manager网页无法打开http://localhost:6080/arcgis/manager/ 2. 查看服务无法启动,启动后 ...
- C# IL DASM 使用
IL DASM反编译工具 使用C#的猿人或多或少都会对微软的IL反编译工具(ildasm.exe)有所认识.我最早接触到这工具是公司同事使用他反编译exe程序,进行研读和修改.感觉他还是很强大. IL ...
- windows 下c++编译
http://blog.csdn.net/dyllove98/article/details/9314993
- C++ explict 关键字
关键字explicit可以禁止“单参数构造函数”被用于自动类型转换class Stack{explicit Stack(int size);};没有explicit的话Stack s = 40;能编译 ...
- Unity3d 接入 移动MM支付SDK(2.3) 全攻略
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26842177 先将例程运行起来 下载例程(csdn积分不够上传不了,只能用百度网盘 ...
- hdu 1517 A Multiplication Game 博弈论
思路:求必胜区间和必败区间! 1-9 先手胜 10-2*9后手胜 19-2*9*9先手胜 163-2*2*9*9后手胜 …… 易知右区间按9,2交替出现的,所以每次除以18,直到小于18时就可以直接判 ...
- MySQL 创建数据库并且指定编码
GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...
- CKeditor 配置使用
CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...