hdu 5147 樹狀陣列
http://acm.hdu.edu.cn/showproblem.php?pid=5147
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
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
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
/**
hdu5147 樹狀陣列
解題思路:
要統計四元組的數量我們可以通過列舉c,然後統計區間[1,c-1]有多少二元組(a,b)滿足a<b且Aa<Ab,以及統計出區間[c+1,n]有多少d滿足Ac<Ad,
根據乘法原理,把這兩項乘起來就可以統計到答案裡了.然後我們來處理子問題:區間[1,c-1]內有多少二元組(a,b).那麼我們可以列舉b,然後統計
區間[1,b-1]內有多少a滿足Aa<Ab,那麼這個可以通過用樹狀陣列詢問字首和來實現.
具體實現:b[i]和c[i]中儲存的分別為以i結尾的Ax<Ay的對數和從i+1到n中Ax<Ay的對數,二者相乘即為答案。
時間複雜度是O(nlogn).
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
int C[100005],b[100005],c[100005],a[100005];
int n;
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=C[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n)
{
C[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(C,0,sizeof(C));
for(int i=1;i<=n;i++)
{
b[i]=sum(a[i]);
add(a[i],1);
}
memset(C,0,sizeof(C));
for(int i=n;i>=1;i--)
{
c[i]=sum(n)-sum(a[i])+c[i+1];
add(a[i],1);
}
LL ans=0;
for(int i=2;i<=n-2;i++)
{
LL t1=b[i];
LL t2=c[i+1];
ans+=t1*t2;
}
printf("%I64d\n",ans);
}
return 0;
}
相關文章
- HDU5147 Sequence II(樹狀陣列+字首和+字尾和)陣列
- hdu 3874 樹狀陣列陣列
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- hdu4325 樹狀陣列+離散化陣列
- hdu 4368 樹狀陣列 離線維護陣列
- HDU 1166 敵兵佈陣 (樹狀陣列)陣列
- HDU 1166 敵兵佈陣(樹狀陣列)陣列
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- HDU2689 Sort it (樹狀陣列求逆序數)陣列
- HDU4843Wow! Such Sequence!(樹狀陣列寫法)陣列
- 樹狀陣列陣列
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- HDU5419Victor and Toys(樹狀陣列+數學期望)陣列
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- 解析樹狀陣列陣列
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- HDU4991 Ordered Subsequence (dp+樹狀陣列+離散化)陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- poj 2481 樹狀陣列陣列
- 二維樹狀陣列陣列
- 【樹狀陣列 單點修改,區間求值】hdu 1166 敵兵佈陣陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列和逆序對陣列
- 樹狀陣列快速入門陣列
- HDU1166 敵兵佈陣【樹狀陣列 單點修改+區間查詢】陣列
- HDU 1556-Color the ball(樹狀陣列-區間修改 單點查詢)陣列
- 樹狀陣列模板+習題集陣列
- 樹狀陣列3種基本操作陣列
- 學習筆記----樹狀陣列筆記陣列
- 樹狀陣列upc1976陣列
- CSU 4441 Necklace (樹狀陣列/LIS)陣列
- 樹狀陣列(我是真小白)陣列
- 線段樹+差分——【模板】樹狀陣列2陣列
- 10:Challenge 3(樹狀陣列直接修改)陣列