BZOJ 3339: Rmq Problem
Time Limit: 20 Sec
Memory Limit: 128 MB
Submit: 1004
Solved: 507
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=200005;
const int inf=1e9;
struct segmenttree{int l,r,mn,lazy;}T[N*4];
struct qur{int l,r,num;} Q[N];
int n,m,a[N],nxt[N],lj[N],SG[N],ans[N];
bool b[N];
bool cmp(qur u,qur v)
{
if(u.l==v.l) return u.r<v.r;
return u.l<v.l;
}
void Pushdown(int x)
{
int lc=x<<1,rc=x<<1|1;
T[lc].mn=min(T[x].lazy,T[lc].mn);
T[rc].mn=min(T[x].lazy,T[rc].mn);
T[lc].lazy=min(T[lc].lazy,T[x].lazy);
T[rc].lazy=min(T[rc].lazy,T[x].lazy);
T[x].lazy=inf;
}
void Pushup(int x)
{
T[x].mn=min(T[x<<1].mn,T[x<<1|1].mn);
}
void Build(int x,int l,int r)
{
T[x].l=l,T[x].r=r,T[x].lazy=inf;
if(l==r)
{
T[x].mn=SG[l];
return;
}
int mid=(l+r)>>1;
Build(x<<1,l,mid);
Build(x<<1|1,mid+1,r);
Pushup(x);
}
void Modify(int x,int l,int r,int v)
{
if(T[x].l==l&&T[x].r==r)
{
T[x].mn=min(v,T[x].mn);
T[x].lazy=min(v,T[x].lazy);
return;
}
if(T[x].lazy!=inf) Pushdown(x);
int mid=(T[x].l+T[x].r)>>1;
if(r<=mid) Modify(x<<1,l,r,v);
else if(l>mid) Modify(x<<1|1,mid+1,r,v);
else Modify(x<<1,l,mid,v),Modify(x<<1|1,mid+1,r,v);
Pushup(x);
}
int Query(int x,int k)
{
if(T[x].l==T[x].r) return T[x].mn;
if(T[x].lazy!=inf) Pushdown(x);
int mid=(T[x].l+T[x].r)>>1;
if(k<=mid) return Query(x<<1,k);
else return Query(x<<1|1,k);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=n;i>=1;i--) nxt[i]=lj[a[i]],lj[a[i]]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].num=i;
}
int now=0;
for(int i=1;i<=n;i++)
{
b[a[i]]=true;
while(b[now]==true) now++;
SG[i]=now;
}
Build(1,1,n);
sort(Q+1,Q+m+1,cmp);
Q[0].l=1;
for(int i=1;i<=m;i++)
{
for(int j=Q[i-1].l;j<Q[i].l;j++)
{
if(nxt[j]==0) nxt[j]=n+1;
Modify(1,j,nxt[j]-1,a[j]);
}
ans[Q[i].num]=Query(1,Q[i].r);
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
}
Description

Input

Output
![]()
Sample Input
7 5
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7
Sample Output
3
0
3
2
4
0
3
2
4
HINT

本文介绍了解决BZOJ3339:RmqProblem问题的方法,通过离线处理询问并使用线段树优化算法,实现对区间最小值的有效查询。文中详细解释了算法步骤,并提供了完整的C++代码实现。

499

被折叠的 条评论
为什么被折叠?



