QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#391866#6631. Maximum Bitwise ORNahidameowRE 1ms5632kbC++203.5kb2024-04-16 21:21:472024-04-16 21:21:47

Judging History

你现在查看的是最新测评结果

  • [2024-04-16 21:21:47]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5632kb
  • [2024-04-16 21:21:47]
  • 提交

answer

#include<bits/stdc++.h>
#define pd push_back
#define all(A) A.begin(),A.end()
#define lower_bound lb
#define ve std::vector
typedef long long ll;
typedef long long ll;
typedef __int128 Int;
typedef unsigned long long ul;
typedef long double LD;
bool FileIfstream(std::string name){
	std::ifstream f(name.c_str());
	return f.good();
}
namespace Math{
	ll QP(ll x,ll y,ll mod){ll ans=1;for(;y;y>>=1,x=x*x%mod)if(y&1)ans=ans*x%mod;return ans;}
	ll inv(ll x,ll mod){return QP(x,mod-2,mod);}
}
const int N=2e5+10;
const int mod=998244353;
struct Spare_Table{
	int st[N][25];
	int LOG2[N],n;
	void make(int _n,ve<int>&v){
		n=_n;
		for(int i=1;i<=n;i++)
			st[i][0]=v[i];
		for(int i=2;i<=n;i++)
			LOG2[i]=LOG2[i>>1]+1;
		for(int j=1;j<=20;j++)
			for(int i=1;i+(1<<j)-1<=n;i++)
				st[i][j]=(st[i][j-1]|st[i+(1<<j-1)][j-1]);
	}
	int query(int l,int r){
		assert(l<=r);
		int k=LOG2[r-l+1];
		return st[l][k]|st[r-(1<<k)+1][k];
	}
}ST;
struct node{
	std::array<int,2> S[32];
	int dS[32];
};
node operator + (node L,node R){
	node ans;
	for(int i=0;i<=30;i++)
		ans.dS[i]=std::max(L.dS[i],R.dS[i]);
	for(int i=0;i<=30;i++)
		ans.S[i][0]=L.S[i][0]+R.S[i][0];
	for(int i=0;i<=30;i++)
		if(ans.S[i][0]!=1)ans.S[i][1]=-1;
		else ans.S[i][1]=std::max(L.S[i][1],R.S[i][1]);
	return ans;
}
struct sg_tree{
	node tr[N<<2];
	void pushup(int rt){
		tr[rt]=tr[rt<<1]+tr[rt<<1|1];}
	void build(int rt,int l,int r,ve<int>&v){
		if(l==r){
			for(int i=0;i<=30;i++)
				tr[rt].dS[i]=((v[l]<(1<<i))?0:(v[l]^(v[l]-(1<<i))));
			for(int i=0;i<=30;i++)
				tr[rt].S[i]={0,-1};
			for(int i=0;i<=30;i++)
				if(v[l]>>i&1)tr[rt].S[i][0]=1,tr[rt].S[i][1]=l; 
			return;
		}
		auto mid=l+r>>1;
		build(rt<<1,l,mid,v);build(rt<<1|1,mid+1,r,v); 
		pushup(rt);
	}
	node query(int rt,int l,int r,int x,int y){
		if(x<=l&&r<=y)return tr[rt];
		auto mid=l+r>>1;
		if(x<=mid&&y>mid)return query(rt<<1,l,mid,x,y)+query(rt<<1|1,mid+1,r,x,y);
		if(x<=mid)return query(rt<<1,l,mid,x,y);
		if(y>mid)return query(rt<<1|1,mid+1,r,x,y);
	}
}Tr;
void solve(){
	//don't forget to open long long
	int n,m;std::cin>>n>>m;
	ve<int>v(n+1);
	for(int i=1;i<=n;i++)std::cin>>v[i];
	ST.make(n,v);Tr.build(1,1,n,v);
	while(m--){
		int l,r;std::cin>>l>>r;
		int S=ST.query(l,r);
		if(!S){std::cout<<0<<' '<<0<<'\n';continue;}
		int T=(1<<std::__lg(S)+1)-1;
		if(S==T){std::cout<<T<<' '<<0<<'\n';continue;}
		auto check=[&]()->bool{
			auto Q=Tr.query(1,1,n,l,r);
			ve<int>pos;pos.pd(l-1),pos.pd(r+1);
			for(int i=0;i<=30;i++)
				if(Q.S[i][1]!=-1)pos.pd(Q.S[i][1]);
			sort(all(pos));pos.erase(unique(all(pos)),pos.end());
			ve<int>A(31,0);
			for(int i=0;i+1<pos.size();i++){
				int L=pos[i]+1,R=pos[i+1]-1;
				if(L<=R){
					auto K=Tr.query(1,1,n,L,R);
					for(int j=0;j<=30;j++)
						A[j]=std::max(A[j],K.dS[j]);
				}
			}
			for(int i=0;i<=30;i++)
				if((A[i]|S)==T)
					return true;
			for(int i=1;i+1<pos.size();i++){
				int P=ST.query(l,pos[i]-1)|ST.query(pos[i]+1,r);
				auto K=Tr.query(1,1,n,pos[i],pos[i]);
				for(int j=0;j<=30;j++)
					if((K.dS[j]|P)==T)return true;
			}
			return false;
		};
		std::cout<<T<<' '<<(check()?1:2)<<'\n';
	}
}
int main(){
#ifndef ONLINE_JUDGE
	if(!FileIfstream("IO.in")){
		freopen("IO.in","w",stdout);
		return 0;
	}
	freopen("IO.in","r",stdin);
	freopen("IO.out","w",stdout);
#endif
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int T=1;
	std::cin>>T;
	while(T--)solve();

	return 0;
}




详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5632kb

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
Runtime Error

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:


result: