QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135471 | #6631. Maximum Bitwise OR | PhantomThreshold | TL | 2ms | 9684kb | C++20 | 3.0kb | 2023-08-05 15:55:44 | 2023-08-05 15:55:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
inline int lowbit(int x){
return x&(-x);
}
const int maxL=20;
const int maxn=100000;
int T,n,Q;
int dp[maxL+5][maxn+50];
int LG[(1<<maxL)+50];
struct osc{
int l,r,id,ans1,ans2,len;
}q[maxn+50];
int a[maxn+50];
void build(){
for (int i=1;i<=n;i++) dp[0][i]=a[i];
for (int j=1;j<maxL;j++){
for (int i=1;i+(1<<j)<=n+1;i++){
dp[j][i]=dp[j-1][i]|dp[j-1][i+(1<<(j-1))];
}
}
for (int i=0;i<maxL;i++){
for (int j=1<<i;j<1<<(i+1);j++) LG[j]=i;
}
}
int query(int l,int r){
int L=LG[r-l+1];
int ans=dp[L][l]|dp[L][r-(1<<L)+1];
return ans;
}
void update(int &x,int y){
x=max(x,y);
}
int main(){
ios_base::sync_with_stdio(false);
cin >> T;
for (;T--;){
cin >> n >> Q;
for (int i=1;i<=n;i++) cin >> a[i];
build();
/*
cerr << "---" << endl;
for (int j=0;j<=3;j++){
for (int i=1;i+(1<<j)<=n+1;i++){
cerr << dp[j][i] << " ";
}
cerr << endl;
}
cerr << "---" << endl;
for (int i=1;i<=10;i++) cerr << LG[i] << " ";
cerr << endl;
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++) cerr << query(i,j) << " ";
cerr << endl;
}
*/
vector<vector<pii>> pre(n+2);
pre[0].emplace_back(0,1);
for (int i=1;i<=n;i++){
pre[i].emplace_back(0LL,i+1);
for (auto [x,ind]:pre[i-1]){
x=x|a[i];
if (x==pre[i].back().first) continue;
pre[i].emplace_back(x,ind);
}
}
vector<vector<pii>> suf(n+2);
suf[n+1].emplace_back(0,n);
for (int i=n;i>=1;i--){
suf[i].emplace_back(0LL,i-1);
for (auto [x,ind]:suf[i+1]){
x=x|a[i];
if (x==suf[i].back().first) continue;
suf[i].emplace_back(x,ind);
}
}
/*
cerr << "-----------------" << endl;
for (int i=1;i<=n;i++){
for (auto [x,ind]:pre[i]) cerr << "(" << x << "," << ind << ") ";
cerr << endl;
}
cerr << "-----------------" << endl;
for (int i=1;i<=n;i++){
for (auto [x,ind]:suf[i]) cerr << "(" << x << "," << ind << ") ";
cerr << endl;
}
*/
vector<vector<int>> List(n+2);
for (auto &e:List) e.resize(40);
for (int i=1;i<=n;i++){
for (auto [vl,l]:pre[i-1]){
for (auto [vr,r]:suf[i+1]){
int v=vl|vr;
int bit=lowbit(v+1);
if (bit<=a[i]) v|=a[i]^(a[i]-bit);
int bit2=lowbit(v+1);
bit2=31-__builtin_clz(bit2);
update(List[r][bit2],l);
}
}
}
vector<vector<int>> qset(n+2);
for (int i=1;i<=Q;i++){
cin >> q[i].l >> q[i].r;
q[i].id=i;
int tmp=query(q[i].l,q[i].r);
int L=31-__builtin_clz(tmp)+1;
q[i].ans1=(1<<L)-1;
q[i].len=L;
if (tmp==q[i].ans1) q[i].ans2=0;
else{
q[i].ans2=2;
qset[q[i].r].push_back(i);
}
}
vector<int> tmp(32);
for (int r=1;r<=n;r++){
for (int j=0;j<32;j++) update(tmp[j],List[r][j]);
for (auto qid:qset[r]){
for (int j=q[r].len;j<32;j++){
if (q[qid].l<=tmp[j]) q[qid].ans2=1;
}
}
}
for (int i=1;i<=Q;i++){
cout << q[i].ans1 << " " << q[i].ans2 << "\n";
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 9684kb
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...