QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#127122 | #6631. Maximum Bitwise OR | Ethan_xu# | WA | 80ms | 3508kb | C++20 | 4.2kb | 2023-07-19 13:15:40 | 2023-07-19 13:15:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 1e5 + 10 ;
int n ;
int a[maxn] ;
array<int , 30> mx[maxn << 2] ;
int ls(int id)
{
return id << 1 ;
}
int rs(int id)
{
return id << 1 | 1 ;
}
void build(int id , int l , int r)
{
for(int i = 0 ; i < 30 ; i ++) mx[id][i] = 0 ;
if(l == r)
{
mx[id][0] = a[l] ;
return ;
}
int mid = (l + r) / 2 ;
build(ls(id) , l , mid) ;
build(rs(id) , mid + 1 , r) ;
int cur1 = 0 , cur2 = 0 ;
for(int i = 0 ; i < 30 ; i ++)
{
if(cur2 == 30) mx[id][i] = mx[ls(id)][cur1 ++] ;
else if(cur1 == 30) mx[id][i] = mx[rs(id)][cur2 ++] ;
else
{
if(mx[ls(id)][cur1] > mx[rs(id)][cur2]) mx[id][i] = mx[ls(id)][cur1 ++] ;
else mx[id][i] = mx[rs(id)][cur2 ++] ;
}
}
}
array<int , 30> merge(const array<int , 30> &x , const array<int , 30> &y)
{
array<int , 30> res ;
for(int i = 0 ; i < 30 ; i ++) res[i] = 0 ;
int cur1 = 0 , cur2 = 0 ;
for(int i = 0 ; i < 30 ; i ++)
{
if(cur2 == 30) res[i] = x[cur1 ++] ;
else if(cur1 == 30) res[i] = y[cur2 ++] ;
else
{
if(x[cur1] > y[cur2]) res[i] = x[cur1 ++] ;
else res[i] = y[cur2 ++] ;
}
}
return res ;
}
array<int , 30> query(int id , int l , int r , int x , int y)
{
if(x <= l && r <= y) return mx[id] ;
int mid = (l + r) / 2 ;
array<int , 30> res ;
for(int i = 0 ; i < 30 ; i ++) res[i] = 0 ;
if(x <= mid) res = merge(res , query(ls(id) , l , mid , x , y)) ;
if(mid < y) res = merge(res , query(rs(id) , mid + 1 , r , x , y)) ;
return res ;
}
int main()
{
ios::sync_with_stdio(false) ;
cin.tie(0) ;
int T ;
cin >> T ;
while(T --)
{
cin >> n ;
int q ;
cin >> q ;
for(int i = 1 ; i <= n ; i ++) cin >> a[i] ;
vector<vector<int>> pre(n + 1 , vector<int>(30 , 0)) ;
for(int i = 1 ; i <= n ; i ++)
for(int j = 0 ; j < 30 ; j ++)
pre[i][j] += pre[i - 1][j] + ((a[i] >> j) & 1) ;
build(1 , 1 , n) ;
while(q --)
{
int l , r ;
cin >> l >> r ;
array<int , 30> t = query(1 , 1 , n , l , r) ;
vector<int> cnt(30 , 0) ;
int res = 0 ;
int num = 30 ;
for(int i = 0 ; i < 30 ; i ++)
{
for(int j = 0 ; j < 30 ; j ++)
if((t[i] >> j) & 1) cnt[j] += 1 ;
}
for(int i = 29 ; i >= 0 ; i --)
if(cnt[i] == 1) res |= (1 << i) ;
else if(cnt[i] >= 2) res |= (1 << i) | ((1 << i) - 1) ;
auto go = [&](int j)
{
int c = 30 ;
for(int i = 0 ; i < 30 ; i ++)
if((a[i] >> j) & 1)
{
int mask = (1 << j) - 1 ;
int cc = __builtin_popcount(a[i] & mask) ;
c = min(c , cc + 1) ;
}
return c ;
} ;
int mn = 30 ;
for(int i = 29 ; i >= 0 ; i --)
if(cnt[i] == 0)
mn = i ;
int lst = -1 ;
int f = 0 ;
for(int i = 29 ; i > mn ; i --)
{
if(cnt[i] >= 2)
{
f = 1 ;
if(lst == -1 || lst + 1 == i) num = min(num , go(i)) , lst = i ;
else break ;
}
}
int rr = 0 ;
for(int i = 0 ; i < 30 ; i ++)
if(pre[r][i] - pre[l - 1][i])
rr |= (1 << i) ;
if(rr == res)
{
cout << rr << ' ' << 0 << '\n' ;
continue ;
}
cout << res << ' ' << num * f << '\n' ;
}
}
return 0 ;
}
/*
1
3 1
10 10 5
1 2
1
2 1
10 10
1 2
1
2 1
2 2
1 2
1
32 1
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
4 4 4 4 4
2 1
1 32
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3404kb
input:
1 3 2 10 10 5 1 2 1 3
output:
15 2 15 0
result:
ok 4 number(s): "15 2 15 0"
Test #2:
score: -100
Wrong Answer
time: 80ms
memory: 3508kb
input:
100000 1 1 924704060 1 1 1 1 149840457 1 1 1 1 515267304 1 1 1 1 635378394 1 1 1 1 416239424 1 1 1 1 960156404 1 1 1 1 431278082 1 1 1 1 629009153 1 1 1 1 140374311 1 1 1 1 245014761 1 1 1 1 445512399 1 1 1 1 43894730 1 1 1 1 129731646 1 1 1 1 711065534 1 1 1 1 322643984 1 1 1 1 482420443 1 1 1 1 20...
output:
924704060 0 149840457 0 515267304 0 635378394 0 416239424 0 960156404 0 431278082 0 629009153 0 140374311 0 245014761 0 445512399 0 43894730 0 129731646 0 711065534 0 322643984 0 482420443 0 202661591 0 529979773 0 520572847 0 500838890 0 224446016 0 228171383 0 973333717 0 8493236 0 622547486 0 677...
result:
wrong answer 1st numbers differ - expected: '1073741823', found: '924704060'