题意:

给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ax is the value at position x.  的个数

明显数组的值太大了

先离散化,然后就是简单的树状数组了

对于每一个i,只要统计i前面的数中比a[i]大的数的个数,和i后面的数中比a[i]小的数的个数即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <math.h> #define LL long long using namespace std; const int maxn = + ; struct Node
{
int id,init,chg;
};
Node node[maxn];
int fir[maxn];
int sec[maxn];
int c[maxn]; bool cmp1(Node x,Node y)
{
return x.init<y.init;
} bool cmp2(Node x,Node y)
{
return x.id<y.id;
} inline int lb(int x)
{
return x & (-x);
} void update(int x,int add)
{
while(x <= maxn){
c[x] += add;
x += lb(x);
}
} int query(int x)
{
int ret = ;
while(x > ){
ret += c[x];
x -= lb(x);
}
return ret;
} LL solve(int n)
{
sort(node+,node+n+,cmp1);
for(int i=;i<=n;i++)
node[i].chg = i;
sort(node+,node+n+,cmp2);
memset(c,,sizeof c);
for(int i=;i<=n;i++){
fir[i] = i - - query(node[i].chg - );
update(node[i].chg,);
}
memset(c,,sizeof c);
for(int i=n;i>;i--){
sec[i] = query(node[i].chg - );
update(node[i].chg,);
}
LL ret = ;
for(int i=;i<=n;i++)
ret += (LL) fir[i] * sec[i];
return ret;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&node[i].init);
node[i].id = i;
}
//printf("%I64d\n",solve(n));
cout<<solve(n)<<endl;
return ;
}

cf 61 E. Enemy is weak 离散化+树状数组的更多相关文章

  1. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  2. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  3. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  6. poj-----Ultra-QuickSort(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38258   Accepted: 13784 ...

  7. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组

    题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...

  10. 【bzoj5055】膜法师 离散化+树状数组

    题目描述 给定一个序列$a$,求满足$i<j<k$且$a_i<a_j<a_k$的三元组$(i,j,k)$的个数. 输入 第一行1个数 n 第二行n个数 a_i 输出 一个数,表 ...

随机推荐

  1. 异步处理工具类:AsyncTask

    (一) AsyncTask,是android提供的轻量级的异步类.可以直接继承AsyncTask,在类中实现异步操作,可以通过接口实现UI进度更新,最后反馈执行的结果给UI主线程 .之所以有Handl ...

  2. POJ-2991 Crane(区间更新+向量旋转)

    题目大意:n个向量首尾相连,每次操作使某个区间中的所有向量都旋转同样的角度.每次操作后都回答最后一个向量的坐标. 题目分析:区间维护向量信息.向量旋转:x1=x0*cos(t)-y0*sin(t),y ...

  3. poj3259 最短路判环

    题意:有一些点.一些道路和一些虫洞,道路是双向的,连接两点,花费正的时间,而虫洞是单向的,连接两点,可以使时间倒退,求是否能够回到过去. 只要明确回到过去其实就是当出现一个负环的时候,不断沿这个环走, ...

  4. 26 个 jQuery使用技巧

    1. 禁用右键点击(Disable right-click) $(document).ready(function(){ $(document).bind("contextmenu" ...

  5. Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

    MVC中数据库表如果是一对一的主键关系时要加[Required]不然会出错Unable to determine the principal end of an association between ...

  6. Python_Day_5装饰器、字符串格式化、序列化、内置模块、生成器、迭代器之篇

    一.装饰器 为什么要用装饰器??? 在实际的开发环境中应遵循开发封闭原则,虽然在这个原则是用的面向对象开发,但也适用于函数式编程,简单地说,它规定已经实现的功能代码不是允许修改的,但是可以被扩展: 封 ...

  7. kubernetes centos 安装

    1.  安装       yum install -y  etcd  kubernetes   2.  配置         docker             /etc/sysconfig/doc ...

  8. ubutntu apt 源

    中国开源软件中心更新服务器(北京光环新网 服务器),包含其他开源镜像: deb http://mirrors.oss.org.cn/ubuntu/ vivid main restricted univ ...

  9. Dubbo架构设计详解-转

    Dubbo架构设计详解  2013-09-03 21:26:59    Yanjun Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解 ...

  10. 一个完整的JENKINS下的ANT BUILD.XML文件(Jenkins可以参考)

    一个完整的JENKINS下的ANT BUILD.XML文件 <?xml version="1.0" encoding="UTF-8"?> <p ...