传送门

题意:给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串。


思路:

注意要求的是子串而不是子序列!!!

然后直接用线段树维护最大子段和的方式合并一下就完了。

注意要维护当前区间最靠左/右的数是什么。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
const int N=50005;
int n,a[N];
inline int max(const int&a,const int&b){return a>b?a:b;}
inline int max(const int&a,const int&b,const int&c){return a>b?(a>c?a:c):(b>c?b:c);}
namespace SGT{
    #define lc (p<<1)
    #define rc (p<<1|1)
    #define mid (l+r>>1)
    struct Val{
        int L,R,len,ls[2],rs[2],ms[2];
        friend inline Val operator+(const Val&a,const Val&b){
            Val ret;
            ret.L=a.L,ret.R=b.R,ret.len=a.len+b.len;
            ret.rs[0]=b.rs[0]+(b.rs[0]==b.len&&a.R<=b.L?a.rs[0]:0);
            ret.rs[1]=b.rs[1]+(b.rs[1]==b.len&&a.R>=b.L?a.rs[1]:0);
            ret.ls[0]=a.ls[0]+(a.ls[0]==a.len&&a.R<=b.L?b.ls[0]:0);
            ret.ls[1]=a.ls[1]+(a.ls[1]==a.len&&a.R>=b.L?b.ls[1]:0);
            ret.ms[0]=max(a.ms[0],b.ms[0],a.R<=b.L?a.rs[0]+b.ls[0]:0);
            ret.ms[1]=max(a.ms[1],b.ms[1],a.R>=b.L?a.rs[1]+b.ls[1]:0);
            return ret;
        }
        inline void Set(const int&x){L=R=x,len=ls[0]=rs[0]=ms[0]=ls[1]=rs[1]=ms[1]=1;}
    };
    struct Node{int l,r;Val val;}T[N<<2];
    inline void pushup(int p){T[p].val=T[lc].val+T[rc].val;}
    inline void build(int p,int l,int r){
        T[p].l=l,T[p].r=r;
        if(l==r)return T[p].val.Set(a[l]);
        build(lc,l,mid),build(rc,mid+1,r),pushup(p);
    }
    inline Val query(int p,int l,int r,int ql,int qr){
        if(ql<=l&&r<=qr)return T[p].val;
        if(qr<=mid)return query(lc,l,mid,ql,qr);
        if(ql>mid)return query(rc,mid+1,r,ql,qr);
        return query(lc,l,mid,ql,mid)+query(rc,mid+1,r,mid+1,qr);
    }
    #undef lc
    #undef rc
    #undef mid
}
int main(){
    n=read();
    for(ri i=1;i<=n;++i)a[i]=read();
    SGT::build(1,1,n);
    SGT::Val tmp;
    for(ri tt=read(),l,r;tt;--tt){
        l=read(),r=read(),tmp=SGT::query(1,1,n,l,r);
        cout<<max(tmp.ms[0],tmp.ms[1])<<'\n';
    }
    return 0;
}

2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)的更多相关文章

  1. 【BZOJ4491】我也不知道题目名字是什么 [线段树]

    我也不知道题目名字是什么 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 给定一个序列A[i ...

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

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

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

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

  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. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

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

  8. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  9. Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)

    题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...

随机推荐

  1. [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(四) -- 安装crowd 3.1.2

    [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(四) -- 安装crowd 3.1.2 本来已经安装完毕, 并使用Jira集成的OAuth账户管理, ...

  2. 用spring的@Scheduled实现定时任务

    先在spring的配置文件中添加扫描 在applicationContext.xml中添加  <task:annotation-driven/>,我用的是idea有提示功能 选择第一个后会 ...

  3. EOS

    1.移植性特别差,例如用Eclipse,idea,等其他集成工具开发之后的项目,难以快速的搭建到EOS集成工具中(例如逻辑流,数据结构等)2.项目框架的局限性大,底层封装的框架或者组件迭代性较差,很难 ...

  4. Servlet第五篇(会话技术之Session)

    Session 什么是Session Session 是另一种记录浏览器状态的机制.不同的是Cookie保存在浏览器中,Session保存在服务器中.用户使用浏览器访问服务器的时候,服务器把用户的信息 ...

  5. TZOJ 2519 Regetni(N个点求三角形面积为整数总数)

    描述 Background Hello Earthling. We're from the planet Regetni and need your help to make lots of mone ...

  6. Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException

    Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. M ...

  7. How to decode input data from a contract transaction without ABI?

    1 I've found some libraries which decode input from transaction, but all of them require ABI of cont ...

  8. 使用Snappy将html或者url转成PDF文件

    这是一个操作简单的html文件或者url转PDF的php库 Github地址 https://github.com/KnpLabs/snappy 安装: $ composer require knpl ...

  9. JS 中常见数组API使用方法(join、concat、slice、splice、reverce)

    刚接触前端不久,个人觉得学习程序还是需要经常总结的.下面是我的一些知识的归纳总结,如果哪里说得不对的还请各位大神指点! 1.to str (1)String(arr)将数组中的每个元素转为字符串并用逗 ...

  10. svg绘制一个简单地饼图

    一个简单地svg绘制饼图的demo,代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...