E. And Reachability
time limit per test
3 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputToad Pimple has an array of integers a1,a2,…,an.
We say that 1≤i<k.
Here bitwise AND operation.
You are given q pairs of indices, check reachability for each of them.
Input
The first line contains two integers 1≤q≤300000) — the number of integers in the array and the number of queries you need to answer.
The second line contains 0≤ai≤300000) — the given array.
The next xi.
Output
Output
Example
input
Copy
5 3 1 3 0 2 1 1 3 2 4 1 4
output
Copy
Fou Shi Shi
Note
In the first example, p=[1,2,4].
#include<iostream> #include<cstdio> #define maxn 300010 using namespace std; int n,q,a[maxn],dp[maxn][30],las[30]; int main(){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=0;i<20;i++){ las[i]=n+1; dp[n+1][i]=n+1; } for(int i=n;i>=1;i--){ for(int j=0;j<20;j++) dp[i][j]=n+1; for(int j=0;j<20;j++){ if(a[i]&(1<<j)){ for(int k=0;k<20;k++) dp[i][k]=min(dp[i][k],dp[las[j]][k]); las[j]=i; dp[i][j]=i; } } } int x,y; while(q--){ cin>>x>>y; bool f=0; for(int i=0;i<20;i++){ if(a[y]&(1<<i)&&dp[x][i]<=y){ f=1; break; } } if(f)puts("Shi"); else puts("Fou"); } return 0; }