[题目链接]

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. 非GUI模式下运行JMeter和远程启动JMeter

    JMeter是一款非常不错的免费开源压力测试工具,越来越多的公司在使用.不过,在使用过程中可能会存在一些问题,比如:GUI模式非常消耗资源,单个客户端测试无法达到目标压力.而使用非 GUI 模式,即命 ...

  2. sql的一些知识_函数_汇总数据

    汇总数据 avg()---------求平均数 值得注意的是:avg()只能用于一个列的平均值查询,多个列的平均值请使用多个avg() avg()忽略null值 count()-------计数(指定 ...

  3. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. vue2 + typescript2 自定义过滤器

    1.定义一个过滤器 // color-directive.ts import { DirectiveOptions } from 'vue' const directive: DirectiveOpt ...

  5. 解决Eclipse中C++代码显示Symbol &#39;std&#39; could not be resolved的问题

    第一次在Eclipse中写C++代码,写了一个简单的hello world程序,还没有等我编译.就报出了各种错误,但是这么简单的代码.怎么可能这么多错误.于是没有理会.编译执行后,能够正常输出!!!H ...

  6. iOS 获取图片某一点的颜色对象(UIColor*)。

    - (UIColor *)colorAtPixel:(CGPoint)point { // Cancel if point is outside image coordinates if (!CGRe ...

  7. Android调用JNI本地方法跟踪目标代码

    正如Android调用JNI本地方法经过有点改变章所说跟踪代码是可行的,但是跟踪某些代码会出现anr,点击取消,还是不好运,有提高办法吗?回答是有(gdb还没试过,本文只讨论ida). 下面是我使用  ...

  8. kubernetes容器探针检测

    系列目录 kubernetes提供了livenessProbe(可用性探针)和readinessProbe(就绪性探针)对容器的健康性进行检测,当然这仅仅简单的关于可用性方面的探测,实际上我们不仅仅要 ...

  9. AngularJs 在控制器中过滤

    <html> <head> <title>Simple app</title> <script src="https://ajax.go ...

  10. HDU 6074 Phone Call LCA + 并查集

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pro ...