#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> usingnamespace std; #define int long long intf(int x){ int res = 0, base = 1; while (x) res += x * (x + 1) / 2 * base, base <<= 1, x >>= 1; return res; } intcalc(int x){ int res = 0; while (x) res += x, x >>= 1; return res; } int n; signedmain(){ int T;cin >> T; while (T--) { cin >> n; int l = 1, r = 2000000000, ans; while (l <= r) { int m = (l + r) >> 1; if ( f(m - 1) < n ) ans = m, l = m + 1; else r = m - 1; } cout << calc(ans - 1) + (n - f(ans - 1)) / ans << endl; } return0; }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> usingnamespace std; #define int long long #define MAXN 300006 int n; int A[MAXN] , res; structBIT { int T[MAXN]; voidadd( int x , int c ){ while( x < MAXN ) T[x] += c , x += ( x & -x ); } intsum( int x ){ int ret = 0; while( x > 0 ) ret += T[x] , x -= ( x & -x ); return ret; } intget( int x ){ returnsum( MAXN - 1 ) - sum( x ); } } lef , rig; signedmain(){ cin >> n; for( int i = 1 ; i <= n ; ++ i ) scanf("%lld",&A[i]) , rig.add( A[i] , 1 ); for( int i = 1 ; i <= n ; ++ i ) { rig.add( A[i] , -1 ); res += min( rig.get( A[i] ) , lef.get( A[i] ) ); lef.add( A[i] , 1 ); } cout << res << endl; }