zxjn @ 2024-08-06 17:14:01
P1253 扶苏的问题
#include<iostream>
#define bitset(x) x & (-x)
#define int long long
using namespace std;
int n, m, a[1000005];
struct node
{
int l, r, flag, sum, cover;
} tree[4000005];
void build(int i, int l, int r)
{
tree[i].flag = 1e18 + 5;
tree[i].l = l, tree[i].r = r;
if(l == r)
{
tree[i].sum = a[i];
return ;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
tree[i].sum = max(tree[i << 1].sum, tree[i << 1 | 1].sum);
return ;
}
void pushdown(int i)
{
if(tree[i].flag != 1e18 + 5)
{
tree[i << 1].flag = tree[i << 1 | 1].flag = tree[i].flag;
tree[i << 1].cover = tree[i << 1 | 1].cover = 0;
tree[i << 1].sum = tree[i << 1 | 1].sum = tree[i].flag;
tree[i].flag = 1e18 + 5;
}
if(tree[i].cover)
{
tree[i << 1].cover += tree[i].cover;
tree[i << 1 | 1].cover += tree[i].cover;
tree[i << 1].sum += tree[i].cover;
tree[i << 1 | 1].sum += tree[i].cover;
tree[i].cover = 0;
return ;
}
}
void add(int i, int l, int r, int k)
{
if(l <= tree[i].l && tree[i].r <= r)
{
tree[i].flag += k;
tree[i].sum += k;
return ;
}
int mid = tree[i].l + tree[i].r >> 1;
pushdown(i);
if(l <= mid) add(i << 1, l, r, k);
if(r >= mid + 1) add(i << 1 | 1, l, r, k);
tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
int find(int i, int l, int r)
{
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].sum;
}
pushdown(i);
int mid = tree[i].l + tree[i].r >> 1, ans = -1e18;
if(l <= mid) ans = max(find(i << 1, l, r), ans);
if(r >= mid + 1) ans = max(find(i << 1 | 1, l, r), ans);
return ans;
}
void change(int i, int l, int r, int x)
{
if(l <= tree[i].l && tree[i].r <= r)
{
tree[i].cover = x;
tree[i].sum = x;
tree[i].flag = 0;
return ;
}
int mid = tree[i].l + tree[i].r >> 1;
pushdown(i);
if(l <= mid)
{
change(i << 1, l, r, x);
}
if(mid + 1 <= r)
{
change(i << 1 | 1, l, r, x);
}
tree[i].sum = max(tree[i << 1].sum, tree[i << 1 | 1].sum);
}
signed main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
build(1, 1, n);
for(int i = 1; i <= m; i ++)
{
int op, x, y, k;
cin >> op >> x >> y;
if(op == 1)
{
cin >> k;
change(1, x, y, k);
}
if(op == 2)
{
cin >> k;
add(1, x, y, k);
}
if(op == 3)
{
cout << find(1, x, y) << endl;
}
}
return 0;
}
by suheming2010 @ 2024-08-06 17:44:08
add()数组中的求最大值的操作写错了,应用 max 函数
by suheming2010 @ 2024-08-06 17:51:18
而且build()函数中赋值的操作应改为tree[i].sum = a[l];