题目链接

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. 常用CSS设置

    主要内容: 一.容器类 二.文本类 三.特效类 一.容器类 1.background-image:url('img/02.gif');   设置背景图(可以是动态图) 2.background-col ...

  2. Luogu-4410 [HNOI2009]无归岛

    裸的仙人掌最大独立子集,结果一个zz的错误让我调了好久... \(-inf\)开始设为\(0x7fffffff\)结果\(A_i\)有负数一加就炸了 #include<cstdio> #i ...

  3. 算法(Algorithms)第4版 练习 2.1.24

    代码实现: package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; ...

  4. JS中不同类型的值比较问题

    我们比较数据的时候大多是以下两种情况: 3 > 4 ; "m" > "n" ; 但有时候可能会出现比较符号两侧的数据类型不相同的场景,例如 5 &g ...

  5. 在CentOS安装CMake

    http://www.cnblogs.com/mophee/archive/2013/03/19/2969456.html 一.环境描述 1.系统:CentOS 6.4 i386 (min) 2.登录 ...

  6. 机器学习(十九)— xgboost初试kaggle

     1.官网下载kaggle数据集Homesite Competition数据集,文件结构大致如下: 2.代码实战 #Parameter grid search with xgboost #featur ...

  7. Django-02

    知识预览 Ajax前戏:json Ajax简介 jquery实现的ajax JS实现的ajax 回到顶部 Ajax前戏:json 什么是json? 定义: JSON(JavaScript Object ...

  8. 基于T4的生成方式

    一.什么是T4模板 T4是对“Text Template Transformation Toolkit”(4个T)的简称.是一个基于文本文件转换的工具包.T4的核心是一个基于“文本模板”的转换引擎(以 ...

  9. hibernate复习第(二)天

    今日要点: 关联映射 多对一(Employee - Department) 一对多(Department - Employee) 一对一(Person - IdCard) 多对多(teachet - ...

  10. 2018-06-07 RF test 1 :TX Power test

    Test item: 1.Output power:   屏蔽网房-同轴线-频谱仪 The radio circuitry, generally referred to as the Device U ...