社区讨论
0分,求调
P1816忠诚参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mm97jcsu
- 此快照首次捕获于
- 2026/03/02 21:21 6 天前
- 此快照最后确认于
- 2026/03/05 21:30 3 天前
CPP
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N = 5e5+5;
#define PII pair<int,int>
#define f first
#define s second
ll a[N],n,m;
struct node{
ll sum,L,R,lazy;
}st[N<<2];
void pushup(ll root){//更新父节点值
st[root].sum=min(st[root<<1].sum,st[root<<1|1].sum);
}
void pushdown(ll root){//下传标记
if(!st[root].lazy) return ;//没有需要下传的lazy
node& ls=st[root<<1];
node& rs=st[root<<1|1];
ls.sum=min(ls.sum,st[root].lazy*(ls.R-ls.L+1));
ls.lazy=min(ls.lazy,st[root].lazy);
rs.sum=min(rs.sum,st[root].lazy*(rs.R-rs.L+1));
rs.lazy=min(rs.lazy,st[root].lazy);
st[root].lazy=0;
}
void build(ll root,ll L,ll R){//建树
st[root].L=L,st[root].R=R;
if(L==R){
st[root].sum=a[L];//a[L]是一个数值
return ;
}
ll mid=L+R>>1;
build(root<<1,L,mid);
build(root<<1|1,mid+1,R);
pushup(root);
}
ll query(ll root,ll L,ll R,ll l,ll r){//查询[l,r]区间和
if(l<=L&&R<=r) return st[root].sum;//[L,R]被完全包含在内
pushdown(root);//先下传标记
ll mid=L+R>>1,sum=0;
if(l<=mid) sum=min(sum,query(root<<1,L,mid,l,r));//左侧有部分
if(r>mid) sum=min(sum,query(root<<1|1,mid+1,R,l,r));//右侧有部分
return sum;
}
int 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);//根节点下标是1,代表的区间是[1,n]
while(m--){
int a,b;
cin>>a>>b;
cout<<query(1,1,n,a,b)<<" ";
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...