# 线段树，区间最值

CodeForces 91B Queue (线段树，区间最值)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<unordered_map>
using namespace std;
const int maxn = 1e5+6;
struct node
{
int v;
int l,r;
}edge[4*maxn];
int a[maxn];
unordered_map<int,int> vis;
void build(int next,int x,int y)
{
edge[next].l=x;
edge[next].r=y;
if(x==y)
{
edge[next].v=a[x];
}
else
{
int mid=(x+y)>>1;
int next_left=(next<<1)+1;
int next_rigth=(next<<1)+2;
build(next_left,x,mid);
build(next_rigth,mid+1,y);
edge[next].v=min(edge[next_left].v,edge[next_rigth].v);
}
}
int query(int next,int d)
{
if(edge[next].l==edge[next].r) return edge[next].l;
int mid=(edge[next].l+edge[next].r)>>1;
int next_left=(next<<1)+1;
int next_rigth=(next<<1)+2;
if(edge[next_rigth].v<d) return query(next_rigth,d);  //从右边开始查找
if(edge[next_left].v<d) return query(next_left,d);
}
int main()
{
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
build(0,0,n-1);
int h;
for(int i=0;i<n;i++)
{
if(i)
printf(" ");
if(edge[0].v<a[i])
h=query(0,a[i])-i-1;
else
h=-1;
if(h<0)
h=-1;
printf("%d",h);
}
return 0;
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

08-09 6004
09-26 3534

06-10 894
08-20 104
09-18 414
03-30 260
05-11 379
04-18 100
09-14 219
10-11 545
11-05 305
06-15 322
06-05 777