题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1145

神题。。。。。。
定义f(abcd)为高度排名为abcd的个数,例如闪电的个数为f(1324)。
我们化简一下:
 f(1324)-f(1243)-f(1432)
=f(1x2x)-f(1423)-f(12xx)+f(1234)-f(14xx)+f(1423)
=f(1x2x)+f(1234)-f(12xx)-f(14xx)
=f(1x2x)+f(1234)-[f(1xxx)-f(13xx)]
=f(1x2x)+f(13xx)+f(1234)-f(1xxx)
实验变成求f(1x2x),f(13xx),f(1234)和f(1xxx)。
其中f(1234)和f(1xxx)都比较好求,我们来重点讨论f(1x2x)和f(13xx)。
首先预处理出两个比较重要的数组l[i]和r[i]。l[i]表示1到i-1小于a[i]的个数;r[i]表示i+1到N中小于a[i]的个数。
--------------------------------------------------------------------------------------------------------------------------------------------
f(1x2x):
我们在2的位置进行统计,不妨记2的位置为i。为方便识别,我们在位置i那里涂成红色,即f(1xx)。
容易发现,如果我们知道了f(13),那么f(1xx)=f(13)*(N-i-r[i])。
于是变成了求f(13)。
13
=(13+31+12+21)-(31+12+21)
观察第1个括号:132 312 123 21
我们发现他们左边两个黑色的数中,其中一个小于红色的数,另一个可以大于或小于红色的数。
所以第1个括号为:l[i]*(l[i]-1)/2+l[i]*(i-1-l[i])
观察第2个括号:312 123 21
我们发现他们第2个黑色的数小于红色的数,第1个黑色的数没有限制。
所以第2个括号为:∑(j-1)(j<i且a[j]<a[i])
这个可以用树状数组实现。
--------------------------------------------------------------------------------------------------------------------------------------------
f(13xx):
我们在3的位置进行统计,记3的位置为i,把位置i那里涂成红色,即f(1xx)。
同样容易发现,如果我们知道了f(12),那么f(1xx)=f(12)*(N-i-r[i])。
注意这里的f(12)和上面的f(13)的不同之处在于统计的位置(红色)。
于是变成求f(12)。
12
=(12+12+21)-(12+21)
观察第1个括号:12 12 21
我们发现3的右边一定有一个2,然后1的位置随便放。
所以第1个括号为:∑(a[j]-1)(i<j且a[i]>a[j])
观察第2个括号:12 21
其实就是 xx
所以第2个括号为:r[i]*(r[i]-1)/2
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int sgn(DB x){if(abs(x)<EPS)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); inline int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const int maxN=;
const LL Mod=; int N;
int a[maxN+];
LL l[maxN+],r[maxN+];
LL ans; LL tree[maxN+];
#define lowbit(a) (a&(-a))
inline void update(int a,LL v){for(;a<=N;a+=lowbit(a))tree[a]=(tree[a]+v)%Mod;}
inline LL ask(int a){LL res=;for(;a>=;a-=lowbit(a))res=(res+tree[a])%Mod;return res;} LL g[maxN+]; /*
f(1324)-f(1243)-f(1432)
=f(1x2x)-f(1423)-f(12xx)+f(1234)-f(14xx)+f(1423)
=f(1x2x)+f(1234)-f(12xx)-f(14xx)
=f(1x2x)+f(1234)-[f(1xxx)-f(13xx)]
=f(1x2x)+f(13xx)+f(1234)-f(1xxx)
*/ int main()
{
freopen("bzoj1145.in","r",stdin);
freopen("bzoj1145.out","w",stdout);
int i;
N=gint();
re(i,,N)a[i]=gint();
mmst(tree,);re(i,,N)l[i]=ask(a[i]-),update(a[i],);
mmst(tree,);red(i,N,)r[i]=ask(a[i]-),update(a[i],);
ans=; //+f(1x2x)
re(i,,N)g[i]=(LL(l[i])*LL(l[i]-)/+LL(l[i])*LL(i--l[i]))%Mod;
mmst(tree,);
re(i,,N)
{
g[i]=(g[i]-ask(a[i]-))%Mod;
update(a[i],i-);
}
re(i,,N)ans=(ans+g[i]*LL(N-i-r[i]))%Mod; //+f(13xx)
mmst(tree,);
red(i,N,)
{
LL t=LL(N-i-r[i]);
LL p=(ask(a[i]-)-r[i]*(r[i]-)/)%Mod;
ans=(ans+p*t)%Mod;
update(a[i],a[i]-);
} //+f(1234)
re(i,,N)g[i]=l[i];
mmst(tree,);re(i,,N)update(a[i],g[i]),g[i]=ask(a[i]-);
mmst(tree,);re(i,,N)update(a[i],g[i]),g[i]=ask(a[i]-);
re(i,,N)ans=(ans+g[i])%Mod; //-f(1xxx)
re(i,,N)
{
LL t=LL(N-i-r[i]);
ans=(ans-t*(t-)*(t-)/)%Mod;
} ans=(ans%Mod+Mod)%Mod;
cout<<ans<<endl;
return ;
}

