Y_QWQ_Y @ 2024-06-07 20:10:29
初学线段树,样例都过不了……
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N], tree[N << 2], tag[N << 2], ch[N << 2], op, l, r, d;
int ls (int x){return x << 1;}
int rs (int x){return x << 1 | 1;}
void change_tag (int x, int l, int r, int d)
{
ch[x] = d;
tree[x] = (r - l + 1) * d;
}
void add_tag (int x, int l, int r, int d)
{
if (ch[x])
{
change_tag (x, l, r, ch[x]);
tag[x] = 0;
}
tag[x] += d;
tree[x] += (r - l + 1) * d;
}
void push_up (int x){tree[x] = max (tree[ls (x)], tree[rs (x)]);}
void push_down (int x, int l, int r)
{
int mid = (l + r) >> 1;
if (tag[x])
{
add_tag (ls (x), l, mid, tag[x]);
add_tag (rs (x), mid + 1, r, tag[x]);
tag[x] = 0;
}
}
void build (int x, int l, int r)
{
if (l == r){tree[x] = a[l];return;}
int mid = (l + r) >> 1;
build (ls (x), l, mid);
build (rs (x), mid + 1, r);
push_up (x);
}
void update1 (int x, int l, int r, int d, int L, int R)
{
if (L <= l && r <= R)
{
add_tag (x, l, r, d);
return;
}
push_down (x, l, r);
int mid = (l + r) >> 1;
if (L <= mid)update1 (ls (x), l, mid, d, L, R);
if (mid < R)update1 (rs (x), mid + 1, r, d, L, R);
push_up (x);
}
void update2 (int x, int l, int r, int d, int L, int R)
{
if (L <= l && r <= R)
{
change_tag (x, l, r, d);
return;
}
push_down (x, l, r);
int mid = (l + r) >> 1;
if (l <= mid)update2 (ls (x), l, mid, d, L, R);
if (mid < R)update2 (rs (x), mid + 1, r, d, L, R);
}
int query (int x, int l, int r, int L, int R)
{
if (L <= l && r <= R)return tree[x];
push_down (x, l, r);
int mid = (l + r) >> 1, ans = -1e18;
if (L <= mid)ans = max (query (ls (x), l, mid, L, R), ans);
if (mid < R)ans = max (query (rs (x), mid + 1, r, L, R), ans);
return ans;
}
signed main ()
{
ios::sync_with_stdio (0), cin.tie (0), cout.tie (0);
cin >> n >> m;
for (int i = 1; i <= n; ++ i)cin >> a[i];
build (1, 1, n);
while (m --)
{
cin >> op >> l >> r;
if (op < 3)
{
cin >> d;
if (op == 1)update2 (1, 1, n, d, l, r);
else update1 (1, 1, n, d, l, r);
}
else cout << query (1, 1, n, l, r) << '\n';
}
return 0;
}
by Y_QWQ_Y @ 2024-06-07 20:13:34
add_tag 的 tag[x]=0
后面还有个 ch[x]=0
。