Transmission Door
The tree-like array for reverse order + discretization?
I never seem to remember how to use a tree-like array to reverse the order. Before I remember all is writing, this time how to write to be able to go backwards ...
Then the scope of the subject A is very large, but the range of n is not small, 500000, need to be a discrete wave, then can be normal according to the tree array operation. And then, in query, pay attention to-1.
Let's take a look at the code.
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<cstring>#include<utility>#include<map>#definePR pair<int,int>#defineMP Make_pair#defineFi first#defineSC Second#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')#defineLowbit (x) x & (-X)using namespaceStd;typedefLong Longll;Const intM =500005;Const intN =32005;intRead () {intAns =0, op =1; CharCH =GetChar (); while(Ch <'0'|| CH >'9') { if(ch = ='-') op =-1; CH=GetChar (); } while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); } returnAns *op;}intn,a[m],c[m],tot,d[m];ll ans;voidAddintx) { while(x <= m5) A[x]++,x + =lowbit (x);}intQueryintx) { intCur =0; while(x) cur + = a[x],x-=lowbit (x); returncur;}voidClear () {tot= ans =0; Memset (A,0,sizeof(a));}intMain () { while(1) {n=read (); if(n = =0) Break; Clear (); Rep (I,1, n) c[i] = Read (), d[i] =C[i]; Sort (c+1, c+1+N); Tot= Unique (c+1, c+1+N)-C-1; Per (I,n,1) { intK = Lower_bound (c+1, c+1+tot,d[i])-C; //printf ("#%d\n", k);Ans + = query (K-1), add (k); } printf ("%lld\n", ans); } return 0;}
POJ2299 Ultra-quicksort