洛谷

Codeforces


思路

一开始想偏想到了DP,后来发现我SB了……

考虑每个\(a_i<x\)的\(i\),记录它前一个和后一个到它的距离为\(L_i,R_i\),那么就有

\[ans_k=\sum_{i=1}^n L_iR_{i+k-1}
\]

显然把\(L\)数组翻转一下就是一个FFT了。

最后特判\(k=0\)。


代码

#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 800202
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std; int n,x;
int a[sz];
int L[sz],R[sz]; const db PI=acos(-1.0);
struct Complex
{
db x,y;
Complex(db xx=0,db yy=0){x=xx,y=yy;}
};
const Complex operator + (const Complex &a,const Complex &b) {return Complex(a.x+b.x,a.y+b.y);}
const Complex operator - (const Complex &a,const Complex &b) {return Complex(a.x-b.x,a.y-b.y);}
const Complex operator * (const Complex &a,const Complex &b) {return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);} int limit,r[sz];
void FFT_init(int n)
{
int l=-1;limit=1;
while (limit<=n+n) ++l,limit<<=1;
rep(i,0,limit-1) r[i]=(r[i>>1]>>1)|((i&1)<<l);
}
void FFT(Complex *a,int type)
{
for (int i=0;i<limit;i++) if (i<r[i]) swap(a[i],a[r[i]]);
for (int mid=1;mid<limit;mid<<=1)
{
Complex Wn(cos(PI/mid),type*sin(PI/mid));
for (int len=mid<<1,j=0;j<limit;j+=len)
{
Complex w(1,0);
for (int k=0;k<mid;k++,w=w*Wn)
{
Complex x=a[j+k],y=w*a[j+mid+k];
a[j+k]=x+y;a[j+k+mid]=x-y;
}
}
}
}
Complex _a[sz],_b[sz]; ll ans[sz];
ll CC(int n){return 1ll*n*(n+1)/2;} int main()
{
file();
int cnt,c,cc;
read(n,x);
rep(i,1,n) read(a[i]);
cnt=c=0;
rep(i,1,n)
if (a[i]<x) L[++c]=cnt+1,cnt=0;
else ++cnt;
cnt=0;cc=c;
drep(i,n,1)
if (a[i]<x) R[c--]=cnt+1,cnt=0;
else ++cnt;
reverse(L+1,L+cc+1);
rep(i,1,cc) _a[i].x=L[i],_b[i].x=R[i];
FFT_init(cc+1);
FFT(_a,1);FFT(_b,1);
rep(i,0,limit-1) _a[i]=_a[i]*_b[i];
FFT(_a,-1);
rep(i,1,n) ans[i]=ll(_a[i+cc].x/limit+0.5);
if (cc)
{
rep(i,1,cc) ans[0]+=CC(R[i]-1);
ans[0]+=CC(L[cc]-1);
} else ans[0]=CC(n);
rep(i,0,n) printf("%lld ",ans[i]);
return 0;
}

Codeforces 993E Nikita and Order Statistics [FFT]的更多相关文章

  1. [Codeforces 993E]Nikita and Order Statistics

    Description 题库链接 给你一个长度为 \(n\) 的序列 \(A\) ,和一个数 \(x\) ,对于每个 \(i= 0\sim n\) ,求有多少个非空子区间满足恰好有 \(i\) 个数 ...

  2. CF993E:Nikita and Order Statistics(FFT)

    Description 给你一个数组 $a_{1 \sim n}$,对于 $k = 0 \sim n$,求出有多少个数组上的区间满足:区间内恰好有 $k$ 个数比 $x$ 小.$x$ 为一个给定的数. ...

  3. CF993E Nikita and Order Statistics 【fft】

    题目链接 CF993E 题解 我们记小于\(x\)的位置为\(1\),否则为\(0\) 区间由端点决定,转为两点前缀和相减 我们统计出每一种前缀和个数,记为\(A[i]\)表示值为\(i\)的位置出现 ...

  4. CF993E Nikita and Order Statistics

    小于x的赋值为1,否则为0 区间等于k的个数 求0~n连续的n+1个k? N<=1e5? FFT! 考虑卷积建模:用下标相加实现转移到位,数值相乘类比乘法原理! 法一: 分治,然后FFT没了 法 ...

  5. CF993E Nikita and Order Statistics 多项式卷积 快速傅里叶变换

    题意: 给你一个数组a1~an,对于k=0~n,求出有多少个数组上的区间满足:区间内恰好有k个数比x小.x为一个给定的数.n<=10^5.值域没有意义. 分析: 大神们都说这道题是一个套路题,真 ...

  6. Codeforces 756C Nikita and stack

    Codeforces 756C Nikita and stack 题目大意: 给定一个对栈进行操作的操作序列,初始时序列上没有任何操作,每一次将一个本来没有操作的位置变为某一操作(push(x),po ...

  7. codeforces 632E. Thief in a Shop fft

    题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...

  8. Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞

    B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...

  9. codeforces 286 E. Ladies' Shop (FFT)

    E. Ladies' Shop time limit per test 8 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. ubuntu没有/usr/include/sys目录

    实际上不是没有sys目录,只是系统给换路径了 32位系统:/usr/incude/i386-linux-gnu/sys 64位系统:/usr/include/x86_64-linux-gnu/sys/ ...

  2. 测试四则运算2:Right-BICEP

    n6个值得测试的具体部位,他们能够提高你的测试技巧 nRight-结果是否正确? nB-是否所有的边界条件都是正确的? nI-能查一下反向关联吗 nC-能用其他手段交叉检查一下结果吗? nE-你是否可 ...

  3. Dapper.net 输出存储过程实例

    1.存储过程名: public static class CampaignTrackingDomainSql { /// <summary> /// proc /// </summa ...

  4. Python的参数类型

    参数类型: 1.必填参数,位置参数(positional arguments,官方定义,就是其他语言所说的参数) 2.默认值参数,非必传 3.可变参数,非必传,不限制参数个数,比如说给多个人发邮件,发 ...

  5. redis架构~哨兵模式

    一 哨兵模式稳定版本  redis哨兵模式是redis自带的高可用框架,稳定版本为redis2.8以上二 哨兵模式建立  1 避免单点故障,建立启动多个哨兵进程  2 哨兵模式启动命令 redis-s ...

  6. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  7. ASP.NET MVC - 发布web应用程序、部署到IIS

    发布项目 右击项目 - 发布 选择IIS - 点击发布 发布方法 - 文件系统,目标位置 - 选择与项目所在目录不同的目录(也即,指定一个发布生成文件的目录),文件发布选项 - 不选 - 点击发布 安 ...

  8. sorter函数参数设置

    python内置sorted函数排完序生成一个新列表,而列表list.sorted则是直接在原列表上修改 sorted默认排序是正序,即从小到大. 若想倒着排序,则reverse=True 当需要排序 ...

  9. win7系统查看硬盘序列号步骤

    1.在开始那里输入cmd,打开命令窗口: 2.输入diskpart,按enter键,进入底盘查看选项: 3.输入list disk,按回车键: list disk:查看电脑上有几块硬盘: 输入sele ...

  10. Perf -- Linux下的系统性能调优工具,第 1 部分【转】

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ Perf 简介 Perf 是用来进行软件性能分析的工具. 通过它,应用程序可以利用 ...