社区讨论
50分求调
P1253扶苏的问题参与者 4已保存回复 7
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 7 条
- 当前快照
- 1 份
- 快照标识符
- @m3zqf9ma
- 此快照首次捕获于
- 2024/11/27 18:17 去年
- 此快照最后确认于
- 2025/11/04 13:48 4 个月前
CPP
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#define int long long
using namespace std;
struct node{
int val,l,r,add,ex;
}tr[8000005];
int n,q;
int op,l,r,x;
int a[8000005];
void pushdown(int p){
if(tr[p].ex > -1e18){
tr[p * 2].val = tr[p * 2 + 1].val = tr[p].ex;
tr[p * 2].ex = tr[p * 2 + 1].ex = tr[p].ex;
tr[p * 2].add = tr[p * 2 + 1].add = 0;
tr[p].ex = -1e18;
}
tr[p * 2].val += tr[p].add;
tr[p * 2 + 1].val += tr[p].add;
tr[p * 2].add += tr[p].add;
tr[p * 2 + 1].add += tr[p].add;
return ;
}
void build(int p,int l,int r){
tr[p].l = l,tr[p].r = r,tr[p].ex = -1e18;
if(tr[p].l == tr[p].r){
tr[p].val = a[l];
return ;
}
int mid = (tr[p].l + tr[p].r) / 2;
build(p * 2,tr[p].l,mid);
build(p * 2 + 1,mid + 1,tr[p].r);
tr[p].val = max(tr[p * 2].val,tr[p * 2 + 1].val);
return ;
}
void exchange(int p,int l,int r,int k){
if(tr[p].l >= l && tr[p].r <= r){
tr[p].val = k;
tr[p].ex = k;
tr[p].add = 0;
return ;
}
pushdown(p);
int mid = (tr[p].l + tr[p].r) / 2;
if(mid >= l) exchange(p * 2,l,r,k);
if(mid < r) exchange(p * 2 + 1,l,r,k);
tr[p].val = max(tr[p * 2].val,tr[p * 2 + 1].val);
return ;
}
void add(int p,int l,int r,int k){
if(tr[p].l >= l && tr[p].r <= r){
tr[p].val += k;
tr[p].add += k;
return ;
}
pushdown(p);
int mid = (tr[p].l + tr[p].r) / 2;
if(mid >= l) add(p * 2,l,r,k);
if(mid < r) add(p * 2 + 1,l,r,k);
tr[p].val = max(tr[p * 2].val,tr[p * 2 + 1].val);
return ;
}
int found(int p,int l,int r){
if(tr[p].l >= l && tr[p].r <= r) return tr[p].val;
pushdown(p);
int ans = 0,mid = (tr[p].l + tr[p].r) / 2;
if(mid >= l) ans = found(p * 2,l,r);
if(mid < r) ans = max(found(p * 2 + 1,l,r),ans);
return ans;
}
signed main(){
scanf("%lld%lld",&n,&q);
for(int i = 1;i <= n;i++) scanf("%lld",&a[i]);
build(1,1,n);
for(int i = 1;i <= q;i++){
scanf("%lld%lld%lld",&op,&l,&r);
if(op == 1){
scanf("%lld",&x);
exchange(1,l,r,x);
}
else if(op == 2){
scanf("%lld",&x);
add(1,l,r,x);
}
else printf("%lld\n",found(1,l,r));
}
return 0;
}
回复
共 7 条回复,欢迎继续交流。
正在加载回复...