我也不知道题目名字是什么

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

  9
  1 2 3 4 5 6 5 4 3
  5
  1 6
  1 7
  2 7
  1 9
  5 9

Sample Output

  6
  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】我也不知道题目名字是什么 [线段树]的更多相关文章

  1. BZOJ 4491: 我也不知道题目名字是什么 线段树+离线

    code: #include <string> #include <cstring> #include <cstdio> #include <algorith ...

  2. BZOJ4491: 我也不知道题目名字是什么

    Description 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 Input 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一 ...

  3. 2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)

    传送门 题意:给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串. 思路: 注意要求的是子串而不是子序列!!! 然后直接用线段树维护最大子段和的方式合并一 ...

  4. 【BZOJ4991】我也不知道题目名字是什么(线段树)

    [BZOJ4991]我也不知道题目名字是什么(线段树) 题面 BZOJ 题解 对于线段树维护的区间维护以下东西: 区间左(右)端开始(结束)的最长(短)子串的长度 左端右端的值,以及当前区间内的答案 ...

  5. BZOJ 4491: 我也不知道题目名字是什么

    4491: 我也不知道题目名字是什么 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 278  Solved: 154[Submit][Status][ ...

  6. BZOJ 4491: 我也不知道题目名字是什么 RMQ

    4491: 我也不知道题目名字是什么 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 317  Solved: 174[Submit][Status][ ...

  7. bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并)

    bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并) bzoj Luogu 给出一个字符串 $ S $ 及 $ q $ 次询问,每次询问一个字符串 $ T $ ...

  8. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  9. [NOI2018]你的名字(后缀自动机+线段树)

    题目描述 小A 被选为了ION2018 的出题人,他精心准备了一道质量十分高的题目,且已经把除了题目命名以外的工作都做好了. 由于ION 已经举办了很多届,所以在题目命名上也是有规定的,ION 命题手 ...

随机推荐

  1. sigsuspend

    1)头文件:#include <signal.h> 2)一个保护临界区代码的错误实例:(sigprocmask()和pause()实现) #include <unistd.h> ...

  2. 可用于jquery animate()方法的css属性

    * backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWidth * border ...

  3. 网络编程--System.Net

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. IIS部署网部常用问题汇总

    1.unrecognized attribute 'targetframework' A: 需要注册.net framework到iis.步骤如下: (1)'Start' -> 'CMD' (2 ...

  5. Jdk1.7 与 jdk1.8的区别,最新的特征有哪些(美团,360,京东面试题目)

    在jdk7的新特性方面主要有下面几方面的增强: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头. 所有整数int.short.long.byte都可以用二进制表示: byte aBy ...

  6. dpr dproj 扩展名区别,dprdproj

    这段时间用xe6,看了下目录下生成的一些文件,因为隐藏了扩展名,看到两个名字一样的文件,右键属性看了下,同名但扩展名不同,百度了下区别,没有找到答案,问群里的朋友才知道区别,特此记录下来: dpr:D ...

  7. SQL SERVER 存储过程中SELECT 返回值如何赋值给变量

    今天在处理一个问题时,使用到一个存储过程,是用于更新并获取最新ID的.在使用过程中,需要获取到这个ID并赋值给变量,结果用EXEC @ID = 存储过程的方式获取失败了.具体情况如下: 为了还原整个情 ...

  8. BZOJ4864 BeiJing 2017 Wc神秘物质(splay)

    splay维护区间最大值.最小值.相邻两数差的绝对值的最小值即可. #include<iostream> #include<cstdio> #include<cmath& ...

  9. CF961D Pair Of Lines

    题目描述 You are given n n n points on Cartesian plane. Every point is a lattice point (i. e. both of it ...

  10. Ubuntu 10.04 下载android 4.1.1_r4

    一.安装 curl git $ sudo apt-get install  curl $ sudo apt-get install git-core 二.安装repo 1.在主目录(~)建立目录 bi ...