【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 命题手 ...
随机推荐
- Internet Technologe
Store and Forward Networking Efficient Message Transmission:Packet Switching(分组交换) Challenge: in a s ...
- Java中的 toString 方法
1. Object 类中定义有 public String toString() 方法,其返回值是 String 类型,描述当前对象的有关信息: 2. 在进行 String 与其它类型数据的连接操作时 ...
- Uncaught ReferenceError: wx is not defined
程序的分享功能调用了微信的接口,但是忽然发现就报这个错误, Uncaught ReferenceError: wx is not defined 同时下方还有这个错误 This content sho ...
- python 爬虫 糗百成人
import urllib from time import sleep import requests from lxml import etree try: def all_links(url,p ...
- redis——持久化方式RDB与AOF分析
https://blog.csdn.net/u014229282/article/details/81121214 redis两种持久化的方式 RDB持久化可以在指定的时间间隔内生成数据集的时间点快照 ...
- RT-thread 设备驱动组件之SPI设备
本文主要介绍RT-thread中的SPI设备驱动,涉及到的文件主要有:驱动框架文件(spi_dev.c,spi_core.c,spi.h),底层硬件驱动文件(spi_hard.c,spi_hard.h ...
- Skills - CF613B
Lesha plays the recently published new version of the legendary game hacknet. In this version charac ...
- [luogu3806]【模板】点分治1
description 求树上长度为\(k\)的路径是否存在. data range \[n\le 10000,k\le 10000000\] solution 点分治复习... 使用普通的点分治枚举 ...
- BZOJ3894:文理分科——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3894 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理 ...
- BZOJ3781:小B的询问——题解
https://www.luogu.org/problemnew/show/2709 http://www.lydsy.com/JudgeOnline/problem.php?id=3781 题目描述 ...