跳到正文

算法·理论

KTT 模板

Aaron_Romeo约 18 分钟
原文
文章信息与版本

文章信息

收录
2026年8月12日
更新
2026年8月13日
篇幅
约 18 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <algorithm>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <vector>
#include <bitset>
#include <random>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
typedef __int128 i128;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int uint;
bool ST;
namespace IO
{
#ifdef LOCAL
	const int N = 1;
#else
	const int N = 1 << 23;
#endif
	template<typename T> struct flt {T num; int pcs; flt() {num = 0, pcs = 6;} flt(const T &_num, const int &_pcs) {num = _num, pcs = _pcs;}};
	struct I {FILE *in; char ibuf[N], *p1, *p2; I() {*this = stdin;} I(FILE *f) {*this = f;} I & operator = (FILE *f) {return p1 = p2 = ibuf, in = f, *this;} bool flush() {return p1 != p2 || (p2 = (p1 = ibuf) + fread(ibuf, 1, N, in), p1 != p2);} char gc() {return !flush() ? EOF : *p1 ++ ;} int getline(std :: string &s) {s.clear(); char c; while (c = gc(), c != 10 && c != EOF) if (c != 13) s += c; return s.length();} int getline(char *s) {char *t = s; char c; while (c = gc(), c != 10 && c != EOF) if (c != 13) *t ++ = c; *t = 0; return t - s;} int getdigit() {char c; while (!isdigit(c = gc()) && c != EOF); return c == EOF ? 0 : c - '0';} template<typename T> T readnum() {bool f = 0; T x = 0; char c; while (!isdigit(c = gc()) && c != EOF) f = c == '-'; while (isdigit(c)) x = x * 10 + c - '0', c = gc(); if (c == '.') for (T v = 0.1; isdigit(c = gc()); v *= 0.1) x += v * (c - '0'); return f ? -x : x;} template<typename T, typename = std :: enable_if_t<std :: is_arithmetic<T> :: value || std :: is_same<T, __int128> :: value || std :: is_same<T, unsigned __int128> :: value || std :: is_same<T, bool> :: value>> operator T() {return readnum<T>();} operator char() {char c = gc(); return c == EOF ? 0 : c;} operator std :: string() {std :: string s; char c; while (c = gc(), c == ' ' || c == 10 || c == 13 || c == 9); while (c != ' ' && c != 10 && c != 9 && c != EOF) {if (c != 13) s += c; c = gc();} return s;} I & operator () () {return *this;} template<typename T> int operator () (T &x) {return x = operator T(), 0;} int operator () (char *s) {char *t = s, c; while (c = gc(), c == ' ' || c == 10 || c == 13 || c == 9); while (c != ' ' && c != 10 && c != 9 && c != EOF) {if (c != 13) *t ++ = c; c = gc();} *t = 0; return t - s;} template<typename T, typename ...Args> typename std :: enable_if<std :: is_pointer<typename std :: remove_reference<T> :: type> :: value || std::is_array<T> :: value, int> :: type operator () (T x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);} template<typename T, typename ...Args> typename std :: enable_if<!std :: is_pointer<typename std :: remove_reference<T> :: type> :: value && !std::is_array<T> :: value, int> :: type operator () (T &x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);}};
	struct O {FILE *out; char obuf[N], *p3; O() {p3 = obuf, out = stdout;} O(FILE *f) {p3 = obuf, out = f;} ~O() {flush();} O & operator = (FILE *f) {return flush(), out = f, *this;} void flush() {fwrite(obuf, p3 - obuf, 1, out), p3 = obuf;} int close() {return flush(), fclose(out);} void pc(char x) {*p3 ++ = x; if (p3 - obuf == N) flush();} template<typename ...Args> int printf(const char *format, Args ...args) {char s[1 << 23], *t = s; sprintf(s, format, args...); while (*t) pc(*t ++ ); return t - s;} template<typename T> void writeint(const T &x) {T y = x; if (!y) return pc('0'); if (y < 0) pc('-'); char c[1 << 6]; int len = 0; if (y < 0) while (y) c[ ++ len] = -(y % 10) + '0', y /= 10; else while (y) c[ ++ len] = y % 10 + '0', y /= 10; while (len) pc(c[len -- ]);} template<typename T> void operator () (const T &x) {writeint<T>(x);} void operator () (const char &x) {pc(x);} void operator () (const std :: string &s) {for (char i : s) pc(i);} void operator () (char *s) {while (*s) pc(*s ++ );} void operator () (const char *s) {while (*s) pc(*s ++ );} template<typename T> void writefloat(const flt<T> &x) {T y = x.num; int r = x.pcs; if (y < 0) pc('-'), y = -y; T f = 5; for (int i = 1; i <= r + 1; i ++ ) f *= 0.1; y += f; char c[1 << 8]; int len = 0; if (y < 1) pc('0'); for (T s = std :: floor(y), t; s >= 1; s = t) t = std :: floor(s * 0.1), c[ ++ len] = std :: max((int)(s - t * 10), 0) + '0'; while (len) pc(c[len -- ]); if (!r) return ; pc('.'); for (T s = (y - std :: floor(y)) * 10; r; s = (s - std :: floor(s)) * 10, r -- ) pc((int)s + '0');} template<typename T> void operator () (const flt<T> &x) {writefloat<T>(x);} void operator () (const float &x) {writefloat(flt<float>(x, 6));} void operator () (const double &x) {writefloat(flt<double>(x, 6));} void operator () (const long double &x) {writefloat(flt<long double>(x, 6));} template<typename T, typename ...Args> void operator () (const T &x, const Args &...args) {operator () (x), operator () (args...);}};
} IO :: I read; IO :: O write, debug = stderr; using IO :: flt;
namespace solve
{
	const int N = 4e5;
	const ll INF = 4e18;
	int n, q;
	ll a[N + 5];
	struct func
	{
		ll k, b;
		func operator + (func t) {return {k + t.k, b + t.b};}
		func & operator += (ll t) {b += t * k; return *this;}
		bool operator < (const func t) const {return b != t.b ? b < t.b : k < t.k;}
		bool operator > (const func t) const {return b != t.b ? b > t.b : k > t.k;}
		bool operator == (func t) {return k == t.k && b == t.b;}
	};
	inline ll get(func a, func b)
	{
		if (b > a) std :: swap(a, b);
		if (a.k >= b.k) return INF;
		return (a.b - b.b) / (b.k - a.k);
	}
	struct segment_tree
	{
		#define ls (u << 1)
		#define rs (u << 1 | 1)
		struct data
		{
			func lmax, rmax, tot, sum;
			ll lim;
			data operator + (data t)
			{
				func jb;
				data res;
				res.lim = std :: min(lim, t.lim);
				jb = t.lmax + sum;
				res.lmax = std :: max(lmax, jb);
				res.lim = std :: min(res.lim, get(lmax, jb));
				jb = rmax + t.sum;
				res.rmax = std :: max(t.rmax, jb);
				res.lim = std :: min(res.lim, get(t.rmax, jb));
				jb = rmax + t.lmax;
				res.tot = std :: max(std :: max(tot, t.tot), jb);
				ll a = get(tot, t.tot), b = get(t.tot, jb), c = get(tot, jb);
				if (res.tot == tot) res.lim = std :: min(res.lim, std :: min(c, a));
				if (res.tot == t.tot) res.lim = std :: min(res.lim, std :: min(a, b));
				if (res.tot == jb) res.lim = std :: min(res.lim, std :: min(b, c));
				res.sum = sum + t.sum;
				return res;
			}
			data & operator += (ll t)
			{
				lmax += t, rmax += t, tot += t, sum += t, lim -= t;
				return *this;
			}
		} ktt[4 * N + 5];
		ll lazy[4 * N + 5];
		void push_up(int u) {ktt[u] = ktt[ls] + ktt[rs];}
		void update(int u, ll x) {ktt[u] += x, lazy[u] += x;}
		void push_down(int u)
		{
			if (!lazy[u]) return;
			update(ls, lazy[u]), update(rs, lazy[u]), lazy[u] = 0;
		}
		void build(int u, int l, int r)
		{
			lazy[u] = 0;
			if (l == r) return ktt[u].lmax = ktt[u].rmax = ktt[u].tot = ktt[u].sum = {1, a[l]}, ktt[u].lim = INF, void();
			int mid = l + r >> 1;
			build(ls, l, mid);
			build(rs, mid + 1, r);
			push_up(u);
		}
		void modify(int u, int l, int r, int L, int R, ll x)
		{
			if (L <= l && r <= R && x <= ktt[u].lim) return update(u, x);
			push_down(u);
			int mid = l + r >> 1;
			if (L <= mid) modify(ls, l, mid, L, R, x);
			if (mid < R) modify(rs, mid + 1, r, L, R, x);
			push_up(u);
		}
		data query(int u, int l, int r, int L, int R)
		{
			if (L <= l && r <= R) return ktt[u];
			push_down(u);
			int mid = l + r >> 1;
			if (L <= mid && mid < R) return query(ls, l, mid, L, R) + query(rs, mid + 1, r, L, R);
			if (L <= mid) return query(ls, l, mid, L, R);
			if (mid < R) return query(rs, mid + 1, r, L, R);
		}
		void build() {build(1, 1, n);}
		void modify(int L, int R, ll x) {modify(1, 1, n, L, R, x);}
		ll query(int L, int R) {return query(1, 1, n, L, R).tot.b;}
	} tr;
	int main()
	{
		n = read(), q = read();
		for (int i = 1; i <= n; i ++ ) a[i] = read();
		tr.build();
		while (q -- )
		{
			int op = read();
			if (op == 1)
			{
				int l = read(), r = read(); ll x = read();
				tr.modify(l, r, x);
			}
			if (op == 2)
			{
				int l = read(), r = read();
				write(std :: max(tr.query(l, r), 0ll), "\n");
			}
		}
		return 0;
	}
}
bool ED;
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	// freopen("f:/study/Informatics/code/in.txt", "r", stdin);
	// freopen("f:/study/Informatics/code/out.txt", "w", stdout);
#elif defined ONLINE_JUDGE
	// freopen("name.in", "r", stdin);
	// freopen("name.out", "w", stdout);
#else
	// freopen("name.in", "r", stdin);
	// freopen("name.out", "w", stdout);
#endif
#ifdef LOCAL
	debug("Start\n\n");
#endif
	int T = 1;
	while (T -- ) solve :: main();
#ifdef LOCAL
	debug("\n\n", flt<double>(std :: clock() / 1000.0, 2), "s ", flt<double>(std :: abs(&ED - &ST) / 1024.0 / 1024.0, 1), "MB\n");
#endif
	return 0;
}
原文