Use this question to test the effects of various practices ~
This is a question entitled "ACM2008" on the Beijing site. It is between simple and moderate. It is not enough for the penalty. Therefore, such a question must be answered.
After adding (a [I] +) is processed in this way, sum (a [I]) is used to calculate a smaller number than a [I. For more information, see the tree chart.
Tree array exceptions ~
The change of the tree array after add (a [I], 1) may be very complicated ..
Hmm !! Understand.
If you change to add (a [I], 1), the result is converted to sum (a [I])-1; this is equivalent to adding 1 at a [I, the following is equivalent to add (a [I] + ~~, However, the time consumption will increase by 30 + MS.
# Include <iostream> # include <stdio. h> # include <string. h> using namespace std; const int maxn = 100005; long c [maxn]; int a [20005]; long temp [maxn]; int lowbit (int x) {return x &-x;} void add (int x, int y) {while (x <= maxn) {c [x] + = y; x + = lowbit (x) ;}long long sum (int x) {long ret = 0; while (x> 0) {ret + = c [x]; x-= lowbit (x);} return ret;} int main () {int case_num; scanf ("% d", & case_nu M); while (case_num --) {memset (c, 0, sizeof (c); int n; scanf ("% d", & n ); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]); add (a [I] +); // try an array with only 0 values and the result is similar, replace + 1 with + x [I] temp [I] = sum (a [I]); // This place minus c [x ..} Long ans = 0; for (int I = 1; I <= n; I ++) printf ("% d", sum (a [I]); printf ("\ n"); for (int I = 2; I <n; I ++) {ans + = temp [I] * (n-I-(sum (a [I])-temp [I]); // In fact the first sum (a [I])-temp [I] is not quite sure ans + = (i-1-temp [I]) * (sum (a [I]) -temp [I]); // my sum (a [I]) actually includes itself (not included)} printf ("% lld \ n", ans );} return 0 ;}