https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=2330
Reference: "An introduction to Algorithms Classic Training Guide" Rujia P197
The book above is written on the main topic, the problem-solving ideas are written out.
I put my own in here.
AC Code:
1#include <stdio.h>2#include <string.h>3 4 #definePeople 200015 #defineVALUE 1000016 7typedefLong LongLL;8 9 intN, A[people], amin[people], amax[people], tree[value];Ten One A intLowbit (intx) { - returnX &-x; - } the - voidAddintXintd) { - while(X <VALUE) { -TREE[X] + =D; +X + =lowbit (x); - } + } A at intSumintx) { - intRET =0; - while(X >0){ -RET + =Tree[x]; -X-=lowbit (x); - } in returnret; - } to + intMain () { - intT; thescanf"%d", &t); * while(t--){ $scanf"%d", &n);Panax Notoginseng for(inti =1; I <= N; ++i) { -scanf"%d", &a[i]); the } + Amemset (Tree,0,sizeof(tree)); the for(inti =1; I <= N; ++i) { +Amin[i] = SUM (a[i]-1);//I personal statistics on the left of the first person and the total number of skill values less than -Amax[i] = i-1-Amin[i];//statistics skill value is greater than first person and total number of people on the left $Add (A[i],1); $ } - -LL ans =0; thememset (Tree,0,sizeof(tree)); - for(inti = n; I >=1; --i) {WuyiLL bmin = SUM (a[i]-1);//The total number of statistics skill values less than the first person and on the right of the first person theAns + = Bmin * Amax[i] + (N-i-bmin) *Amin[i]; -Add (A[i],1); Wu } -printf"%lld\n", ans); About } $ return 0; -}
ACM-ICPC LA 4329 Ping pong (tree-like array)