转载:https://blog.csdn.net/winter2121/article/details/81255827#commentBox
5727: 小奇遐想
时间限制: 1 Sec 内存限制: 128 MB
提交: 225 解决: 35
[提交] [状态] [讨论版] [命题人:admin]
题目描述
撷来一缕清风飘渺
方知今日书信未到
窗外三月天霁垂柳新长枝条
风中鸟啼犹带欢笑
——《清风醉梦》
小奇望着青天中的悠悠白云,开始了无限的遐想,在它的视野中,恰好有n朵高度不同的白云排成一排,他想从左到右选出四朵白云a,b,c,d,使得h_a<h_b<h_d<h_c,即看起来像是彩虹的形状!它想知道有多少种方案数。
输入
第一行包括1个整数n。
第二行包括n个整数,第i个正数表示h_i,保证这n个整数是n的一个全排列。
输出
输出一个整数表示答案。(mod 16777216)
样例输入
-
5
-
1 5 3 2 4
样例输出
0
提示
对于10%的数据n<=600;对于40%的数据n<=5000;
对于100%的数据n<=200000。
【分析】
用树状数组求正序对的方法,求数组pre2,pre2[i] 表示 i 处结尾的长度为2的升序子序列种数(即正序对数量)
同理,求数组late2, late2[i] 表示 i 处为开头向后,长度为2的升序子序列数量(正序对数量)
根据late2数组可继续利用树状数组求得数组late3
late3[i] 表示 i 处为开头向后,长度为3的升序子序列数量
再来一次,倒叙循环,求得数组big , big[i] 表示 i 处后面的大于 a[i] 的数的个数
好,现在,枚举选中的四个数的第二个数,for(int i=1;i<=n;i++)
那么当把a[i]作为第二个数时对总答案的贡献是 1243型种数 = 12**型种数 - 1234型种数(其中*表示大于2的数)
12**型种数 = pre2[i] * C(big[i], 2)
1234型种数 = pre2[i] * late3[i]
【代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mod=16777216;
const int MAX=2e5+5;
ll c[202020];
void add(int k,ll num,int top)
{
for(;k<=top;k+=k&-k)
c[k]=(c[k]+num)%mod;
}
ll read(int k)
{
ll res=0;
for(;k;k-=k&-k)
res=(res+c[k])%mod;
return res;
}
int a[MAX],n;
ll late3[MAX];
ll pre2[MAX];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
pre2[i]=read(a[i]-1); //i结尾长为2的升序列种数
pre2[i]%=mod;
add(a[i],1,n);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
late3[i]=read(n)-read(a[i]); //i开头长为2的升序种数
late3[i]%=mod;
add(a[i],1,n);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
ll temp=late3[i];
late3[i]=read(n)-read(a[i]); //i开头长为3的升序种数
late3[i]%=mod;
add(a[i],temp,n);
}
// ans = 12**-1234 (*>2)
memset(c,0,sizeof(c));
ll ans=0;
for(int i=n;i>=1;i--)
{
ll big=read(n)-read(a[i]);
add(a[i],1,n);
ll late=big*(big-1)/2%mod; //从后边选逆序对
ll res=pre2[i]*late%mod; //12**
res=( (res-pre2[i]*late3[i]%mod)%mod + mod)%mod;// -1234
ans=(ans+res)%mod;
}
cout<<ans<<endl;
}