Tree-like array LA 4329 Asian Games Beijing Division

Source: Internet
Author: User

Review the tree-like array

It's a pretty interesting question:

Http:// problem&problem=4174

Learn a few things:
1. Construction of tree-like array c[i], a c[i]=s[i]-s[i-lowbit (i)]; This is a practice that has been used. Now learn a new, direct add (I,a[i]), and (S[i] for a[1] to A[i)

2, prefixes and ideas, the sum of the tree array itself is based on the idea of prefix and. The number of numbers smaller than a certain number, by opening a large number of space + suffix array, efficient statistics out of a number of small number

3, in fact, I think through this question. The ability to make an O (nlogn) sorting algorithm. Of course, the place that is not good is only can be whole number, but should be able to solve with vector+map?

Stick to your own code first. , because the code behind the LRJ Daniel is too concise ...

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Queue> #include <functional>using namespace std; #define MAXN 20010#define SIZE 100015int a[maxn],sma[size],c[ Size],s[size],e[size],tot[size];int n,mmax;int lowbit (int i) {return I & (-i);}    int sum (int i) {int ans=0;    for (; I>0;i-=lowbit (i)) ans+=c[i];  return ans;}    void Add (int x, int d) {while (x <= SIZE) {c[x] + = D; x + = Lowbit (x);        }}void Init () {memset (c,0,sizeof (c));        memset (A,0,sizeof (a));        memset (sma,0,sizeof (SMA));        Memset (s,0,sizeof (s));        memset (E,0,sizeof (e)); memset (tot,0,sizeof (TOT));}    int main () {//freopen ("La4329.txt", "R", stdin);    int t;    Long long ans;    scanf ("%d", &t);        while (t--) {mmax=0;        ans=0;        Init ();        scanf ("%d", &n);            for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);      Mmax=max (Mmax,a[i]);      Add (a[i],1);            C[i]=sum (a[i]-1);            E[a[i]]=1;        Sma[a[i]]=sum (a[i]-1);        } memset (C,0,sizeof (c));            for (int i=1;i<=mmax;i++) {s[i] =s[i-1]+e[i];            C[i]=s[i]-s[i-lowbit (i)];        Tot[i]=sum (i-1); }/////////////////////////for (int i=1;i<=n;i++)//{//printf ("sma[a[%d]" =%d tot (%d        ) =%d\n ", I,sma[a[i]],a[i],tot[a[i]]); }///////////////////////for (int i=1;i<=n;i++) {int tmp = Tot[a[i]]-sma[a[i]];/*a[i        ] The number of smaller than a[i] */ans+= (Long Long) tmp* (I-1-sma[a[i]) + (Long Long) sma[a[i]]* (n-i-tmp);    } printf ("%lld\n", ans); } return 0;}

Standard process

LA4329 Ping pong//Rujia liu#include<cstdio> #include <vector>using namespace std;//inline int lowbit (int x {return x& (x^ (x-1));} inline int lowbit (int x) {return x&-x;}  struct Fenwicktree {int n;  Vector<int> C; void Resize (int n) {this->n = n; C.resize (n);  } void Clear () {Fill (C.begin (), C.end (), 0);} // ????    A[1]+a[2]+...+a[x] (x<=n) int sum (int x) {int ret = 0;    while (x > 0) {ret + = c[x]; x-= Lowbit (x);  } return ret;    }//a[x] + = d (1<=x<=n) void Add (int x, int d) {while (x <= N) {c[x] + = D; x + = Lowbit (x); }}};const int maxn = 20000 + 5;int N, A[maxn], C[MAXN], D[MAXN];  Fenwicktree F;int Main () {freopen ("La4329.txt", "R", stdin);  int T;  scanf ("%d", &t);    while (t--) {scanf ("%d", &n);    int Maxa = 0;    for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]), Maxa = Max (Maxa, A[i]);}    F.resize (MAXA);    F.clear ();    for (int i = 1; I <= n; i++) {F.add (a[i], 1);  C[i] = f.sum (a[i]-1);    } f.clear ();      for (int i = n; I >= 1; i--) {F.add (a[i], 1);    D[i] = f.sum (a[i]-1);    } long long ans = 0;    for (int i = 1; I <= n; i++) ans + = (long Long) c[i]* (N-i-d[i]) + (Long Long) (i-c[i]-1) *d[i];  printf ("%lld\n", ans); } return 0;}

Tree-like array LA 4329 Asian Games Beijing Division

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: 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.