【BZOJ4491】我也不知道题目名字是什么 [线段树]
我也不知道题目名字是什么
Time Limit: 10 Sec Memory Limit: 512 MB
[Submit][Status][Discuss]
Description
给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串
Input
第一行n,表示A数组有多少元素
接下来一行为n个整数A[i]
接下来一个整数Q,表示询问数量
接下来Q行,每行2个整数l,r
Output
对于每个询问,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串
Sample Input
1 2 3 4 5 6 5 4 3
5
1 6
1 7
2 7
1 9
5 9
Sample Output
6
5
6
4
HINT
N,Q<=50000
Solution
直接上线段树,记录一下一个区间内:左边最长,右边最长,整体最优。再分一下不下降和不上升即可。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std; const int ONE = ;
const int INF = ; int n,Q;
int x,y;
int a[ONE]; struct power
{
int len;
int Lval, Rval;
int Lup,Ldn, Rup,Rdn;
int Maxup, Maxdn; friend power operator +(power a, power b)
{
power A;
A.len = a.len + b.len;
A.Lval = a.Lval; A.Rval = b.Rval; A.Lup = a.Lup; if(a.Lup == a.len && a.Rval <= b.Lval) A.Lup += b.Lup;
A.Ldn = a.Ldn; if(a.Ldn == a.len && a.Rval >= b.Lval) A.Ldn += b.Ldn; A.Rup = b.Rup; if(b.Rup == b.len && a.Rval <= b.Lval) A.Rup += a.Rup;
A.Rdn = b.Rdn; if(b.Rdn == b.len && a.Rval >= b.Lval) A.Rdn += a.Rdn; A.Maxup = max(a.Maxup, b.Maxup);
A.Maxdn = max(a.Maxdn, b.Maxdn); if(a.Rval <= b.Lval) A.Maxup = max(A.Maxup, a.Rup+b.Lup);
if(a.Rval >= b.Lval) A.Maxdn = max(A.Maxdn, a.Rdn+b.Ldn); return A;
}
}Node[ONE]; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} void Build(int i,int l,int r)
{
if(l == r)
{
Node[i].Lval = Node[i].Rval = a[l];
Node[i].len = ;
Node[i].Lup = Node[i].Rup = Node[i].Ldn = Node[i].Rdn = ;
Node[i].Maxup = Node[i].Maxdn = ;
return;
} int mid = l+r>>;
Build(i<<, l,mid); Build(i<<|, mid+,r);
Node[i] = Node[i<<] + Node[i<<|];
} power Query(int i,int l,int r,int L,int R)
{
if(L==l && r==R) return Node[i];
int mid = l+r>>;
if(mid+ > R) return Query(i<<, l,mid,L,R);
else if(L > mid) return Query(i<<|, mid+,r,L,R);
else return Query(i<<, l,mid,L,mid) + Query(i<<|, mid+,r,mid+,R);
} int main()
{
n = get();
for(int i=; i<=n; i++) a[i] = get();
Build(,,n); Q = get();
while(Q--)
{
x = get(); y = get();
power res = Query(,,n,x,y);
printf("%d\n", max(res.Maxup, res.Maxdn) );
}
}
- 提交:315解决:173
【BZOJ4491】我也不知道题目名字是什么 [线段树]的更多相关文章
- BZOJ 4491: 我也不知道题目名字是什么 线段树+离线
code: #include <string> #include <cstring> #include <cstdio> #include <algorith ...
- BZOJ4491: 我也不知道题目名字是什么
Description 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 Input 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一 ...
- 2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)
传送门 题意:给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串. 思路: 注意要求的是子串而不是子序列!!! 然后直接用线段树维护最大子段和的方式合并一 ...
- 【BZOJ4991】我也不知道题目名字是什么(线段树)
[BZOJ4991]我也不知道题目名字是什么(线段树) 题面 BZOJ 题解 对于线段树维护的区间维护以下东西: 区间左(右)端开始(结束)的最长(短)子串的长度 左端右端的值,以及当前区间内的答案 ...
- BZOJ 4491: 我也不知道题目名字是什么
4491: 我也不知道题目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 278 Solved: 154[Submit][Status][ ...
- BZOJ 4491: 我也不知道题目名字是什么 RMQ
4491: 我也不知道题目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 317 Solved: 174[Submit][Status][ ...
- bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并)
bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并) bzoj Luogu 给出一个字符串 $ S $ 及 $ q $ 次询问,每次询问一个字符串 $ T $ ...
- 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树
题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...
- [NOI2018]你的名字(后缀自动机+线段树)
题目描述 小A 被选为了ION2018 的出题人,他精心准备了一道质量十分高的题目,且已经把除了题目命名以外的工作都做好了. 由于ION 已经举办了很多届,所以在题目命名上也是有规定的,ION 命题手 ...
随机推荐
- 数据库索引(结合B-树和B+树)
数据库索引,是数据库管理系统中一个排序的数据结构以协助快速查询.更新数据库表中数据.索引的实现通常使用B树及其变种B+树. 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种 ...
- 数据包从tcp->ip发出去
ip_local_out->OUTPUT->dst_out->ip_output-> POSTROUTING -->ip_output_finish 上面的路径中啊,在O ...
- BZOJ4773 负环(floyd+倍增)
倍增floyd求出经过<=2k条边时两点间最短路,一个点到自身的最短路就是包含该点的最小环.然后倍增找答案即可.注意初始时到自身的最短路设为0,这样求出的最短路就是经过<=2k条边的而不是 ...
- hdu 3549 Flow Problem (网络最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- Andorid API Package ---> android.app
包名: android.app Added in API level 1 URL:http://developer. ...
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
一.RadioButton / CheckBox 系统自带的RadioButton/CheckBox的样式,注定满足不了实际运用中的情况,有时候自定义自己的样式:此次把自己中工作学习过程中所学到的东西 ...
- POJ2104:K-th Number——题解
http://poj.org/problem?id=2104 题目大意:求区间第k小. —————————————————————————— 主席树板子题. ……我看了半天现在还是一知半解的状态所以应 ...
- BZOJ4698 & 洛谷2463:[SDOI2008]Sandy的卡片——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4698 https://www.luogu.org/problemnew/show/P2463#sub ...
- BZOJ2453:维护队列——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2453 Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到 ...
- BZOJ3343 & 洛谷2801:教主的魔法——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 https://www.luogu.org/problemnew/show/2801 题目描述 ...