[SPOJ1557] Can you answer these queries II
[题目链接]
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的更多相关文章
- BZOJ2482: [Spoj1557] Can you answer these queries II
题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树
传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
- GSS2-Can you answer these queries II
---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...
随机推荐
- windows pipe
管道分为 匿名管道 和 命名管道 . 1.匿名管道仅仅能在父子进程间进行通信.不能在网络间通信,并且传输数据是单向的.仅仅能一端写,还有一端读. 2.命令管道能够在随意进程间通信.通信是双向的,随意一 ...
- HeadFirst设计模式 之 C++实现(三):Decorator(装饰者模式)
装饰者模式是非常有意思的一种设计模式,你将可以在不改动不论什么底层代码的情况下.给你的(或别人的)对象赋予新的职责. 不是使用继承每回在编译时超类上改动代码,而是利用组合(composition)和托 ...
- python_获得列表中重复的项的索引
a = ['b','a', 'b', 'c', 'a', 'c','d'] b=[] f=[] for i in a: c=[] for item in enumerate(a): if item[1 ...
- C#如何生成release版本的程序,生成debug版本的程序
除了右击项目在生成中配置改成Release还要在顶部切换成Release
- android 5.2
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2VyZ2V5Y2Fv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- Android多线程下载大文件解析
1.多线程介绍 用过迅雷的同学都知道.迅雷有个功能叫做多线程.另一个叫离线下载,我们这里重点介绍一下多线程下载.多线程,顾名思义就是非常多歌线程同一时候在执行,为什么要提出多线程这个概念呢?由于有时候 ...
- Kubernetes对象之Service
系列目录 通过ReplicaSet来创建一组Pod来提供具有高可用性的服务.虽然每个Pod都会分配一个单独的Pod IP,然而却存在如下两问题: Pod IP仅仅是集群内可见的虚拟IP,外部无法访问. ...
- 不使用flash实现复制文字(图片)到剪贴板
<div>这里是待复制的文字或图片</div> var range = document.createRange(); var referenceNode = document ...
- Oracle 索引 简单介绍
1 索引的创建语法: CREATE UNIUQE | BITMAP INDEX <schema>.<index_name> ON <schema>.&l ...
- javascript点滴积累
1. javascript中的array, set, map 均为数据容器,使用iterable内置的forEach方法 var a = ['A', 'B', 'C'];a.forEach(funct ...