题目: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. 【转】Mac访问Windows共享文件夹

    相信大多数的用户用Windows访问Windows的共享文件夹是一件很容易的事,但是如果用Mac来访问Windows共享文件夹就会遇到很多的麻烦了,尤其是设置是比较有区别的吗,接下来的将用图文交大家怎 ...

  2. php防sql注入、xss

    php自带的几个防止sql注入的函数http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2013/0318/12234.html addslashe ...

  3. cnBlogs_代码着色

    一个程序员当然希望写出来的代码不仅质量上好,而且看上去也很好.以前在网络上看见别人写的代码,着色以及背景都好极了,很是羡慕,但就是不知道如何设置 --------------------------- ...

  4. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  5. 字符串数组越界bug(2)

    概述 数组下标从0開始,尽管从初学都已经知道,<陷阱与缺陷>重复强调,而在指尖运动中,就有那么几次不小心,让"精子"掉进这个"洞里"!其次,C语言字 ...

  6. Redmine backlogs 升级

    刚装完1.0.3两天,1.0.4发布了,乘项目还没有开始,赶快升级.升级过程 1.设置环境变量: RAILS_ENV=production export RAILS_ENV 2. 获取最新代码: cd ...

  7. Java基础知识强化63:Arrays工具类之方法源码解析

    1. Arrays工具类的sort方法: public static void sort(int[] a): 底层是快速排序,知道就可以了,用空看. 2. Arrays工具类的toString方法底层 ...

  8. Python字符串方法

    capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...

  9. jquery之onchange事件

    $(function(){ $("#opreateHtml").window("close"); $("#deliveryGrid").da ...

  10. 什么是工程师文化?各位工程师是为什么活的?作为一个IT或互联网公司为什么要工程师文化?

    为什么要工程师文化? 看看最近二十年来社会的发展,计算机和互联网已经渗透到了这个社会的每一个角落,各式各样的计算机技术成为了整个世界发展的强大引擎,各式各样的创新,无论是业务创新还是技术创新,都是依托 ...