bzoj1145的更多相关文章

  1. bzoj1145[CTSC2008]图腾

    传送门 虽然是远古时期的ctsc,但是果然还是ctsc啊 前置芝士:树状数组 这个题最开始的思路很好想,由于之前写过一个类似处理的题,所以这个题我一开始就想到了思路. 首先,我们可以尝试讲图腾表示为x ...

  2. [bzoj1145]图腾

    如果将关系用一个数字来表示(相等表示不确定),那么题目相当于要计算$1324-1243-1432$=$(1323-1423)-(1233-1234)-(1322-1423)$=$1323+1234-( ...

  3. [转载]hzwer的bzoj题单

    counter: 664BZOJ1601 BZOJ1003 BZOJ1002 BZOJ1192 BZOJ1303 BZOJ1270 BZOJ3039 BZOJ1191 BZOJ1059 BZOJ120 ...

  4. BZOJ刷题列表【转载于hzwer】

    沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...

随机推荐

  1. 《python源代码剖析》笔记 python中的Dict对象

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.PyDictObject对象 -->  C++ STL中的map是基于RB-tre ...

  2. Mybatis高级映射、动态SQL及获得自增主键

    一.动态SQL 相信大家在用mybatis操作数据库时时都会碰到一个问题,假如现在我们有一个关于作者的list authorList,需要根据authorList里已有的作者信息在数据库中查询相应作者 ...

  3. [React Testing] The Redux Store - Multiple Actions

    When using Redux, we can test that our application state changes are working by testing that dispatc ...

  4. C++11 : variadic templates(可变参数模板)

      Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templat ...

  5. WPF之Binding的使用

    引出: 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件 ...

  6. Linux 数据 CD 刻录

    http://www.cyberciti.biz/tips/linux-burning-multi-session-cds-on-linux.html #mkisofs -dvd-video -inp ...

  7. Sass函数--数字函数

    数字函数简介 Sass 中的数字函数提要针对数字方面提供一系列的函数功能: percentage($value):将一个不带单位的数转换成百分比值: round($value):将数值四舍五入,转换成 ...

  8. C语言学习笔记---谭浩强

    前段时间有机会去面试了一次,真是备受“打击”(其实是启发),总的来说就是让我意识到了学习工具和学习技术的区别.所以最近在看一些数据结构和算法,操作系统,python中的并行编程与异步编程等东西.然而数 ...

  9. asp.net 面试基础题

    WebSite和WebApplication的区别1)当改变后台代码时,WebApplication需重启浏览器或者重新生成解决方案,而WebSite则不用:2)WebSite没有Solution,没 ...

  10. 掌众android面试题

    1,android动画分类? http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html 2,android service启动 ...