笛卡尔树--牛客第四场(sequence)

思路:
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)的更多相关文章
- 线性基求交(线段树)--牛客第四场(xor)
题意: 给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x . 思路: 建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了. #define ...
- 最短meeting路线(树的直径)--牛客第四场(meeting)
题意: 给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少. 思路: 其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1. ...
- 分层最短路(牛客第四场)-- free
题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...
- %300为0的个数(牛客第四场)-- number
题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ...
- 3的倍数 或运算构造x(牛客第四场)-- triples I
题意: 给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案. 思路: 进制%3有规律,1.2.4.8.16%3是1.2.1.2.1 ... 利用这一点分情况取一些位合成一些数就是答案了. # ...
- 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)
Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
- 19牛客暑期多校 round1 A 有关笛卡尔树的结论
题目传送门//res tp nowcoder 分析 定理:B1~B2当且仅当B1与B2有同构的笛卡尔树. (B₁~B₂ iff B₁ and B₂ have isomorphic Cartesian ...
- 平衡树及笛卡尔树讲解(旋转treap,非旋转treap,splay,替罪羊树及可持久化)
在刷了许多道平衡树的题之后,对平衡树有了较为深入的理解,在这里和大家分享一下,希望对大家学习平衡树能有帮助. 平衡树有好多种,比如treap,splay,红黑树,STL中的set.在这里只介绍几种常用 ...
随机推荐
- Centos 7自定义屏幕分辨率
$ xrandrScreen 0: minimum 1 x 1, current 1680 x 900, maximum 8192 x 8192Virtual1 connected primary 1 ...
- codeforces402B
Trees in a Row CodeForces - 402B The Queen of England has n trees growing in a row in her garden. At ...
- Partial Dependence Plot
Partial Dependence就是用来解释某个特征和目标值y的关系的,一般是通过画出Partial Dependence Plot(PDP)来体现. PDP是依赖于模型本身的,所以我们需要先训练 ...
- django xadmin安装
安装方式一: 下载xadmin源码文件,下载之后,解压缩,将解压目录中的xadmin文件夹拷贝到项目项目文件中.下载地址:https://codeload.github.com/sshwsfc/xad ...
- springboot 2.2.0 SNAPSHOT 解决 repositories.repository.id must be unique 的问题
如果打包 jar 也报错了 ,那也是这个的原因,注释掉即可 <!-- 这个仓库必须注释掉 否则打包 war 的时候 , 会报错 'repositories.repository.id' must ...
- Python file 读写与操作
# strip() 返回数据类型为字符串# strip()去除头和尾的指定字符:# 如果没有指定,默认去掉头和尾的空格 str_1 = " he llo " # he llopri ...
- 认识HTML语言(CodePen)
认识HTML语言 1.一个网页的加载过程 2.Web技术全览 3.HTML语法 HTML语法(一):标签 HTML语法(四):网页结构 4.HTML常用元素 展示元素 (1)块级元素div (2)内联 ...
- [学习笔记] L1-PCA
L1-PCA Intro PCA的本质就是从高维空间向低维空间投影,投影的本质又是左乘(或右乘)一个向量(表征原来特征空间到投影后特征空间的权重),经过线性加权,转换到低维空间表征,如果将向量换成矩阵 ...
- ArrayList && HashMap扩容策略
ArrayList扩容策略:默认10 扩容时是base + base/2, 即10 15 22 33 49...扩容时不安全:grow方法扩容时,赋值 elementData = Arrays.cop ...
- Struts ognl表达式语言几个符号
# 获取非根元素值 . 动态都建map集合 $ 配置文件取值 % 提供一个ognl表达式运行环境 <%@ page language="java" import=&q ...