转载自:http://blog.csdn.net/queuelovestack/article/details/52096337

【题意】
给你一个序列A,选出四个下标不同的元素,下标记为a,b,c,d

a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n

满足Aa<Ab,Ac>Ad

问能找到多少个这样的四元组(a,b,c,d)

【类型】
树状数组应用

【分析】
因为a<b,c<d,Aa<Ab,Ac>Ad

所以我们暂时称(a,b)为递增对,(c,d)为递减对

题目就转化成递增对*递减对-重复对

重复对包括如下四种:

①b,c一致

②a,c一致

③b,d一致

④a,d一致

那么我们该如何计算重复对呢?

考虑一致点下标为i,我们需要事先处理出位置i左边比它大的数的个数w[i],比它小的数的个数l[i];右边比它大的数的个数v[i],比它小的数的个数r[i],这样所有重复对的对数为l[i]*r[i]+l[i]*v[i]+w[i]*r[i]+w[i]*v[i]

而计算这些个数可以通过树状数组求解

【时间复杂度&&优化】
O(nlogn)

题目链接→HDU 5792 World is Exploding

感觉他写的真的很好,看完之后就大致可以懂了,但树状数组求的那块太简略,我补充下:

首先要做的是把输入s[i]离散化,因为题目说的是1e9。离散化之后就是每次检查前n项和,求l w数组,同理,更新v r数组,但注意,每次都要初始化c数组,因为c数组代表的就是第几位出现的次数,这两次相求会影响,所以每次都跟新。

这题还说明一个问题:树状数组和离散化合到一起有很大作用,比如这题,他就能求出递增对个数,利用的就是当求i的递增对个数时,求0~i-1中所有出现次数之和。

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
#define PI(A) cout<<(A)<<endl
#define SI(N) cin>>(N)
#define SII(N,M) cin>>(N)>>(M)
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
#define dbg(x) cout <<#x<<" = "<<(x)<<endl
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS= 1e- ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAXN= + ; int n,s[MAXN],c[MAXN],a[MAXN];
ll l[MAXN],r[MAXN],w[MAXN],v[MAXN];
//树状数组
int lowbit(int t){return t&(-t);}
//在c[i]上加x
void add(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
}
//求c[]的前n项和
int sum(int n)
{
int s=;
while(n>)
{
s+=c[n];
n-=lowbit(n);
}
return s;
} int main()
{
while(SI(n))
{
int k,Max=;
//su1是递增对个数 su2是递减对个数
ll ans=,su1=,su2=;
//输入
rep(i,n) {SI(s[i]);a[i]=s[i];}
//将s[]离散化
sort(a,a+n);
k=unique(a,a+n)-a;
rep(i,n)
{
s[i]=lower_bound(a,a+k,s[i])-a+;
Max=max(Max,s[i]);
}
//求递增对个数 和l w数组
cle(c,);
rep(i,n)
{
l[i]=sum(s[i]-);
w[i]=sum(Max)-sum(s[i]);
add(s[i],);
su1+=l[i];
}
//求递减对个数 和r v数组
cle(c,);
reRep(i,n-,)
{
r[i]=sum(s[i]-);
v[i]=sum(Max)-sum(s[i]);
add(s[i],);
su2+=r[i];
}
ans=su1*su2;
//减去重复的
rep(i,n)
ans-=l[i]*r[i]+l[i]*w[i]+v[i]*r[i]+v[i]*w[i];
PI(ans);
}
return ;
}

2016 Multi-University Training Contest 5 World is Exploding的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  5. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  8. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

  9. 2016 Al-Baath University Training Camp Contest-1 D

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

  10. 2016 Al-Baath University Training Camp Contest-1 C

    Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...

随机推荐

  1. 三国游戏 2010年NOIP全国联赛普及组

    题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有N 位武将(N 为偶数且不小 ...

  2. HDU-3308 LCIS(区间合并)

    题目大意:给一个整数序列,m次询问,每次询问某个区间中最长连续上升子序列的长度. 题目分析:线段树区间合并.维护以区间左端开头的.以区间右端点结尾的和区间最长的上升连续序列. 代码如下: # incl ...

  3. Linux驱动设计—— 中断与时钟

    中断和时钟技术可以提升驱动程序的效率 中断 中断在Linux中的实现 通常情况下,一个驱动程序只需要申请中断,并添加中断处理函数就可以了,中断的到达和中断函数的调用都是内核实现框架完成的.所以程序员只 ...

  4. c++关于接口机制和不完全类型的小问题

    都和typedef有关 一个是接口机制时用到的 就是所有用到接口的源文件只需包含简单的接口声明 接口的具体实现在其他源文件中实现 接口可以是 //interface.h typedef struct ...

  5. JVM 虚拟化

    http://www.infoq.com/cn/news/2015/05/java20-multitenant-jvm http://2016.qconshanghai.com/presentatio ...

  6. mysql学习之-三种安装方式与版本介绍

    MYSQL版本介绍 mysql分alpha,beta,rc,GA四个版本. alpha  暗示这是一个以展示新特性为目的的版本,存在比较多的不稳定因素,还会向代码中添加新新特性beta 以后的beta ...

  7. apk反编译生成程序的源代码和图片、XML配置、语言资源等文件

    Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...

  8. Linux体系结构(二): Linux系统层次

    前一节内容大概介绍了一下Linux的历史由来,各大Linux发行厂商都是基于GNU项目下的所有开源软件,来构建各自己的Linux发行版本,一个完整的Linux发行版本大概可以分为以下四个层次结构:   ...

  9. 一个不错的vim配置

    set nocp set backspace=indent,eol,start set number set autoindent set nocompatible set bs=indent,eol ...

  10. Linux-Hostname-details

    转自:http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题 ...