题目链接

Sequence II

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 331    Accepted Submission(s): 151

Problem Description
Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than 1 and no bigger than n, and all numbers are different in this sequence.
Please calculate how many quad (a,b,c,d) satisfy:
1. 1≤a<b<c<d≤n
2. Aa<Ab
3. Ac<Ad
 
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with a line contains an integer n.
The next line follows n integers A1,A2,…,An.

[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= Ai <= n

 
Output
For each case output one line contains a integer,the number of quad.
 
Sample Input
1
5
1 3 2 4 5
 
Sample Output
4
题意:
很久很久以前,有一个长度为n的数列A,数列中的每个数都不小于1且不大于n,且数列中不存在两个相同的数.
请统计有多少四元组(a,b,c,d)满足:
1. 1≤a<b<c<d≤n
2. Aa<Ab
3. Ac<Ad 分析:
我的做法是把四元组分解成二元组来处理,分解的方法就是枚举把数组依次分为两部分,然后对每部分都用树状数组求逆序数,结果相乘就是满足条件的四元组的个数。
树状数组求逆序数的做法是,因为知道数列里的数是1-n,所以可以 以个数为c[]数组的元素,值为下标,通过求和来 求大于当前数 或者 小于当前数的个数
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#define LL __int64
const int maxn = 1e5 + ;
using namespace std;
LL a[maxn], c[maxn], n, f[maxn]; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int d)
{
while(x <= n)
{
c[x] += d;
x +=lowbit(x);
}
}
LL sum(int x)
{
LL ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
} int main()
{
int t;
LL i, ans, tmp;
scanf("%d", &t);
while(t--)
{
ans = ;
scanf("%I64d", &n); memset(f, , sizeof(f));
memset(c, , sizeof(c));
for(i = ; i <= n; i++)
{
scanf("%I64d", &a[i]);
add(a[i], );
f[i] = f[i-] + sum(a[i]-);
} memset(c, , sizeof(c));
tmp = ;
for(i = n; i >= ; i--)
{
tmp = sum(n) - sum(a[i]);
add(a[i], );
ans += tmp*f[i-];
}
printf("%I64d\n", ans);
}
return ;
}

hdu 5147 Sequence II (树状数组 求逆序数)的更多相关文章

  1. hdu 5147 Sequence II 树状数组

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  2. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  4. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  5. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

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

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  7. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  8. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

随机推荐

  1. EntityFramework 学习 一 Table-Valued Function in Entity Framework 5.0

    USE [SchoolDB] GO /****** Object: UserDefinedFunction [dbo].[GetCourseListByStudentID] */ SET ANSI_N ...

  2. 机器学习——支持向量机(SVM)

    机器学习--支持向量机(SVM) 支持向量机(Support Vector Machine)广泛地应用于分类问题,回归问题和异常检测问题.支持向量机一个很好的性质是其与凸优化问题相对应,局部最优解就是 ...

  3. Oracle的PL_SQL的异常处理

    --什么是异常 --异常是在PL/SQL运行过程中有可能出现的错误. --执行异常的语句 exception when [异常] when --异常输出信息. --Oracle的预定义异常 CASE_ ...

  4. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

  5. Linux各个文件夹的主要作用 (源地址

    (源地址blog.csdn.net/lonelysky/article/details/5374230,侵删) linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev ...

  6. hibernate复习第(4)天

    1.hibernate的映射类型.hbm.xml中property中的type属性.这个type属性是表示持久化类中的属性对应数据库中的什么数据类型,用来构建一种映射type的可选值:hibernat ...

  7. Opencv - Android 配置安装

    1.道具们: windows 7 64位 OpenCV-2.4.6-android-sdk-r2 ( http://sourceforge.net/projects/opencvlibrary/fil ...

  8. windows下面的python的MySQLdb环境安装

    什么是MySQLdb? MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 如何安装MySQ ...

  9. 【转】Lucas定理 & 逆元学习小结

    (From:离殇灬孤狼) 这个Lucas定理是解决组合数的时候用的,当然是比较大的组合数了.比如C(1000000,50000)% mod,这个mod肯定是要取的,要不算出来真的是天文数字了. 对于一 ...

  10. Operating System-Process(2)进程表&&中断处理

    上一篇文章阐述了进程的基本信息,本文主要介绍进程的实现,主要内容: 进程表(Process Table or Process Control Blocks) 中断处理(Interrupt) 一.进程表 ...