QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135367 | #6631. Maximum Bitwise OR | nameless_story | TL | 1ms | 3604kb | C++20 | 2.3kb | 2023-08-05 14:06:13 | 2023-08-05 14:06:17 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
bool flg[30][30];
#define all(x) (x).begin(),(x).end()
void solve()
{
int n,m,i,j,k,l; cin>>n>>m;
vector<int> a(n+1);
for (i=1; i<=n; i++) cin>>a[i];
vector cnt(30,vector<int>(n+1));
vector dp(30,vector(30,vector<int>(n+1)));
vector<vector<pair<int,int>>> seg(n+1);
vector<vector<int>> pos(30);
vector<vector<vector<int>>> sum(30);
for (i=0; i<30; i++) sum[i].resize(i+1,vector<int>(n+1));
for (i=1; i<=n; i++)
{
for (j=0; j<30; j++) if (a[i]>>j&1)
{
++cnt[j][i];
pos[j].push_back(i);
}
memset(flg,0,sizeof flg);
for (j=0; j<30; j++) if (0==(a[i]>>j&1))
{
k=j;
while (k<30&&0==(a[i]>>k&1)) ++k;
if (k==30) break;
seg[i].push_back({j,k-1});
for (l=j; l<k; l++)
{
flg[j][l]=1;
}
j=k;
}
// cerr<<"?\n";
for (j=1; j<30; j++) for (k=j; k<30; k++) flg[j][k]|=flg[j-1][k];
for (j=0; j<30; j++) for (k=j; k<30; k++) sum[k][j][i]=sum[k][j][i-1]+flg[j][k];
}
for (j=0; j<30; j++) for (i=2; i<=n; i++) cnt[j][i]+=cnt[j][i-1];
while (m--)
{
int l,r,q;
cin>>l>>r; --l;
for (i=29; i>=0; i--) if (cnt[i][r]!=cnt[i][l]) break;
q=i+1;
if (q==0) { cout<<"0 0\n"; continue; }
for (i=0; i<q; i++) if (cnt[i][r]==cnt[i][l]) break;
cout<<(1<<q)-1<<' ';
if (i==q) { cout<<"0\n"; continue; }
int L=i;
for (i=q-1; i>=0; i--) if (cnt[i][r]==cnt[i][l]) break;
int R=i;
// cerr<<"?\n";
vector<int> id;
// cerr<<L<<' '<<R<<endl;
// for (i=0; i<4; i++) cerr<<cnt[i][l]<<' '<<cnt[i][r]<<endl;
for (i=L; i<=R; i++) if (cnt[i][r]==cnt[i][l]+1)
{
// cerr<<*lower_bound(all(pos[i]),l+1)<<endl;
id.push_back(*lower_bound(all(pos[i]),l+1));
}
sort(all(id)); id.resize(unique(all(id))-id.begin());
int ans=sum[R][L][r]-sum[R][L][l];
// cerr<<ans<<endl;
for (int x:id)
{
// cerr<<"x = "<<x<<endl;
int cc=0;
for (i=0; i<30; i++) cc+=(a[x]>>i&1)&&cnt[i][r]-cnt[i][l]==1;
for (auto [ll,rr]:seg[x]) if (ll<=L&&R<=rr)
{
if (rr+1<30&&cc-(cnt[rr+1][r]-cnt[rr+1][l]==1)==0) ans=1e9;
else ans-=seg[x].size()>1;
cerr<<ll<<' '<<rr<<endl;
}
}
if (ans==0) cout<<"2\n"; else cout<<"1\n";
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int T; cin>>T;
while (T--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3604kb
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
Time Limit Exceeded
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...