World is exploding tree-like array + discretization

Source: Internet
Author: User
Tags cmath

Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:A≠B≠C≠D,1≤A<B≤N,1≤C<D≤N,Aa <ab,A c >ad A≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,aa<ab,ac>ad.

Inputthe input consists of multiple test cases.
Each test case is begin with a integer n

The next line contains n integersa1,a2? An A1,a2?an.
1≤n≤50000 1≤n≤50000
0 & #x2264; A i & #x2264; 1 e 9 " > 0≤ ai≤1e9 0≤ai≤1e9 Outputfor each test case,output a line contains an integer. Sample Input

42 4 1 341 2 3 4

Sample Output

10

Because the relative size relationship is considered, the data is discretized first, then the elements that are larger than I or smaller than I are recorded in a tree-like array, all the numbers are calculated, the overlapping is discussed, and the coincident elements are subtracted.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<sstream>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<map>#include<stack>#include<fstream>#include<Set>#include<memory>#include<bitset>#include<string>#include<functional>using namespaceStd;typedefLong LongLL;#defineMAXN 500009LL PRE_MAX[MAXN], PRE_MIN[MAXN], POST_MAX[MAXN], POST_MIN[MAXN]; LL A[MAXN], TMP[MAXN]; LL T[MAXN], N; ll Lowbit (ll x) {returnx& (-x);}voidUpdate (LL x) { while(x <=MAXN) {T[x]+=1; X+=lowbit (x); }}ll Getsum (ll x) {ll sum=0;  while(X >0) {sum+=T[x]; X-=lowbit (x); }    returnsum;}intMain () { while(SCANF ("%lld", &n)! =EOF) {memset (T,0,sizeof(T));  for(LL i =1; I <= N; i++) scanf ("%lld", &A[i]); memcpy (TMP, A,sizeof(a)); Sort (tmp+1, TMP + n +1); LL Len= Unique (tmp +1, TMP + n +1) -tmp;  for(LL i =1; I <= N; i++) A[i]= Lower_bound (tmp +1, TMP + len +1, A[i])-tmp;  for(LL i =1; I <= N; i++) {update (a[i]); Pre_min[i]= Getsum (A[i]-1); Pre_max[i]= i-getsum (A[i]);        } LL sum1, sum2; Sum1= Sum2 =0;  for(inti =1; I <= N; i++) {Post_min[i]= Getsum (A[i]-1) -Pre_min[i]; Post_max[i]= N-getsum (A[i])-Pre_max[i]; Sum1+=Post_min[i]; Sum2+=Post_max[i]; } LL ans= sum1*sum2;  for(inti =1; I <= N; i++) {ans-= pre_min[i] *Pre_max[i]; Ans-= pre_min[i] *Post_min[i]; Ans-= post_max[i] *Pre_max[i]; Ans-= post_max[i] *Post_min[i]; } printf ("%lld\n", ans); }}

World is exploding tree-like array + discretization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.