才译救我
查看原帖
才译救我
1305692
xiangixuan楼主2025/1/18 17:08
#include<bits/stdc++.h>
#define int long long
#define N 1000005
using namespace std;
int n, q, a[N];
struct seg{
	int l, r, v, t1, t2;
	bool b;
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define v(x) t[x].v
	#define t1(x) t[x].t1
	#define t2(x) t[x].t2
	#define b(x) t[x].b
} t[4*N];
int mid(int x) {return l(x)+r(x)>>1; }
int ls(int x) {return x<<1; }
int rs(int x) {return (x<<1)+1; }
void build(int p, int l, int r) {
	l(p)=l, r(p)=r;
	if (l==r) {v(p)=a[l]; return; }
	build(ls(p), l, mid(p));
	build(rs(p), mid(p)+1, r);
	v(p)=max(v(ls(p)), v(rs(p)));
}
void spread(int p) {
	if(b(p)) {
		v(ls(p))=t2(p); v(rs(p))=t2(p);
		t2(ls(p))=t2(p); t2(rs(p))=t2(p);
	}
	v(ls(p))+=t1(p); v(rs(p))+=t1(p);
	t1(ls(p))+=t1(p); t1(rs(p))+=t1(p);
	t1(p)=0; t2(p)=0; b(p)=0;
}
void add(int p, int l, int r, int v) {
	if (l<=l(p) && r>=r(p)) {
		v(p)+=v; t1(p)+=v;
		return;
	}
	spread(p);
	if (l<=mid(p)) add(ls(p), l, r, v);
	if (r>mid(p)) add(rs(p), l, r, v);
	v(p)=max(v(ls(p)), v(rs(p)));
}
void cover(int p, int l, int r, int v) {
	if (l<=l(p) && r>=r(p)) {
		v(p)=v; t1(p)=0; t2(p)=v; b(p)=1;
		return;
	}
	spread(p);
	if (l<=mid(p)) cover(ls(p), l, r, v);
	if (r>mid(p)) cover(rs(p), l, r, v);
	v(p)=max(v(ls(p)), v(rs(p)));
}
int query(int p, int l, int r) {
	if (l<=l(p) && r>=r(p)) return v(p);
	spread(p);
	int v=LLONG_MIN;
	if (l<=mid(p)) v=max(v, query(ls(p), l, r));
	if (r>mid(p)) v=max(v, query(rs(p), l, r));
	return v;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> q;
	for (int i=1; i<=n; ++i) cin >> a[i];
	build(1, 1, n);
	int opt, x, y, z;
	while (q--) {
		cin >> opt;
		if (opt==1) {
			cin >> x >> y >> z;
			cover(1, x, y, z);
		} else if (opt==2) {
			cin >> x >> y >> z;
			add(1, x, y, z);
		} else {
			cin >> x >> y;
			cout << query(1, x, y) << '\n';
		}
	}
	return 0;
}
2025/1/18 17:08
加载中...