Topic Links:
World is exploding
Time limit:2000/1000 MS (java/others)
Memory limit:65536/65536 K (java/others)
Problem Descriptiongiven 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,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 .
1≤n≤50000
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 Test Instructions: asked to meet the question of the four-tuple how many; ideas: tolerance, first calculate a,b,c,d meet Aa<ab&&ac<ad number, Minus the number of a==c,a==d,b==c,b==d, is the answer, because it is impossible to have two equal appearance, and then is to use a tree-like array to find Pres[i],preb[i],nexs[i],nexb[i], respectively, the number of the first is smaller than it, larger than it, Later than it is smaller than its large number of specific look at the code bar; &NBSP;AC Code:
/************************************************┆┏┓┏┓┆┆┏┛┻━━━┛┻┓┆┆┃┃┆┆┃━┃┆┆┃┳┛┗┳┃┆┆┃┃ ┆┆┃┻┃┆┆┗━┓┏━┛┆┆┃┃┆┆┃┗━━━┓┆┆┃ac Horse ┣┓┆┆┃┏┛┆┆┗┓┓┏━┳┓┏┛┆┆┃┫┫┃┫┫┆ ┆┗┻┛┗┻┛┆************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include < Stack> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL; Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E9;CONST int N=5e4+10;const int Maxn=1e3+14;const double eps= 1e-8;int n,fa[n],pres[n],preb[n],nexs[n],nexb[n],sum[n];struct node{int a,id;} Po[n];int CMP (node X,node y) {if (X.A==Y.A) return x.id<y.id; return x.a<y.a;} int CMP1 (node X,node y) {if (X.A==Y.A) return x.id<y.id; return x.a>y.a;} int lowbit (int x) {return x& (-X);} inline void Update (int x) {while (x<=n) {sum[x]++; X+=lowbit (x); }}int query (int x) {int s=0; while (x) {s+=sum[x]; X-=lowbit (x); } return s;} int main () {while (scanf ("%d", &n)!=eof) {for (i,1,n) read (PO[I].A), po[i].id=i; Sort (po+1,po+n+1,cmp); Po[0].a=-1; MST (sum,0); for (I,1,n) {if (PO[I].A==PO[I-1].A) fa[i]=fa[i-1]; else fa[i]=i; Pres[po[i].id]=query (po[i].id)-(i-fa[i]); Nexs[po[i].id]=fa[i]-1-pres[po[i].id]; Update (po[i].id); } MST (sum,0); Sort (PO+1,PO+N+1,CMP1); for (I,1,n) {if (PO[I].A==PO[I-1].A) fa[i]=fa[i-1]; else fa[i]=i; Preb[po[i].id]=query (po[i].id)-(i-fa[i]); Nexb[po[i].id]=fa[i]-1-preb[po[i].id]; Update (po[i].id); } sort (PO+1,PO+N+1,CMP1); LL Ans1=0,ans2=0,ans; for (I,1,n) {ans1=ans1+pres[i]; Ans2=ans2+preb[i]; } ans=ans1*ans2; for (i,1,n) {ans=ans-nexs[i]*nexb[i];//a==c Ans=ans-preb[i]*nexb[i];//a==d Ans=ans-pres[i]*nexs[i];//b==c Ans=ans-pres[i]*preb[i];//b==d} cou t<<ans<<endl; } return 0;}
hdu-5792 World is exploding (tolerant + tree array)