【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 命题手 ...
随机推荐
- java报错:Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ss ...
- 无法打开mfc120.lib
今天在用VS2013编译OSG的时候报错:无法打开mfc120.lib.仔细查了下资料. 我是这么解决的: 安装Multibyte MFC Library for Visual Studio 2013 ...
- node中的__dirname
先说结论:__dirname指的是当前文件所在文件夹的绝对路径. 测试路径如下: 即 根目录/dir0.js 根目录/path1/dir1.js 根目录/paht1/path2/dir2.js 每个d ...
- windows下apache+php安装
1.安装apache 通过exe安装,如果80端口被占用,修改httpd.conf中的Listen,然后再次用exe安装,选择repaire 2.安装php 解压php包,添加系统变量 path,加上 ...
- JAVA学习之泛型
ArrayList<E>类定义和ArrayList<Integer>类引用中涉及的术语:1.整个ArrayList<E>称为泛型类型 2.ArrayList< ...
- mysql学习之主从复制
该文使用mysql5.5 centos6.5 64位 一.主从复制的作用 1.如果主服务器出现问题,可以快速切换到从服务器. 2.对与实时性要求不高或者更新不频繁的应用可以在从服务器上执行查询操作,降 ...
- BZOJ 1406 密码箱(数论)
很简洁的题目.求出x^2%n=1的所有x<=n的值. n<=2e9. 直接枚举x一定是超时的. 看看能不能化成有性质的式子. 有 (x+1)(x-1)%n==0,设n=a*b,那么一定有x ...
- 【bzoj3754】Tree之最小方差树 最小生成树
题目描述 给出一张无向图,求它的一棵生成树,使得选出的所有边的方差最小.输出这个最小方差. 输入 第一行两个正整数N,M 接下来M行,每行三个正整数Ui,Vi,Ci N<=100,M<=2 ...
- python函数入门到高级
函数的定义: def test(x): "The function definitions" x+=1 return x def:定义函数的关键字 test:函数名 ():内可定义 ...
- Dwango Programming Contest V 翻车记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...