思路:

O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<long long, vector<long long>, greater<long long> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
void swapp(long long &a,long long &b);
double fabss(double a);
long long maxx(long long a,long long b);
long long minn(long long a,long long b);
long long Del_bit_1(long long n);
long long lowbit(long long n);
long long abss(long long a);
const long long INF=(1LL<<);
const double E=2.718281828;
const double PI=acos(-1.0);
const long long inf=(<<);
const double ESP=1e-;
const long long mod=(long long)1e9+;
const long long N=(long long)3e6+; int son[N][];
long long min_[N],max_[N];
long long a[N];
long long b[N];
int stak[N];
long long ans=-INF; void dfs(int rt,int l,int r)
{
if(son[rt][])dfs(son[rt][],l,rt-);
if(son[rt][])dfs(son[rt][],rt+,r); long long minl=minn(b[l-],min_[son[rt][]]);
long long minr=minn(b[rt],min_[son[rt][]]);
long long maxl=maxx(b[l-],max_[son[rt][]]);
long long maxr=maxx(b[rt],max_[son[rt][]]); max_[rt]=maxx(maxl,maxr);
min_[rt]=minn(minl,minr); ans=maxx(ans,(maxr-minl)*a[rt]);
ans=maxx(ans,(minr-maxl)*a[rt]);
} int main()
{
int n;
sc("%d",&n);
fo(i,,n)sc("%lld",&a[i]);
fo(i,,n)sc("%lld",&b[i]),b[i]+=b[i-]; long long top=;
fo(i,,n)
{
while(top&&a[i]<a[stak[top]])
son[i][]=stak[top--];
if(top)
son[stak[top]][]=i;
stak[++top]=i;
}
min_[]=INF,max_[]=-INF;//注意细节;;...
dfs(stak[],,n);
pr("%lld\n",ans);
return ;
} /**************************************************************************************/ long long maxx(long long a,long long b)
{
return a>b?a:b;
} void swapp(long long &a,long long &b)
{
a^=b^=a^=b;
} long long lowbit(long long n)
{
return n&(-n);
} long long Del_bit_1(long long n)
{
return n&(n-);
} long long abss(long long a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} long long minn(long long a,long long b)
{
return a<b?a:b;
}

笛卡尔树--牛客第四场(sequence)的更多相关文章

  1. 线性基求交(线段树)--牛客第四场(xor)

    题意: 给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x . 思路: 建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了. #define ...

  2. 最短meeting路线(树的直径)--牛客第四场(meeting)

    题意: 给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少. 思路: 其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1. ...

  3. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  4. %300为0的个数(牛客第四场)-- number

    题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ...

  5. 3的倍数 或运算构造x(牛客第四场)-- triples I

    题意: 给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案. 思路: 进制%3有规律,1.2.4.8.16%3是1.2.1.2.1 ... 利用这一点分情况取一些位合成一些数就是答案了. # ...

  6. 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)

    Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...

  7. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

  8. 19牛客暑期多校 round1 A 有关笛卡尔树的结论

    题目传送门//res tp nowcoder 分析 定理:B1~B2当且仅当B1与B2有同构的笛卡尔树. (B₁~B₂ iff B₁ and B₂ have isomorphic Cartesian ...

  9. 平衡树及笛卡尔树讲解(旋转treap,非旋转treap,splay,替罪羊树及可持久化)

    在刷了许多道平衡树的题之后,对平衡树有了较为深入的理解,在这里和大家分享一下,希望对大家学习平衡树能有帮助. 平衡树有好多种,比如treap,splay,红黑树,STL中的set.在这里只介绍几种常用 ...

随机推荐

  1. Into Blocks (easy version)

    G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...

  2. VM中的Centos 7配置静态IP

    环境: CentOS Linux release 7.6.1810 (Core) VM 网卡配置 将VM的网卡配置成桥接模式 在cmd中查看本机的ip,子网掩码,网关,虚拟机中配置需要和本机在同一个网 ...

  3. HDU 5813 Elegant Construction ——(拓扑排序,构造)

    可以直接见这个博客:http://blog.csdn.net/black_miracle/article/details/52164974. 对其中的几点作一些解释: 1.这个方法我们对队列中取出的元 ...

  4. Pycharm添加python2解释器

    Anaconda3+python3环境下如何创建python2环境(win+Linux下适用,同一个anaconda下py2/3共存) conda info -e 查看python2的路径 Pycha ...

  5. [转]基于java的程序OutOfMemory问题的解决及Xms/Xmx/Xss的解释和应用

    长期以来一直都是做java应用的开发,所使用的开发工具基本上也都是基于java的,经常用的有eclipse, netbeans, ant, maven, cruisecontrol, tomcat, ...

  6. 极光推送报错time_to_live value should be a non-negative integertime_to_live value should be a non-negative integer

    文件中修改

  7. java8 lamda 表达式

  8. PHP中获取数组中单列的值

    PHP中获取数组中单列的值如下: 利用PHP中的数组函数 array_column():返回数组中某个单列的值.(PHP 5.5+适用) 语法: array_column(array,column_k ...

  9. vue路由在keep-alive下的刷新问题 对watch的影响

    转载自:https://www.cnblogs.com/dansingal/p/8302100.html 问题描述: 在keep-alive中的在跳转到指定的路由时刷新对应的路由,其余不刷新. 1 2 ...

  10. MySQL如何查询某个字段长度最大的记录

    转: MySQL如何查询某个字段长度最大的记录 2017年06月24日 13:12:15 翔云123456 阅读数 18348   版权声明:本文为翔云原创文章,未经博主允许不得转载. https:/ ...