BZOJ4491: 我也不知道题目名字是什么
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
样例解释
五个询问分别对应
[1,6][1,6][2,6][1,6][6,9]
HINT
N,Q<=50000
Source
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=50010;
const int maxnode=6000010;
struct Query {int l,r,id,next;}Q[maxnode];
int n,cnt,ans[maxn],f[maxn],g[maxn],A[maxn],first[maxn*4],res[maxn*4];
void AddQuery(int o,int ql,int qr,int id) {
Q[++cnt]=(Query){ql,qr,id,first[o]};first[o]=cnt;
}
void solve(int o,int l,int r) {
int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(l==r) {
for(int i=first[o];i;i=Q[i].next) ans[Q[i].id]=max(ans[Q[i].id],1);
res[o]=1;
}
else {
for(int i=first[o];i;i=Q[i].next) {
Query& T=Q[i];
if(T.r<=mid) AddQuery(lc,T.l,T.r,T.id);
else if(T.l>mid) AddQuery(rc,T.l,T.r,T.id);
else if(T.l!=l||T.r!=r) AddQuery(lc,T.l,mid,T.id),AddQuery(rc,mid+1,T.r,T.id);
}
solve(lc,l,mid);solve(rc,mid+1,r);
res[o]=max(res[lc],res[rc]);
int ok;
ok=1;f[mid+1]=1;
rep(i,mid+2,r) {
if(A[i-1]<A[i]) ok=0;
f[i]=f[i-1]+ok;
}
ok=1;if(A[mid]<A[mid+1]) ok=0;f[mid]=ok;
dwn(i,mid-1,l) {
if(A[i+1]>A[i]) ok=0;
f[i]=f[i+1]+ok;
}
res[o]=max(res[o],f[l]+f[r]);
ok=1;g[mid+1]=1;
rep(i,mid+2,r) {
if(A[i-1]>A[i]) ok=0;
g[i]=g[i-1]+ok;
}
ok=1;if(A[mid]>A[mid+1]) ok=0;g[mid]=ok;
dwn(i,mid-1,l) {
if(A[i+1]<A[i]) ok=0;
g[i]=g[i+1]+ok;
}
res[o]=max(res[o],g[l]+g[r]);
for(int i=first[o];i;i=Q[i].next) {
Query& T=Q[i];
if(T.l<=mid&&T.r>mid) ans[T.id]=max(ans[T.id],max(f[T.l]+f[T.r],g[T.l]+g[T.r]));
if(T.l==l&&T.r==r) ans[T.id]=max(ans[T.id],res[o]);
}
}
}
int main() {
n=read();rep(i,1,n) A[i]=read();
int m=read();
rep(i,1,m) {
int l=read(),r=read();
AddQuery(1,l,r,i);
}
solve(1,1,n);
rep(i,1,m) printf("%d\n",ans[i]);
return 0;
}
BZOJ4491: 我也不知道题目名字是什么的更多相关文章
- 2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)
传送门 题意:给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串. 思路: 注意要求的是子串而不是子序列!!! 然后直接用线段树维护最大子段和的方式合并一 ...
- 【BZOJ4491】我也不知道题目名字是什么 [线段树]
我也不知道题目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 给定一个序列A[i ...
- 【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][ ...
- 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树
题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...
- BZOJ 4491: 我也不知道题目名字是什么 线段树+离线
code: #include <string> #include <cstring> #include <cstdio> #include <algorith ...
- [原]hdu2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (这个只是题目名字) (多重背包)
本文出自:http://blog.csdn.net/svitter 原题:http://acm.hdu.edu.cn/showproblem.php?pid=2191 题意:多重背包问题.转换成为01 ...
- Linux shell 只删除目录下所有(不知道文件名字)文件,只删除文件夹
#!/bin/sh RM="rm -rf" function delete_all_dir() { for i in `ls` do if [ -d $i ];then $RM $ ...
随机推荐
- 【翻译二十】-java线程池
Thread Pools Most of the executor implementations in java.util.concurrent use thread pools, which co ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- 单机安装Hadoop环境
目的 这篇文档的目的是帮助你快速完成单机上的Hadoop安装与使用以便你对Hadoop分布式文件系统(HDFS)和Map-Reduce框架有所体会,比如在HDFS上运行示例程序或简单作业等. 先决条件 ...
- sqlplus 中spool命令的简单用法
spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...
- [Eclipse] Eclipse is running in a JRE, but a JDK is required
安装Maven后每次启动出现警告信息: Eclipse is running in a JRE, but a JDK is required Some Maven plugins may not wo ...
- 字节流、字符串、16进制字符串转换__Java(转)
/** * @Package: * @ClassName:TypeConversion * @Description:字节流.字符串.16进制字符串转换 * @author:xk * @date:Ja ...
- FZU Problem 2082 过路费 树链剖分
Problem 2082 过路费 Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...
- Get open Popups
public IEnumerable<Popup> GetOpenPopups() { return PresentationSource.CurrentSources.OfType< ...
- BPEL是个什么东东
研究团队有个做智能服务组合的,其中用到叫BPEL的东西,因为全称是Business Process Execution Language,译成中文就是商业执行过程语言,这个东东的是整合SOA的一个执行 ...
- java 请求 google translate
// */ // ]]> java 请求 google translate Table of Contents 1. 使用Java获取Google Translate结果 1.1. 开发环境设置 ...