CodeForces 91B Queue【线段树】
2016-10-20 20:41
946 查看
QueueTime Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d& %I64uSubmit Status Practice CodeForces91BDescriptionThere are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at theend of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j),that ai > aj. Thedispleasure of the i-thwalrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the strongerthe displeasure is.The airport manager asked you to count for each of n walruses in the queue his displeasure.InputThe first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. Thesecond line contains integers ai (1 ≤ ai ≤ 109).Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.OutputPrint n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-thwalrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.Sample InputInput
6 10 8 5 3 50 45Output
2 1 0 -1 0 -1Input
7 10 4 6 3 2 8 15Output
4 2 1 0 -1 -1 -1Input
5 10 3 1 10 11Output
1 0 -1 -1 -1
线段树维护区间最小值, 循环一遍,每次找到比这个数小且在最右端的数,将左边处理过的数可以更新成INF并更新最小值。那么查询之前, 先询问整个数组的最小是和当前值的比较,当前值大于最新最小值才有查询的必要,而且右边不成立就一定在左边。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<climits>#include<string>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>using namespace std;#define rep(i,j,k)for(i=j;i<k;i++)#define per(i,j,k)for(i=j;i>k;i--)#define MS(x,y)memset(x,y,sizeof(x))#define max(a,b) a>b?a:b#define min(a,b) a<b?a:b#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;const int INF=0x7ffffff;#define lson rt<<1, l, m#define rson rt<<1|1, m+1, rconst int maxn = 100005;const int MAX = 0x3f3f3f3f;int n,i,j,k,b;int cnt;int a[maxn],mi[maxn<<2],ans[maxn];void up(int rt){mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);}void build(int rt ,int l,int r){if(l==r){scanf("%d",&mi[rt]);a[l]=mi[rt];return;}int m=(l+r)>>1;build(lson);build(rson);up(rt);}void query(int rt,int l,int r){if(l==r){ans[k++]=l-b-1;return;}int m=(l+r)>>1;if(mi[rt<<1|1]<cnt)query(rson);else query(lson);}void update(int rt,int l,int r){if(l==r)mi[rt]=MAX;else {int m=(l+r)>>1;if(b<=m)update(lson);else update(rson);up(rt);}}int main(){scanf("%d",&n);build(1,1,n);for(int i=1;i<=n;i++){cnt=a[i],b=i;update(1,1,n);if(mi[1]>=cnt)ans[k++]=-1;else query(1,1,n);}for(i=0;i<k;i++)printf("%d ",ans[i]);return 0;}
相关文章推荐
- CodeForces 91B Queue 线段树 单点更新 成段查询
- codeforces 91B Queue 线段树,简单查询
- 解题报告 之 CodeForces 91B Queue
- CodeForces 173E Camping Groups 离线线段树 树状数组
- 【CODEFORCES】 B. Queue
- Codeforces 482B Interesting Array(线段树)
- Codeforces 609F Frogs and mosquitoes 二分+简单离散化线段树+Multimap
- [扫描线 线段树] Codeforces 720D Russian Code Cup 2016 - Finals D. Slalom
- Codeforces 165D Buses and People(线段树)
- Codeforces 449B Jzzhu and Cities (最短路/SPFA/priority_queue<int>Q)
- Codeforces 622C Not Equal on a Segment(线段树)
- CodeForces 356A_(set应用,线段树)
- 【CodeForces】343D Water tree (线段树好题!还未弄懂)
- codeforces 38G Queue
- Codeforces 666E Forensic Examination (后缀自动机+线段树合并)
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- HDU - 5493 Queue 线段树 || Treap
- codeforces 628E Zbazi in Zeydabad(线段树||树状数组优化)
- 线段树 CodeForces 61E 逆序对
- Codeforces 633H. Fibonacci-ish II (Mo's Algorithm(莫队算法) + 线段树 + 离散化)