[关闭]
@Chilling 2017-02-16T09:57:42.000000Z 字数 2280 阅读 874

CodeForces-91B: Queue

线段树


链接:CodeForces-91B

Description

There 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 the end 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. The displeasure of the i-th walrus 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 stronger the displeasure is.

The airport manager asked you to count for each of n walruses in the queue his displeasure.

Input

The first line contains an integer — the number of walruses in the queue. The second line contains integers .

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.

Output

Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

Example

Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1 
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1 
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1 

题意:给出n个数字,每个数字下标为i,比它小的最右端的数字的下标为k,求k-i-1,若右边没有比它更小的数字,则为-1。

分析:对于每一个数字来说,先判断是否有比它小的数字,然后先找右边的区间,若没有再找左边的。每一个数字查询完之后,更新这个位置,让它变成无穷大,之后的查询就不会再找到左边的区间了!


  1. #include<algorithm>
  2. #include<stdio.h>
  3. const int INF=0x3f3f3f3f;
  4. using namespace std;
  5. int a[100005];
  6. int ans[100005];
  7. struct node
  8. {
  9. int l,r,minx;
  10. }s[100005*4];
  11. void build(int id,int l,int r)
  12. {
  13. s[id].l=l;
  14. s[id].r=r;
  15. if(l==r)
  16. s[id].minx=a[l];
  17. else
  18. {
  19. int mid=(l+r)/2;
  20. build(id*2,l,mid);
  21. build(id*2+1,mid+1,r);
  22. s[id].minx=min(s[id*2].minx,s[id*2+1].minx);
  23. }
  24. }
  25. int query(int id,int l,int r,int i)
  26. {
  27. if(s[id].l==s[id].r)
  28. return s[id].l-i-1;
  29. int mid=(s[id].l+s[id].r)/2;
  30. if(a[i]>s[id*2+1].minx)
  31. query(id*2+1,mid+1,r,i);
  32. else
  33. query(id*2,l,mid,i);
  34. }
  35. int rep(int id,int i)
  36. {
  37. if(s[id].l==s[id].r)
  38. s[id].minx=INF;
  39. else
  40. {
  41. int mid=(s[id].l+s[id].r)/2;
  42. if(i<=mid)
  43. rep(id*2,i);
  44. else
  45. rep(id*2+1,i);
  46. s[id].minx=min(s[id*2].minx,s[id*2+1].minx);
  47. }
  48. }
  49. int main()
  50. {
  51. int n;
  52. while(scanf("%d",&n)!=EOF)
  53. {
  54. for(int i=1;i<=n;i++)
  55. scanf("%d",&a[i]);
  56. build(1,1,n);
  57. for(int i=1;i<=n;i++)
  58. {
  59. if(s[1].minx>=a[i])
  60. ans[i]=-1;
  61. else
  62. ans[i]=query(1,1,n,i);
  63. rep(1,i);
  64. }
  65. printf("%d",ans[1]);
  66. for(int i=2;i<=n;i++)
  67. printf(" %d",ans[i]);
  68. printf("\n");
  69. }
  70. return 0;
  71. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注