【算法】线段树
#include<cstdio> #include<algorithm> using namespace std; struct treess{int l,r,ms;}t[400010]; const int maxn=200010,inf=0x3f3f3f3f; int a[maxn],n,m; void build(int k,int l,int r) { t[k].l=l;t[k].r=r; if(l==r){t[k].ms=a[l];return;} int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); t[k].ms=min(t[k<<1].ms,t[k<<1|1].ms); // printf("[%d] l=%d r=%d ms=%d",k,t[k].l,t[k].r,t[k].ms); } int ask(int k,int l,int r) { int left=t[k].l,right=t[k].r,ans=inf; if(l<=left&&r>=right)return t[k].ms; else { int mid=(left+right)>>1; if(l<=mid)ans=min(ans,ask(k<<1,l,r)); if(r>mid)ans=min(ans,ask(k<<1|1,l,r)); } return ans; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); build(1,1,n); int nowl,nowr; for(int i=1;i<m;i++) { scanf("%d%d",&nowl,&nowr); printf("%d ",ask(1,nowl,nowr)); } scanf("%d%d",&nowl,&nowr); printf("%d",ask(1,nowl,nowr)); return 0; }
时间: 2024-12-28 22:03:49