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

时间:2023-03-09 22:15:53
笛卡尔树--牛客第四场(sequence)

笛卡尔树--牛客第四场(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;
}