[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
---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...
随机推荐
- extern “C”的使用
2016-12-11 22:40:48 VS编译的时候,可以指定编译为C代码或者C++代码.c/c++->高级.而当你新建一个cpp文件时,VS很有可能自动会把编译方式由C变成C++编译.然 ...
- 现代数字信号处理——AR模型
1. AR模型概念观 AR模型是一种线性预测,即已知N个数据,可由模型推出第N点前面或后面的数据(设推出P点),所以其本质类似于插值,其目的都是为了增加有效数据,只是AR模型是由N点递推, ...
- JSP自己定义标签继承哪个类
JSP自己定义标签继承哪个类 解:JSP自己定义标签继承TagSupport
- angular - 使用es6等一些功能
app.module.ts var model = { user: 'Admin', items: [{ action: 'buy flowsers', done: false },{ action: ...
- node开发自动刷新网页中的css和javascript
在已有node的环境下,安装browser-sync: npm install -g browser-sync 然后运行,默认本目录下(最后填写要监听的文件--本实例监听了css文件夹下面的所有css ...
- struts1.3中使用DispatchAction的一个问题
近期做项目发现我们公司的项目是用struts1写的,在多方百度下,总有理解了struts1.3的DispatchAction的使用方法 一:struts.xml文件的配置 <?xml versi ...
- 系统安全-SElinux
Selinux是[security-Enhanced Linux]的简称,是美国国家安全局[NSA]和[SCC]开发的Linux的一个扩张强制访问控制安全模块. 因为企业的业务平台的服务器上存储着大量 ...
- SpringMVC学习(一):搭建SpringMVC-注解-非注解
文章参考:http://www.cnblogs.com/Sinte-Beuve/p/5730553.html 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话pom.xml配置依 ...
- HDU 5145 NPY and girls (莫队分块离线)
题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...
- Tabs in Non-RootViewController Scenarios
新建空工程如图 添加一个MainStoryboard如图 设置启动项为MainStoryboard 重写AppDelegate的Window方法 public override UIWindow Wi ...