QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#376064 | #6631. Maximum Bitwise OR | 369Pai | TL | 498ms | 66688kb | C++14 | 2.3kb | 2024-04-03 20:14:59 | 2024-04-03 20:14:59 |
Judging History
answer
#include<bits/stdc++.h>
#define lc(p) ((p) * 2)
#define rc(p) ((p) * 2 + 1)
using namespace std;
typedef long long ll;
const int N = 1e5 + 5 , T = N * 4 , LG = 30;
int n , q , a[N];
struct Info
{
int mx = 0 , s = 0 , ans = 0 , cnt[LG] = {};
vector<int>v;
}tr[T];
ostream& operator << (ostream &os , const Info &a)
{
os << a.mx << ' ' << a.s << ' ' << a.ans << "; ";
for(int i = 0 ; (1ll << i) < a.mx ; i++)
os << a.cnt[i] << " ";
os << "; ";
for(int x : a.v)os << x << " ";
return os;
}
Info operator + (const Info &a , const Info &b)
{
Info c; int s1 = 0 , s0 = 0;
c.s = a.s | b.s , c.mx = a.mx | b.mx;
c.ans = min(a.ans , b.ans);
if(c.s == c.mx)c.ans = 0;
for(int i = 0 ; i < LG ; i++)
{
c.cnt[i] = a.cnt[i] + b.cnt[i];
if(!c.cnt[i])s0 |= 1 << i;
if(c.cnt[i] == 1)s1 |= 1 << i;
}
auto chk = [&](int x)
{
if(!(x & s1))
{
if(c.ans <= 1)return ;
for(int i = 0 ; i < LG ; i++)
{
int y = x ^ (x - (1 << i));
if((s0 & y) == s0)
{
c.ans = min(c.ans , 1);
break ;
}
}
}
else c.v.push_back(x);
};
for(int x : a.v)chk(x);
for(int x : b.v)chk(x);
// cerr << "Merge " << a << "\n+\n" << b << "\n";
// cerr << "=\n" << c << "\n";
return c;
}
void Pushup(int p){tr[p] = tr[lc(p)] + tr[rc(p)];}
void Build(int l , int r , int p)
{
if(l == r)
{
tr[p].mx = (1ll << (64 - __builtin_clzll(a[l]))) - 1;
tr[p].s = a[l] , tr[p].ans = 2;
for(int i = 0 ; i < LG ; i++)
tr[p].cnt[i] = (a[l] >> i) & 1;
if(a[l])tr[p].v.push_back(a[l]);
return ;
}
int mid = (l + r) >> 1;
Build(l , mid , lc(p));
Build(mid + 1 , r , rc(p));
Pushup(p);
}
Info Query(int s , int e , int l = 1 , int r = n , int p = 1)
{
if(s <= l && r <= e)return tr[p];
int mid = (l + r) >> 1;
if(s <= mid && mid < e)return Query(s , e , l , mid , lc(p)) + Query(s , e , mid + 1 , r , rc(p));
else if(s <= mid)return Query(s , e , l , mid , lc(p));
else return Query(s , e , mid + 1 , r , rc(p));
}
int Solve()
{
cin >> n >> q;
for(int i = 1 ; i <= n ; i++)cin >> a[i];
Build(1 , n , 1);
while(q--)
{
int l , r; cin >> l >> r;
Info ans = Query(l , r);
cout << ans.mx << ' ' << ans.ans << "\n";
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int T; cin >> T;
while(T--)Solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 12ms
memory: 66108kb
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: 0
Accepted
time: 498ms
memory: 66688kb
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:
1073741823 2 268435455 2 536870911 2 1073741823 2 536870911 2 1073741823 2 536870911 2 1073741823 2 268435455 2 268435455 2 536870911 2 67108863 2 134217727 2 1073741823 2 536870911 2 536870911 2 268435455 2 536870911 2 536870911 2 536870911 2 268435455 2 268435455 2 1073741823 2 16777215 2 10737418...
result:
ok 200000 numbers
Test #3:
score: -100
Time Limit Exceeded
input:
50000 2 2 924896435 917026400 1 2 1 2 2 2 322948517 499114106 1 2 2 2 2 2 152908571 242548777 1 1 1 2 2 2 636974385 763173214 1 2 1 1 2 2 164965132 862298613 1 1 1 2 2 2 315078033 401694789 1 2 1 2 2 2 961358343 969300127 2 2 1 2 2 2 500628228 28065329 1 2 1 2 2 2 862229381 863649944 1 2 2 2 2 2 541...
output:
1073741823 2 1073741823 2 536870911 2 536870911 2 268435455 2 268435455 2 1073741823 2 1073741823 2 268435455 2 1073741823 2 536870911 2 536870911 2 1073741823 2 1073741823 2 536870911 2 536870911 2 1073741823 2 1073741823 2 1073741823 2 268435455 2 536870911 2 536870911 2 1073741823 2 1073741823 2 ...