MnZn刚学OI,线段树 3 拼尽全力无法战胜
查看原帖
MnZn刚学OI,线段树 3 拼尽全力无法战胜
1295097
LittleFoxFairy楼主2025/1/3 10:04

WA*3 ,AC on #1,#5,#6。

#include<bits/stdc++.h>
#define int long long 
#define max(a,b) Max(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=5e5+10;
const int INF=INT_MAX;

int n,m,e[N];
inline int Max(const int &a,const int &b) {return a>b?a:b;}
namespace SGT {
	struct Segment_Tree {
		int l,r,maxx,his,sum;
		int add,have,cmax,cnt;
		int hadd,cover;
#define ls u<<1
#define rs u<<1|1
	}t[N<<2];
	inline void Add(int u,int pls,int hpls) {
		t[u].hadd=max(t[u].hadd,t[u].add+hpls);
		t[u].his=max(t[u].his,t[u].maxx+hpls);
		t[u].sum+=(t[u].r-t[u].l+1)*pls;
		t[u].add+=pls,t[u].maxx+=pls,t[u].cmax+=pls;
		return ;
	}
	inline void push_min(int u,int tag) {
		if (t[u].maxx<=tag) return ;
		t[u].sum+=(tag-t[u].maxx)*t[u].cnt;
		t[u].maxx=t[u].cover=tag,t[u].have=1;
		return ;
	}
	inline void push_up(int u) {
		t[u].sum=t[ls].sum+t[rs].sum;
		t[u].his=max(t[ls].his,t[rs].his);
		if (t[ls].maxx==t[rs].maxx) {
			t[u].maxx=t[ls].maxx;
			t[u].cnt=t[ls].cnt+t[rs].cnt;
			t[u].cmax=max(t[ls].cmax,t[rs].cmax);
		}
		if (t[ls].maxx>t[rs].maxx) {
			t[u].maxx=t[ls].maxx;
			t[u].cnt=t[ls].cnt;
			t[u].cmax=max(t[ls].cmax,t[rs].maxx);
		}
		if (t[rs].maxx>t[ls].maxx) {
			t[u].maxx=t[rs].maxx;
			t[u].cnt=t[rs].cnt;
			t[u].cmax=max(t[ls].maxx,t[rs].cmax);
		}
		return ;
	}
	inline void push_down(int u) {
		if (t[u].have) {
			push_min(ls,t[u].cover);
			push_min(rs,t[u].cover);
			t[u].cover=t[u].have=0;
		}
		Add(ls,t[u].add,t[u].hadd);
		Add(rs,t[u].add,t[u].hadd);
		t[u].add=t[u].hadd=0;
		return ;
	}
	inline void build(int u,int l,int r) {
		t[u].l=l,t[u].r=r,t[u].maxx=t[u].his=t[u].cmax=-INF;
		if (l==r) {
			t[u].maxx=t[u].his=t[u].sum=e[l];
			t[u].cnt=1,t[u].cmax=-INF;
			return ;
		}
		int mid=l+r>>1;
		build(ls,l,mid),build(rs,mid+1,r);
		return push_up(u);
	}
	inline void modify(int u,int l,int r,int val) {
		if (t[u].maxx<=val) return ;
		if (t[u].l>=l and t[u].r<=r and t[u].cmax<=val) {
			push_min(u,val);
			return ;
		}
		push_down(u);
		int mid=t[u].l+t[u].r>>1;
		if (l<=mid) modify(ls,l,r,val);
		if (r>mid) modify(rs,l,r,val);
		return push_up(u);
	}
	inline void update(int u,int l,int r,int val) {
		if (t[u].l>=l and t[u].r<=r) {
			Add(u,val,val);
			return ;
		}
		push_down(u);
		int mid=t[u].l+t[u].r>>1;
		if (l<=mid) update(ls,l,r,val);
		if (r>mid) update(rs,l,r,val);
		return push_up(u);
	}
	inline int querysum(int u,int l,int r) {
		if (t[u].l>=l and t[u].r<=r) return t[u].sum;
		push_down(u);
		int mid=t[u].l+t[u].r>>1,res=0;
		if (l<=mid) res+=querysum(ls,l,r);
		if (r>mid) res+=querysum(rs,l,r);
		return res;
	}
	inline int querymax(int u,int l,int r) {
		if (t[u].l>=l and t[u].r<=r) return t[u].maxx;
		push_down(u);
		int mid=t[u].l+t[u].r>>1,res=-INF;
		if (l<=mid) res=max(res,querymax(ls,l,r));
		if (r>mid) res=max(res,querymax(rs,l,r));
		return res;
	}
	inline int queryhis(int u,int l,int r) {
		if (t[u].l>=l and t[u].r<=r) return t[u].his;
		push_down(u);
		int mid=t[u].l+t[u].r>>1,res=-INF;
		if (l<=mid) res=max(res,queryhis(ls,l,r));
		if (r>mid) res=max(res,queryhis(rs,l,r));
		return res;
	}
}using namespace SGT;
signed main() {
	cin>>n>>m;
	for (int i=1;i<=n;i++) cin>>e[i];
	build(1,1,n);
	while (m--) {
		int op,l,r,val;
		cin>>op>>l>>r;
		if (op==1) {
			cin>>val;
			update(1,l,r,val);
		}
		if (op==2) {
			cin>>val;
			modify(1,l,r,val);
		}
		if (op==3) {
			cout<<querysum(1,l,r)<<endl;
		}
		if (op==4) {
			cout<<querymax(1,l,r)<<endl;
		}
		if (op==5) {
			cout<<queryhis(1,l,r)<<endl;
		}
	}
	return (0-0);
}
2025/1/3 10:04
加载中...