Reverse order to
——! x^n+y^n=z^n
can get here [Luogu]:
https://www.luogu.org/problem/show?pid=1908
Test instructions: For a given sequence of positive integers, the inverse pair is the ordered pair of Ai>aj and i<j in the sequence. Knowing the concept, they compete on who first calculates the number of reverse pairs in a given sequence of positive integers.
If these numbers are:
8 2 3) 1 7
If we add a number to each one, using an array of a[i] statistics I appears several times.
Initial state of A:
8 after adding in:
Since there are no more than 8 large numbers, the explanation does not produce reverse
2 after adding in:
Statistics are larger than 2 (i.e. in reverse order), there is a
3 after adding in:
Reverse to +1
...
Perhaps everyone can see, that is, each time to join the statistics before the number of a few larger than their own, that is, the number of statistics behind a few, this kind of thing is of course a tree-like array operation is very convenient.
In fact, if you think about it, if I turn these numbers upside down, then we just want to count the number of numbers in front of us, which is not the same as a tree array operation?
Of course, in carrying out the above operation may see the problem, that is 4,5,6 these are useless, so not only time-consuming and cost space.
Actually the number above and
5 2 3 1 4 The reverse logarithm is the same, so our operation is perfect. (Of course you have to have a few numbers in the same situation)
Attach the Lowbee code
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6 #defineMAXN 1000107 structlys{8 intpos,v;9 }P[MAXN];Ten intBIT[MAXN],A[MAXN]; OneInlineintread (); A namespaceliisa{ - intN; - intLowbit (intx) { the returnx& (-x); - } - BOOLcmpConstLys &a,ConstLys &b) { - returna.v<B.V; + } - //inserted, or understood as a single point of update + voidInsertintx) { A inti; at for(i=x;i<=n;i+=lowbit (i)) -bit[i]++; - } - //Statistics and - Long LongGetsum (intx) { - inti; in Long Longres=0; - for(i=x;i>0; i-=lowbit (i)) tores+=Bit[i]; + returnRes; - } the Long Longans=0; * intMain () { $n=read ();Panax Notoginseng inti; - for(i=1; i<=n;i++){ theP[i].pos=i; p[i].v=read (); + } ASort (p+1, p+1+n,cmp); the ints=0; +p[0].v=-1; - //discretization Operations $ for(i=1; i<=n;i++){ $ if(p[i].v!=p[i-1].V) s++; -a[p[i].pos]=s; - } the //Calculate reverse order, read from backwards - for(i=n;i>=1; i--){Wuyi Insert (A[i]); the //The statistics are smaller than their own -Ans+=getsum (a[i]-1); Wu } -cout<<ans<<Endl; About } $ } - intMain () { - Liisa::main (); - return 0; A } +InlineintRead () { the intk=0, f=1; - CharC=GetChar (); $ while(c>'9'|| c<'0'){ the if(c=='-') f=-1; theC=GetChar (); the } the while(c>='0'&&c<='9'){ -k=k*Ten+c-'0'; inC=GetChar (); the } the returnk*F; About}
If there is any mistake, please correct me.
Using a tree-like array to process inverse pairs of [data structures] [tree arrays]