Question address: http://poj.org/problem? Id = 3270
Note that some of the requirements_ Int64, The other part is ignored (think it is impossibleInt). ResultWaNowNLong unknown reason.
This is a classic question of reverse order. Beginners. There is one more State than the basic number of reverse orders. That is, you need to write one more query.
# Include <iostream> # include <cstdio> using namespace STD ;__ int64 tree [100100] ;__ int64 num [100100]; int n ;__ int64 s [100100]; __int64 query (_ int64 Val) {__ int64 sum = 0; while (Val> 0) {sum + = tree [Val]; Val-= Val &-val ;} return sum; }__ int64 query2 (_ int64 Val) {__ int64 sum = 0; while (Val> 0) {sum + = num [Val]; val-= Val &-val;} return sum;} void Update (INT Val) {__ int64 temp = val; while (Val <= N) {tree [Val] + = temp; num [Val] ++; Val + = Val &-V Al ;}} int main () {int I ;__ int64 sum = 0; while (~ Scanf ("% d", & N) {sum = 0; for (I = 1; I <= N; I ++) tree [I] = num [I] = 0; for (I = 1; I <= N; I ++) scanf ("% i64d ", & S [I]); for (I = N; I> = 1; I --) {sum + = query (s [I]-1) + query2 (s [I]-1) * s [I]; // printf ("% d \ n", s [I]); update (s [I]); // cout <"Tree" <":"; for (Int J = 1; j <= N; j ++) printf ("% 3i64d", tree [J]); cout <Endl; // cout <"num" <":"; for (Int J = 1; j <= N; j ++) printf ("% 3d", num [J]); cout <Endl ;}printf ("% i64d \ n ", sum);} return 0 ;}