题目链接

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. codeforces 154A 贪心

    贪心 题目自身限制每个字母最多出现在一个限制词语中,给出k个限制词语,将问题转化为k个子问题,对每个限制词语遍历给出的字符串,如限制词do,若出现连续的oddoood(连续的o和d),统计o和d出现的 ...

  2. EntityFramework 学习 一 DbContext

    上一节中EDM自动生成SchoolEntities类,该类继承DbContext EntityFramework4.1之前的版本,EDM生成的类继承ObjectContext,使用ObjectCont ...

  3. Spark操作算子本质-RDD的容错

    Spark操作算子本质-RDD的容错spark模式1.standalone master 资源调度 worker2.yarn resourcemanager 资源调度 nodemanager在一个集群 ...

  4. Runtime Error(ACCESS_VIOLATION)

    1.     while(scanf("%d",&n) )// 错误,会出现  Runtime Error(ACCESS_VIOLATION) while(scanf(&q ...

  5. Echarts 关系图 添加点击事件

    /*实现的效果是:在关系图上加点击事件,点击某个点,得到改点代表的内容,并且实现一个跳转效果. 关键代码已用红色标出*/ <!DOCTYPE html> <html lang=&qu ...

  6. jsp 自定义标签中的Function函数

    在taglib中我们发现有一个标签叫做<function>,这次简单介绍<function>标签的基本用法,<function>标签可以做什么呢? 它可以让我们在j ...

  7. bzoj 2342: 双倍回文 回文自动机

    题目大意: 定义双倍回文串的左一半和右一半均是回文串的长度为4的倍数的回文串 求一个给定字符串中最长的双倍回文串的长度 题解: 我们知道可以简单地判定以某一点结尾的最长回文串 我们知道可以简单地判定以 ...

  8. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  9. Python Class 的实例方法/类方法/静态方法

    实例方法.类方法.静态方法 class MyClass(object): class_name = "MyClass" # 类属性, 三种方法都能调用 def __init__(s ...

  10. nvidia-docker 安装

    1.安装docker 官方网址安装说明 https://docs.docker.com/install/linux/docker-ce/ubuntu/ 2.ubuntu 14.04/16.04/18. ...