QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#375560 | #6631. Maximum Bitwise OR | wsc2008 | ML | 0ms | 0kb | C++14 | 2.2kb | 2024-04-03 12:55:02 | 2024-04-03 12:55:04 |
answer
#include<bits/stdc++.h>
typedef int ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
const ll N=2e5+9;
ll T,n,q,a[N],sum[31][N],pos[31][N],cnt[31];
ll f[466][N],bin[31][31],id[31][31],tid;
vector<ll>spe[N];
void solve(){
n=read(),q=read();
rep(i,1,n)a[i]=read();
rep(i,0,30){
rep(j,0,n)sum[i][j]=pos[i][j]=0;
}
rep(i,0,465){
rep(j,0,n)f[i][j]=0;
}
rep(i,0,30){
rep(j,1,n){
sum[i][j]=sum[i][j-1]+((a[j]>>i)&1);
pos[i][j]=pos[i][j-1]+((a[j]>>i)&1)*j;
}
}
rep(i,1,n){
memset(bin,0,sizeof(bin));
ll cur=30;
per(j,30,0){
if((a[i]>>j)&1)cur=j;
if(cur<30){
rep(k,j,cur)bin[j][k]=1;
}
}
cur=0;
rep(j,0,30){
rep(k,j,30)f[cur++][i]=bin[j][k];
}
}
rep(i,0,465){
rep(j,1,n)f[i][j]+=f[i][j-1];
}
while(q--){
ll l=read(),r=read(),top=30;
while(top>=0&&sum[top][r]-sum[top][l-1]==0)top--;
if(!(~top)){
puts("0 0");
continue;
}
write((2<<top)-1),putchar(' ');
rep(i,0,top)cnt[i]=sum[i][r]-sum[i][l-1];
vector<ll>tar;
ll L=31,R=-1;
rep(i,0,top){
if(cnt[i]==1){
ll p=pos[i][r]-pos[i][l-1];
tar.push_back(p);
spe[p].push_back(i);
}
if(!cnt[i])L=min(L,i),R=max(R,i);
}
if(L>R){
puts("0");
for(ll i:tar)spe[i].clear();
continue;
}
sort(tar.begin(),tar.end());
tar.resize(unique(tar.begin(),tar.end())-tar.begin());
ll rid=id[L][R],qq=f[rid][r]-f[rid][l-1];
for(ll i:tar){
sort(spe[i].begin(),spe[i].end());
ll st=f[rid][i]-f[rid][i-1];
qq-=st;
ll nid=id[min(spe[i][0],L)][max(spe[i].back(),R)];
qq+=f[nid][i]-f[nid][i-1];
}
if(qq)puts("1");
else puts("2");
for(ll i:tar)spe[i].clear();
}
}
bool Med;
int main(){
cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
rep(i,0,30){
rep(j,i,30)id[i][j]=++tid;
}
T=read();
while(T--)solve();
return 0;
}
詳細信息
Test #1:
score: 0
Memory Limit Exceeded
input:
1 3 2 10 10 5 1 2 1 3
output:
15 2 15 0