[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
---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...
随机推荐
- mac异常删除管理员账户恢复操作
重新启动电脑,同时按下command+s键进入命令行方式 待系统加载完成后顺序输入以下命令: /sbin/mount -uaw rm var/db/.applesetupdone reboot 待系统 ...
- eclipse下的ssh框架整合过程及測试
最近在搭建Stuts2+hibernate+spring的框架,网上看的教程,大部分都是非常easy的步骤.没有比較具体的步骤以及每一个步骤完毕之后怎样检查是否配置成功.下面是笔者依据自己搭建的过程进 ...
- ffmpeg一些filter使用方法、以及一些功能命令
1.加字幕 命令:ffmpeg -i <input> -filter_complex subtitles=filename=<SubtitleName>-y <outpu ...
- C 标准库 - <limits.h>
C 标准库 - <limits.h> 简介 limits.h 头文件决定了各种变量类型的各种属性.定义在该头文件中的宏限制了各种变量类型(比如 char.int 和 long)的值. 这些 ...
- hdu 1548
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 解决Eclipse中C++代码显示Symbol 'std' could not be resolved的问题
第一次在Eclipse中写C++代码,写了一个简单的hello world程序,还没有等我编译.就报出了各种错误,但是这么简单的代码.怎么可能这么多错误.于是没有理会.编译执行后,能够正常输出!!!H ...
- Python+Selenium框架 ---自动化测试报告的生成
本文来介绍如何生成自动化测试报告,前面文章尾部提到了利用HTMLTestRunner.py来生成自动化测试报告.关于HTMLTestRunner不过多介绍,只需要知道是一个能生成一个HTML格式的网页 ...
- Use the command of tar to multi-part archive method.
We usually meet the package too large to upload internat space when upload have a limited .So we nee ...
- 《好好说话》zz
最近,<奇葩说>闹出来了一些不愉快. 在半决赛中,姜思达惜败,愤怒的粉丝把矛头指向那场比赛的其他人.最终,马薇薇.黄执中和网友们吵起来了. 这件事本不算大事,毕竟娱乐业就是这个样子.刚刚好 ...
- 物联网网络编程和web编程
本文是基于嵌入式物联网研发project师的视觉对网络编程和web编程进行阐述. 对于专注J2EE后端服务开发的同学来说,这篇文章可能略微简单.可是网络编程和web编程对于绝大部分嵌入式物联网proj ...