【BZOJ2482】[Spoj1557] Can you answer these queries II

Description

给定n个元素的序列。 
给出m个询问:求l[i]~r[i]的最大子段和(可选空子段)。 
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。 
比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6。

Input

第一行一个数n。 
第二行n个数,为给定的序列,这些数的绝对值小于等于100000。 
第三行一个数m。 
接下来m行,每行两个数,l[i],r[i]。

Output

M行,每行一个数,为每个询问的答案。

Sample Input

9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9

Sample Output

4
5
3

HINT

【数据说明】
30%:1 <= n, m <= 100
100%:1 <= n, m <= 100000

题解:还是考虑这点:每个子串都是某个前缀的后缀,所以我们依旧枚举每个前缀,用线段树维护它的所有后缀。

出现两次只算一次怎么办?只需要在扫到一个数的时候将它上一个出现的位置变成0即可,这样就会改边很多后缀的值,用线段树去维护。

但是本题求的是最大连续子段和啊,其实只需要维护每个后缀的历史最大值就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=100010;
typedef long long ll;
int n,m;
int last[maxn<<1],pre[maxn],v[maxn];
ll ans[maxn];
struct node
{
ll t,ht,s,hs;
}s[maxn<<2];
struct QUERY
{
int l,r,org;
}q[maxn];
inline void phd(int x,int y)
{
if(y>0) s[x].hs=max(s[x].hs,s[x].s+y),s[x].ht=max(s[x].ht,s[x].t+y);
}
inline void pd(int x,int y)
{
s[x].s+=y,s[x].t+=y;
if(y>0) s[x].hs=max(s[x].hs,s[x].s),s[x].ht=max(s[x].ht,s[x].t);
}
inline void pushdown(int x)
{
if(s[x].ht) phd(lson,s[x].ht),phd(rson,s[x].ht),s[x].ht=0;
if(s[x].t) pd(lson,s[x].t),pd(rson,s[x].t),s[x].t=0;
}
inline void pushup(int x)
{
s[x].s=max(s[lson].s,s[rson].s),s[x].hs=max(s[lson].hs,s[rson].hs);
}
void updata(int l,int r,int x,int a,int b,int c)
{
if(a<=l&&r<=b)
{
pd(x,c);
return ;
}
pushdown(x);
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,c);
if(b>mid) updata(mid+1,r,rson,a,b,c);
pushup(x);
}
ll query(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return s[x].hs;
pushdown(x);
int mid=(l+r)>>1;
if(b<=mid) return query(l,mid,lson,a,b);
if(a>mid) return query(mid+1,r,rson,a,b);
return max(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
}
bool cmp(const QUERY &a,const QUERY &b)
{
return a.r<b.r;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int main()
{
n=rd();
int i,j;
for(i=1;i<=n;i++) v[i]=rd(),pre[i]=last[v[i]+100000],last[v[i]+100000]=i;
m=rd();
for(i=1;i<=m;i++) q[i].l=rd(),q[i].r=rd(),q[i].org=i;
sort(q+1,q+m+1,cmp);
for(i=j=1;i<=n;i++)
{
updata(1,n,1,pre[i]+1,i,v[i]);
for(;q[j].r==i;j++) ans[q[j].org]=query(1,n,1,q[j].l,i);
}
for(i=1;i<=m;i++) printf("%lld\n",ans[i]);
return 0;
}

最新文章

  1. Python3 登陆网页并保持cookie
  2. IOS App 右上脚红色数字提醒
  3. C++开发过程多线程同步lock的实现
  4. HackerRank &quot;Flatland Space Stations&quot;
  5. DDD~Unity在DDD中的使用
  6. Scala入门之函数
  7. git中找回丢失的对象
  8. 颗粒翻页(css3效果展示)
  9. MySQL的left join中on与where的区别
  10. 【转】plist文件的内容清空
  11. Win7中,取消共享文件夹后有个小锁
  12. SVN客户端--TortoiseSVN使用说明(转)
  13. 备注ocp_ORACLE专题网络
  14. 排序算法学习,python实现
  15. [smartMenu.js] 一个基于jquery的实用的右键拓展菜单栏插件
  16. java数组:去重,增加,删除元素
  17. lvs+keepalive实现双主模式(采用DR),同时实现TCP和UDP检测实现非web端的负载均衡,同时实现跨网段的通讯
  18. 那些年我们一起踩过的Dubbo&quot;坑&quot;
  19. [PKUWC2019]Day1 T2 你和虚树的故事
  20. 剖析项目多个logback配置(上)

热门文章

  1. 持续集成之Jenkins+Gitlab简介 [一]
  2. JSONObject和JSONArray(json-lib-2.4)的基本用法
  3. CSS/JavaScript hacks,browserhacks使用
  4. SQL Server 2008R2发布与订阅的配置
  5. 经典SQL语句使用方法大全
  6. HDU - 1358 - Period (KMP)
  7. redis主从和主从切换
  8. 基于Verilog的以2为底取对数函数log2(x)
  9. OCR 即 光学字符识别
  10. 过滤NSString中的Emoji