1. 程式人生 > >2016多校訓練#5 1012 HDU 5792 樹狀陣列 程式碼詳解

2016多校訓練#5 1012 HDU 5792 樹狀陣列 程式碼詳解

              

World is Exploding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 666    Accepted Submission(s): 315


Problem Description Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: abcd,1a<bn,1c<dn,Aa<Ab,Ac>Ad.
Input The input consists of multiple test cases. 
Each test case begin with an integer n in a single line.

The next line contains n integers A
1
,A2An
.
1n50000
0Ai1e9
Output For each test case,output a line contains an integer.
Sample Input 4 2 4 1 3 4 1 2 3 4
Sample Output 1 0                從上面的題目描述來看,本題與2016多校#4的最後一道題有異曲同工之妙,都是尋找陣列中間某一個元素的逆序對,然而不同的是,本題的資料範圍更大,資料也並不是像上次的題目一樣是連續的1到n,而是離散的。這個時候,如果使用線段樹來進行查詢的話肯定很複雜(反正樓主弱,不會離散化的線段樹),樓主第一次寫樹狀陣列,借鑑了其他高手的AC程式碼進行註釋解釋,以下,方便初學者看懂:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>//樹狀陣列 
using namespace std;
typedef long long ll;
#define N 50010 

vector<int>v;
int num[N], vis[N];
int pos[N], val[N];
int e1[N], e2[N];
int lg[N], rg[N], ls[N], rs[N];//左小 右小 左大 右大 
int c[2][N];
int n;

bool cmp1(int a, int b)
{
	if(val[a] == val[b]) return pos[a] < pos[b];
	return val[a] < val[b];
}

bool cmp2(int a, int b)
{
	if(val[a] == val[b]) return pos[a] < pos[b];
	return val[a] > val[b];
}

int lowbit(int x)//求x為二進位制表示的最低位 0的個數 
{
	return x&(-x);//k的值就表示子樹的個數,子樹即為樹狀陣列的元素 
}

void update(int kind, int x, int val)//更新這個節點到祖先的所有節點的值 
{
	for(; x <= n; x += lowbit(x)) c[kind][x] += val;
}
//i的父節點為p,則p = i + lowbit(i); 
int query(int kind, int x)//樹狀陣列求前N個元素的和 
{
	if(x == 0) return 0;
	int sum = 0;
	for(; x > 0; x -= lowbit(x)) sum += c[kind][x];
	return sum;
}

int main()
{
	while(~scanf("%d", &n))
	{
		memset(vis, 0, sizeof(vis));
		v.clear();
		for(int i = 1; i <= n; i++)
		{
			pos[i] = i;
			scanf("%d", &val[i]);
			v.push_back(val[i]);//將讀進去的元素放到向量中 
			e1[i] = e2[i] = i;
		}
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());//去除陣列中相鄰重複元素 
		for(int i = 1; i <= n; i++)
		{
			val[i] = 1+(lower_bound(v.begin(), v.end(), val[i])-v.begin());//求每個元素在排序後陣列中的位置 
		    //即 第i個元素的大小順序位置 
		}
		for(int i = 1; i <= n; i++)
		{
			num[i] = vis[val[i]];//在第i個元素之前等於這個元素的個數 
			vis[val[i]]++;
		}
		sort(e1+1, e1+n+1, cmp1);//將陣列元素的位置按陣列元素值升序排序 
		sort(e2+1, e2+n+1, cmp2);//將陣列元素的位置按陣列元素值降序排序 
		memset(c, 0, sizeof(c));
		ll all1 = 0, all2 = 0;//表示所有元素之前小 和 之後小的元素之和 
		for(int i = 1; i <= n; i++)
		{
			int e = e1[i];//數值第i小的元素的位置 
			lg[e] = query(0, e-1)-num[e];//查詢原陣列第e個元素左邊比它小的元素個數(刪掉前面等於的元素) 
			update(0, e, 1);
			all1 += lg[e];
		    e = e2[i];
			rg[e] = query(1, e-1)-num[e];//查詢原陣列第e個元素右邊比它小的元素個數(刪掉前面等於的元素) 
			update(1, e, 1);
			all2 += rg[e]; 
		}
		memset(c, 0, sizeof(c));
		for(int i = 1; i <= n; i++)
		{
			int e = e1[i];
			ls[e] = query(0, n)-query(0, e);//左邊比他大的元素個數 
			update(0, e, 1);
			e = e2[i];
			rs[e] = query(1, n)-query(1, e);//右邊比他大的元素個數 
			update(1, e, 1);
		}
		ll ans = all1*all2;
		for(int i = 1; i <= n; i++)
		{
			ans -= (ll)rs[i]*rg[i]+(ll)lg[i]*ls[i]+(ll)lg[i]*rg[i]+(ll)ls[i]*rs[i];
		}
		printf("%lld\n", ans);
	}
	return 0;
}
原作者不詳,特此鳴謝!