[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2482

[算法]

线段树维护历史最值

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
typedef long long ll;
typedef long double ld;
const int T = ; struct query
{
int l , r;
int id;
} q[MAXN]; int n , m;
int loc[MAXN << ] , pre[MAXN << ] , val[MAXN << ];
ll ans[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} struct Segment_Tree
{
struct Node
{
int l , r;
ll sum , hsum;
ll taga , tagb;
} a[MAXN << ];
inline void build(int index , int l , int r)
{
a[index].l = l , a[index].r = r;
if (l == r) return;
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
}
inline void pushdown(int index)
{
int l = a[index].l , r = a[index].r;
int mid = (l + r) >> ;
if (l == r) return;
a[index << ].hsum = max(a[index << ].hsum , a[index << ].sum + a[index].tagb);
a[index << | ].hsum = max(a[index << | ].hsum , a[index << | ].sum + a[index].tagb);
a[index << ].sum += a[index].taga;
a[index << | ].sum += a[index].taga;
chkmax(a[index << ].tagb , a[index << ].taga + a[index].tagb);
chkmax(a[index << | ].tagb , a[index << | ].taga + a[index].tagb);
a[index << ].taga += a[index].taga;
a[index << | ].taga += a[index].taga;
a[index].taga = a[index].tagb = ;
}
inline void update(int index)
{
a[index].sum = max(a[index << ].sum , a[index << | ].sum);
a[index].hsum = max(a[index << ].hsum , a[index << | ].hsum);
}
inline void modify(int index , int l , int r , ll val)
{
pushdown(index);
if (a[index].l == l && a[index].r == r)
{
a[index].sum += val;
chkmax(a[index].hsum , a[index].sum);
a[index].taga += val;
chkmax(a[index].tagb , a[index].taga);
} else
{
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) modify(index << , l , r , val);
else if (mid + <= l) modify(index << | , l , r , val);
else
{
modify(index << , l , mid , val);
modify(index << | , mid + , r , val);
}
update(index);
}
}
inline ll query(int index , int l , int r)
{
pushdown(index);
if (a[index].l == l && a[index].r == r)
return a[index].hsum;
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) return query(index << , l , r);
else if (mid + <= l) return query(index << | , l , r);
else return max(query(index << , l , mid) , query(index << | , mid + , r));
}
} SGT; inline bool cmp(query a , query b)
{
return a.r < b.r;
} int main()
{ read(n);
for (int i = ; i <= n; i++) read(val[i]);
read(m);
for (int i = ; i <= m; i++)
{
read(q[i].l);
read(q[i].r);
q[i].id = i;
}
sort(q + , q + m + , cmp);
for (int i = ; i <= n; i++)
{
pre[i] = loc[val[i] + T];
loc[val[i] + T] = i;
}
SGT.build( , , n);
int now = ;
for (int i = ; i <= n; i++)
{
SGT.modify( , pre[i] + , i , val[i]);
while (now <= m && q[now].r == i)
{
ans[q[now].id] = max(SGT.query( , q[now].l , q[now].r) , 0LL);
++now;
}
}
for (int i = ; i <= m; i++) printf("%lld\n" , ans[i]); return ; }

[SPOJ1557] Can you answer these queries II的更多相关文章

  1. BZOJ2482: [Spoj1557] Can you answer these queries II

    题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...

  2. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

  3. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  4. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  5. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  6. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  7. SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树

    传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...

  8. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  9. GSS2-Can you answer these queries II

    ---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...

随机推荐

  1. 第一个AngularJS Sample

    代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3. ...

  2. List&lt;InvestInfoDO&gt; invest = advertiseDao6.qryInvestInfo(InvestInfoDO1);怎样获得list的实体类;

    List<InvestInfoDO>  invest = advertiseDao6.qryInvestInfo(InvestInfoDO1); 怎样获得List的实体类呢,就是怎样获得I ...

  3. flex操作XML,强力总结帖

    初始化XML对象 XML对象可以代表一个XML元素.属性.注释.处理指令或文本元素.在ActionScript 3.0中我们可以直接将XML数据赋值给变量: var myXML:XML =    &l ...

  4. c#高级编程笔记----委托

    因为定义委托基本上是定义一个新类,所以可以在定义类的任何相同地方定义委托,也就是说,可以在另一个类的内部定义,也可以在任何类的外部定义,还可以在名称空间中把委托定义为顶层对象.根据定义的可见性,和委托 ...

  5. 专訪阿里陶辉:大规模分布式系统、高性能server设计经验分享

    http://www.csdn.net/article/2014-06-27/2820432 摘要:先后就职于在国内知名的互联网公司,眼下在阿里云弹性计算部门做架构设计与核心模块代码的编写,主要负责云 ...

  6. mysql手动停止无响应查询方法

    http://www.chenweionline.cn/archives/61.htm

  7. nodejs while-loop

    node-while-loop A while loop alternative for Nodejs based on promises. Install $ npm install --save ...

  8. Kubernetes对象之ReplicaSet

    系列目录 说到ReplicaSet对象,得先说说ReplicationController(简称为RC).在旧版本的Kubernetes中,只有ReplicationController对象.它的主要 ...

  9. 获取当前外网IP地址

    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script><script>cons ...

  10. java 最长回文字串

      package string.string1_6; public class LongestPalidrome { /** * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶 ...