I love max and multiply
solution
# include <bits/stdc++.h>
using namespace std;
# define gcd ( a, b) __gcd ( a, b)
# define lcm ( a, b) ( 1ll * a * b / gcd ( a, b) )
# define Polynomial vector< int >
# define Inv ( x) quick_pow ( x, mod - 2 )
# define DEBUG ( x, y) cout << x << ": " << y << '\n' ;
# define mem ( a, x) memset ( a, x, sizeof a)
using ld = long double ;
using ll = long long ;
using ull = unsigned long long ;
using i16 = short ;
using ill = __int64;
const ld pi = acos ( - 1.0 ) ;
const ll mod = 998244353 , mod_g = 3 , img = 86583718 ;
inline int read ( ) {
int x = 0 , f = 1 ;
char ch = getchar ( ) ;
while ( ch < '0' || ch > '9' ) { if ( ch == '-' ) f = - 1 ; ch = getchar ( ) ; }
while ( ch >= '0' && ch <= '9' ) { x = x * 10 + ch - '0' ; ch = getchar ( ) ; }
return x * f;
}
inline ill print ( ill x) {
if ( x < 0 ) { putchar ( '-' ) ; x = - x; }
if ( x > 9 ) print ( x / 10 ) ;
putchar ( x % 10 + '0' ) ;
}
ll quick_pow ( ll ans, ll p, ll res = 1 ) {
for ( ; p; p >>= 1 , ans = ans * ans % mod) if ( p & 1 ) res = res * ans % mod;
return res % mod;
}
const int bit = 19 ;
const int mx = 1 << bit | 1 ;
ll A[ mx] , F[ mx] , B[ mx] ;
ll maxx[ 2 ] [ mx] , minn[ 2 ] [ mx] ;
void solve ( ) {
int o; cin >> o; while ( o-- ) {
int n; cin >> n;
for ( int i = 0 ; i < n; ++ i) {
cin >> A[ i] ;
maxx[ 0 ] [ i] = minn[ 0 ] [ i] = A[ i] ;
}
for ( int i = 0 ; i < n; ++ i) {
cin >> B[ i] ;
maxx[ 1 ] [ i] = minn[ 1 ] [ i] = B[ i] ;
}
for ( int j = mx; j >= 0 ; -- j) {
for ( int i = 0 ; i < bit; ++ i) {
if ( ( j | ( 1 << i) ) >= n) continue ;
maxx[ 0 ] [ j] = max ( maxx[ 0 ] [ j] , maxx[ 0 ] [ j | ( 1 << i) ] ) ;
maxx[ 1 ] [ j] = max ( maxx[ 1 ] [ j] , maxx[ 1 ] [ j | ( 1 << i) ] ) ;
minn[ 0 ] [ j] = min ( minn[ 0 ] [ j] , minn[ 0 ] [ j | ( 1 << i) ] ) ;
minn[ 1 ] [ j] = min ( minn[ 1 ] [ j] , minn[ 1 ] [ j | ( 1 << i) ] ) ;
}
}
ll maxxx = - 2e18 , res = 0 ;
for ( int i = n - 1 ; i >= 0 ; -- i) {
maxxx = max ( maxxx, maxx[ 0 ] [ i] * maxx[ 1 ] [ i] ) ;
maxxx = max ( maxxx, maxx[ 0 ] [ i] * minn[ 1 ] [ i] ) ;
maxxx = max ( maxxx, minn[ 0 ] [ i] * maxx[ 1 ] [ i] ) ;
maxxx = max ( maxxx, minn[ 0 ] [ i] * minn[ 1 ] [ i] ) ;
res = ( res + maxxx) % mod;
while ( res < 0 ) res += mod;
}
cout << res << '\n' ;
}
}
signed main ( ) {
ios_base:: sync_with_stdio ( false ) ;
cin. tie ( 0 ) ;
cout. tie ( 0 ) ;
# ifdef ACM_LOCAL
freopen ( "input" , "r" , stdin ) ;
signed test_index_for_debug = 1 ;
char acm_local_for_debug = 0 ;
do {
if ( acm_local_for_debug == '$' ) exit ( 0 ) ;
if ( test_index_for_debug > 20 )
throw runtime_error ( "Check the stdin!!!" ) ;
auto start_clock_for_debug = clock ( ) ;
solve ( ) ;
auto end_clock_for_debug = clock ( ) ;
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double ( end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while ( cin >> acm_local_for_debug && cin. putback ( acm_local_for_debug) ) ;
# else
solve ( ) ;
# endif
return 0 ;
}