线段树求调
查看原帖
线段树求调
587248
wcyQwQ楼主2022/10/3 21:01

RT,考虑了最大值与最小值,维护了一堆东西,55pts求调

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 2e5 + 10, INF = 1 << 30;
int a[N];
struct node
{
	int l, r;
	ll lmax, rmax, tmax, lmin, rmin, mul;
	bool flg;
} t[N << 2];
bool has_ans;

inline int read()
{
	int x = 0, y = 1; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * y;
}

inline void pushup(int p)
{
	node &u = t[p], &l = t[p << 1], &r = t[p << 1 | 1];
    u.mul = l.mul * r.mul;
	u.lmax = max(l.mul * r.lmax, max(l.mul * r.lmin, l.lmax));
	u.rmax = max(r.mul * l.rmax, max(r.mul * l.rmin, r.rmax));
	u.lmin = min(l.mul * r.lmax, min(l.mul * r.lmin, l.lmin));
	u.rmin = min(r.mul * l.rmax, min(r.mul * l.rmin, r.rmin));
	u.tmax = max(l.tmax, max(r.tmax, max(l.rmax * r.lmax, l.rmin * r.lmin)));
    if (u.tmax > INF || l.flg || r.flg) u.flg = 1;
    else u.flg = 0;
}

inline void build(int p, int l, int r)
{
	t[p].l = l, t[p].r = r;
	if (l == r)
	{
		t[p].mul = t[p].lmax = t[p].rmax = t[p].lmin = t[p].rmin = t[p].tmax = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(p << 1, l, mid);
	build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

inline void modify(int p, int x, int k)
{
	if (t[p].l == t[p].r)
	{
		t[p].mul = t[p].lmax = t[p].rmax = t[p].lmin = t[p].rmin = t[p].tmax = k;
		return;
	}
	int mid = (t[p].l + t[p].r) >> 1;
	if (x <= mid) modify(p << 1, x, k);
	else modify(p << 1 | 1, x, k);
	pushup(p);
}

inline node merge(node l, node r)
{
	node u;
	u.l = l.l, u.r = r.r;
	u.mul = l.mul * r.mul;
	u.lmax = max(l.mul * r.lmax, max(l.mul * r.lmin, l.lmax));
	u.rmax = max(r.mul * l.rmax, max(r.mul * l.rmin, r.rmax));
	u.lmin = min(l.mul * r.lmax, min(l.mul * r.lmin, l.lmin));
	u.rmin = min(r.mul * l.rmax, min(r.mul * l.rmin, r.rmin));
	u.tmax = max(l.tmax, max(r.tmax, max(l.rmax * r.lmax, l.rmin * r.lmin)));
	if (u.tmax > INF || l.flg || r.flg) u.flg = 1, has_ans = 0;
	else u.flg = 0;
	return u;
}

inline node query(int p, int l, int r)
{
	if (l <= t[p].l && t[p].r <= r)
	{
		if (t[p].flg || t[p].tmax > INF) has_ans = 0;
		return t[p];
	}
	int mid = (t[p].l + t[p].r) >> 1;
    node res;
	if (l > mid) res = query(p << 1 | 1, l, r);
	else if (r <= mid) res = query(p << 1, l, r);
	else res = merge(query(p << 1, l, r), query(p << 1 | 1, l, r));
    if (res.flg || res.tmax > INF) has_ans = 0;
    return res;
}

int main()
{
	int n = read(), m = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	build(1, 1, n);
	while (m--)
	{
		int op = read(), l = read(), r = read();
		if (op == 1) modify(1, l, r);
		else if (op == 2)
		{
			has_ans = 1;
			node res = query(1, l, r);
			if (res.flg || !has_ans) puts("Too large");
			else printf("%lld\n", max(res.tmax, (ll)1));
		}
	}
	return 0;
}
2022/10/3 21:01
加载中...