Nikita and Order Statistics

Nikita likes tasks on order statistics, for example, he can easily find the k-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number x is the k-th number in increasing order on this segment. In other words, you should find the number of segments of a given array such that there are exactly k numbers of this segment which are less than x

.

Nikita wants to get answer for this question for each k

from 0 to n, where n

is the size of the array.

Input

The first line contains two integers n

and x (1n2105,109x109)

.

The second line contains n

integers a1,a2,,an (109ai109)

 — the given array.

Output

Print n+1

integers, where the i-th number is the answer for Nikita's question for k=i1

.

Examples
Input
5 3
1 2 3 4 5
Output
6 5 4 0 0 0 
Input
2 6
-5 9
Output
1 2 0 
Input
6 99
-1 -1 -1 -1 -1 -1
Output
0 6 5 4 3 2 1 



把所有小于 x 的数记为 1,剩下的数记为 0,则题目要求的是有多少个区间有 k1。对这个 01 数组求前缀和,设为 s,则题目要求的是有多少个 (l,r)(lr) 满足 srsl1=k

现在相当于有一个长度为 n+1

的数组 s,满足 sisi+1si+1,问两个元素的差的取值的情况数。设多项式 fxi 项的系数等于 sk=ik 的个数,设多项式 gxi 项的系数等于 sk=ik 的个数。对 fg 做卷积,则积的 xk(k>0) 的系数就是对应的答案。当 k=0 时,首先要减去自己卷上自己的 n+1 次,然后再除以二,就是对应的答案。

 1 #include"cstdio"
 2 #include"iostream"
 3 #include"cmath"
 4 #include"cstring"
 5 #include"algorithm"
 6 using namespace std;
 7 typedef long long ll;
 8 int n,m;
 9 const int nn=2e6+20;
10 const double pi = acos(-1);
11 struct complex {
12   double x,y;
13   complex(double xx=0,double yy=0){x=xx,y=yy;}
14 };
15 
16 complex operator-(complex a,complex b) {return complex(a.x-b.x,a.y-b.y);}
17 complex operator+(complex a,complex b){return complex(a.x+b.x,a.y+b.y);}
18 complex operator*(complex a,complex b)
19 {return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
20 
21 string s1,s2;
22 int n2; int k;
23 complex a[2000000],b[2000000];
24 int limit=1,l;
25 ll num[2000000];
26 int sum[2000000];
27 int r[2000000];
28 
29 inline void fft(complex *t,int type)
30 {
31    for (int i=0;i<limit;i++)if (i<r[i])swap(t[i],t[r[i]]);
32    for (int mid=1;mid<limit;mid<<=1)
33    {
34      complex wn=complex(cos(pi/mid),type*sin(pi/mid));
35      for (int R=mid<<1,j=0;j<limit;j+=R)
36      {
37         complex w=complex(1,0);
38         for (int k=0;k<mid;k++,w=w*wn)
39         {
40            complex x=t[j+k],y=w*t[j+mid+k];
41            t[j+k]=x+y;
42            t[j+k+mid]=x-y;
43         }
44      }
45 
46    }
47 }
48 int t[300000];
49 
50 int main()
51 {
52 
53    cin>>n>>k;
54    for(int i=1;i<=n;i++)scanf("%d",&t[i]),t[i]=t[i-1]+(t[i]<k);
55    for (int i=0;i<=n;i++)a[n+t[i]].x++,b[n-t[i]].x++;
56    int len = 3*n;
57    while (limit<=len)limit<<=1,l++;
58    for (int i=0;i<limit;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
59    fft(a,1); fft(b,1);
60    for (int i=0;i<=limit;i++)a[i]=a[i]*b[i];
61    fft(a,-1);
62    for (int i=0;i<=len;i++)num[i]=(ll)(a[i].x/limit+0.5);
63   // cout<<endl;
64   // for (int i=1;i<=len;i++)cout<<num[i]<<" ";
65   // cout<<"**************"<<endl;
66    ll ans = num[n*2];
67    ans=(ans-n-1)>>1;
68    cout<<ans<<" ";
69    for (int i=1;i<=n;i++)printf("%lld ",num[i+n+n]);
70 
71 
72 }










原文地址:https://www.cnblogs.com/zhangbuang/p/10310925.html