n log n 跑不过 1e6 求调
查看原帖
n log n 跑不过 1e6 求调
826774
Little_Fox_Fairy楼主2024/10/3 21:01

rt,写的线段树。

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long 
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=1e6+10;
const int INF=0x3f3f3f3f3f3f3f3f;
int n,m,e[N],f[N];
int ans,up[N],down[N];
struct Segement_Tree
{
#define ls u<<1
#define rs u<<1|1
	struct node {int l,r,maxx,minn;}t[N<<2];
	inline void push_up(int u) {return t[u].maxx=max(t[ls].maxx,t[rs].maxx),t[u].minn=min(t[ls].minn,t[rs].minn),void();}
	inline void build(int u,int l,int r) {t[u].l=l,t[u].r=r;if (l==r) return t[u].minn=t[u].maxx=e[l],void();int mid=l+r>>1;build(ls,l,mid),build(rs,mid+1,r);return push_up(u);}
	inline int query_max(int u,int l,int r) {if (t[u].l>=l and t[u].r<=r) return t[u].maxx;int mid=t[u].l+t[u].r>>1,res=-INF;if (l<=mid) res=max(res,query_max(ls,l,r));if (r>mid) res=max(res,query_max(rs,l,r));return res;}
	inline int query_min(int u,int l,int r) {if (t[u].l>=l and t[u].r<=r) return t[u].minn;int mid=t[u].l+t[u].r>>1,res=INF;if (l<=mid) res=min(res,query_min(ls,l,r));if (r>mid) res=min(res,query_min(rs,l,r));return res;}
}tr;
signed main()
{
	cin>>n;
	for (int i=1;i<=n;i++) cin>>e[i];
	tr.build(1,1,n);
	for (int i=1;i<=n;i++)
	{
		int S=i,T;
		while (i+1<=n)
		{
			if (e[i+1]<e[i]) break;
			i++;
		}
		T=i;
		for (int j=S;j<=T;j++) up[j]=S;
	}
	for (int i=1;i<=n;i++)
	{
		int S=i,T;
		while (i+1<=n)
		{
			if (e[i+1]>e[i]) break;
			i++;
		}
		T=i;
		for (int j=S;j<=T;j++) down[j]=S;
	}
	for (int i=1;i<=n;i++)
	{
		f[i]=max(f[i-1],max(f[up[i]-1]+tr.query_max(1,up[i],i)-tr.query_min(1,up[i],i),f[down[i]-1]+tr.query_max(1,down[i],i)-tr.query_min(1,down[i],i)));
	}
	cout<<f[n]<<endl;
	return (0-0);
}
2024/10/3 21:01
加载中...