And Reachability CodeForces

大意: 给定序列$a$, 对所有的a[i]&a[j]>0, 从$i$向$j$连一条有向边, 给出$m$个询问$(x,y)$, 求是否能从$x$到达$y$.

裸的有向图可达性, 有向图可达性直接暴力是$O(n^3)$的, 或者可以用$bitset$优化到$O(frac{n^3}{omega})$ 

但是这个图比较特殊, 显然每个点向后最多连20条边即可, 就可以做到$O(20^2n)$预处理.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '
'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head




const int N = 1e6+10;

int n, q, a[N];
int dp[N][22], f[22];

int main() {
	scanf("%d%d", &n, &q);
	REP(i,1,n) scanf("%d", a+i);
	REP(i,0,20) f[i] = n+1;
	PER(i,1,n) {
		REP(j,0,20) dp[i][j] = n+1;
		REP(j,0,20) if (a[i]>>j&1) {
			dp[i][j] = f[j];
			REP(k,0,20) if (dp[f[j]][k]) dp[i][k]=min(dp[i][k],dp[f[j]][k]);
		}
		REP(j,0,20) if (a[i]>>j&1) f[j]=i;
	}
	while (q--) {
		int x, y, flag = 0;
		scanf("%d%d", &x, &y);
		REP(j,0,20) if ((a[y]>>j&1)&&dp[x][j]<=y) flag = 1;
		puts(flag?"Shi":"Fou");
	}
}
原文地址:https://www.cnblogs.com/uid001/p/10942418.html