QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#127902#6631. Maximum Bitwise ORyy_zqML 0ms0kbC++143.9kb2023-07-20 11:16:502023-07-20 11:16:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-20 11:16:51]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2023-07-20 11:16:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,j,k) for(int i=j;i<=k;++i)
#define For(i,j,k) for(int i=j;i>=k;--i)
#define mid (l+r>>1)
const int MAX = 1e5+111;
const int D = 30;
const int INF = 31;
int a[MAX];
int S[MAX][33];
int pos[MAX][33];//表示从某一位开始最后一个连续 0 的位置
int mn[MAX*4][33];//表示右侧节点大于 x 的最小左端点 
int who[MAX*4][33];//特殊数的位置 
int cnt[MAX*4][33];//统计每个数字出现的次数,以寻找特殊数 
//线段树查询存储答案的数组 
int que[33];
int que_who[33];
int used[MAX];//判断一个数是否多次作为 special 出现 
void build(int l,int r,int x){
	if(l==r){
		FOR(i,0,D){
			cnt[x][i] = S[l][i];
			who[x][i] = l;
			mn[x][i] = pos[l][i];
		}
		return;
	}
	build(l,mid,x<<1),build(mid+1,r,x<<1|1);
	FOR(i,0,D){
		cnt[x][i] = cnt[x<<1][i]+cnt[x<<1|1][i];
		if(cnt[x<<1][i]) who[x][i] = who[x<<1][i];
		else who[x][i]=who[x<<1|1][i];
		mn [x][i] =min( mn[x<<1][i], mn[x<<1|1][i] );
	}
}

void ask(int l,int r,int x,int lft,int rht){
	if(l>rht||r<lft) return;
	if(l>=lft&&r<=rht){
		FOR(i,0,D){
			que[i]+=cnt[x][i];
			if(cnt[x][i]) que_who[i] = who[x][i];
		}
		return;
	}
	ask(l,mid,x<<1,lft,rht),ask(mid+1,r,x<<1|1,lft,rht);
}

int ask_min(int l,int r,int x,int lft,int rht,int dig){
	if(l>rht||r<lft) return INF;
	if(l>=lft&&r<=rht){
		return mn[x][dig];
	}
	return min(ask_min(l,mid,x<<1,lft,rht,dig),ask_min(mid+1,r,x<<1|1,lft,rht,dig));
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("1.in","r",stdin);
	int t;
	cin>>t;
	while(t--){
		int n,q;
		cin>>n>>q;
		FOR(i,1,n) cin>>a[i];
		//第一步预处理 
		FOR(i,1,n){
			int mx=0;
			FOR(j,0,D){
				if((a[i])&(1ll<<j)){
					S[i][j]=1;
					mx = j;
				}
				else S[i][j] = 0;
			}
			int lst = 0;
			FOR(j,0,mx){
				if((a[i])&(1ll<<j)){
					pos[i][j] = lst;
					lst = j+1;
				}
				else pos[i][j] = lst;
			}
			FOR(j,mx+1,D) pos[i][j] = 31;
		}
		
		//建树
		build(1,n,1);
		
		while(q--){
			int l,r;
			cin>>l>>r;
			FOR(i,0,D) que[i] = 0;
			ask(1,n,1,l,r);
			//信息提取完成 
			int max_or=0,max_dig=0;
			For(i,30,0){
				if(que[i]){
					max_or = (1ll<<(i+1))-1;
					max_dig = i;
					break;
				}
			}
			int tmpys=1;
			For(i,max_dig,0){
				if(!que[i]) tmpys=0;
			}
			if(tmpys){
				cout<<max_or<<' '<<0<<endl;
				continue;
			}
			else{
				int special_num[33]={};
				int special_cnt=0;
				For(i,30,0){
					if(que[i]==1){
						special_num[++special_cnt] = que_who[i];
						used[que_who[i]]++;
					}
				}
				sort(special_num+1,special_num+1+special_cnt);
				special_cnt = unique(special_num+1,special_num+1+special_cnt) - special_num -1;
				//因为special 是不能选的,因此边界要处理一下,才能一起处理 
				special_num[0] = l-1;
				special_num[special_cnt+1] = r+1;
				//下面查询区间贡献
				int ys = 1;
				For(i,max_dig,0){
					if(!que[i]){
						int mn_dig=INF;
						FOR(j,0,special_cnt){
							int tmp_min =ask_min(1,n,1,special_num[j]+1,special_num[j+1]-1,i+1);
							mn_dig = min(tmp_min,mn_dig);	
						}
						For(j,mn_dig-1,0){
							if(!que[j]){
								ys=0;
								break;
							}
						}
						break;
					}
				}
				if(ys){
					cout<<max_or<<' '<<1<<endl;
				}
				else{
					//此时需要查验特殊数字
					int mn_0=0;
					FOR(i,0,D){
						if(que[i]==0){
							mn_0 = 0;
							break;
						} 	
					}
					int ys2 = 0;
					For(i,max_dig,0){
						if(que[i]==0) break;
						if(que[i]==1){
							if(used[que_who[i]]==1){
								if(pos[que_who[i]][i]<=mn_0){
									ys2=1;
									break;
								}
							}
						}
					}
					
					if(ys2) cout<<max_or<<' '<<1<<endl;
					else cout<<max_or<<' '<<2<<endl;
				}
				FOR(i,1,special_cnt){
					used[special_num[i]] = 0;
				}
			}
		}
	}
	return 0; 
}

/*
1
4 2
12 1 1 12
1 4
1 4
*/ 

详细

Test #1:

score: 0
Memory Limit Exceeded

input:

1
3 2
10 10 5
1 2
1 3

output:


result: