CF91 B. Queue
阿新 • • 發佈:2021-07-01
題目傳送門:https://codeforces.com/problemset/problem/91/B
題目大意:
給一個長度為\(n\)的序列\(A\),記 \(B_i=j-i,j=\max\{j|i<j\and A_j<A_i\}\),若不存在這樣的 \(j\),則記 \(B_i=-1\),求\(B\)的值
考慮用樹狀陣列維護,倒序查詢可以保證 \(i<j\) 這一限制條件,以權值作為下標,位置作為權值,便可以求出 \(A_j<A_i\) 中最大的 \(j\)
/*program from Wolfycz*/ #include<map> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define Fi first #define Se second #define ll_inf 1e18 #define MK make_pair #define sqr(x) ((x)*(x)) #define pii pair<int,int> #define int_inf 0x7f7f7f7f #define lowbit(x) ((x)&(-x)) using namespace std; typedef long long ll; typedef unsigned int ui; typedef unsigned long long ull; inline char gc(){ static char buf[1000000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++; } template<typename T>inline T frd(T x){ int f=1; char ch=gc(); for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1; for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0'; return x*f; } template<typename T>inline T read(T x){ int f=1; char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1; for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0'; return x*f; } inline void print(int x){ if (x<0) putchar('-'),x=-x; if (x>9) print(x/10); putchar(x%10+'0'); } const int N=1e5; int A[N+10],Ans[N+10],list[N+10],Tree[N+10],n; void Add(int x,int v){for (;x<=n;x+=lowbit(x)) Tree[x]=max(Tree[x],v);} int Query(int x){ int res=0; for (;x;x-=lowbit(x)) res=max(res,Tree[x]); return res; } int main(){ n=read(0); for (int i=1;i<=n;i++) A[i]=list[i]=read(0); sort(list+1,list+1+n); int T=unique(list+1,list+1+n)-list-1; for (int i=1;i<=n;i++) A[i]=lower_bound(list+1,list+1+T,A[i])-list; for (int i=n;i>=1;i--){ Ans[i]=Query(A[i]-1); Ans[i]=!Ans[i]?-1:Ans[i]-i-1; Add(A[i],i); } for (int i=1;i<=n;i++) printf("%d%c",Ans[i],i==n?'\n':' '); return 0; }