1878E - Iva Pav - CodeForces Solution


binary search bitmasks data structures greedy

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int dp[200020][20];
void solve()
{
	int n;
	cin>>n;
	// int a[n+10];
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=19;j++)dp[i][j]=0;
	}
	for(int i=1;i<=n;i++)cin>>dp[i][0];
	for(int i=n;i>=1;i--)
	{
		for(int j=1;j<=(int)log2(n-i+1);j++)
		{
			dp[i][j]=(dp[i][j-1]&dp[i+(1<<(j-1))][j-1]);
		}
	}
	int q;
	cin>>q;
	while(q--)
	{
		int l,k;
		cin>>l>>k;
		int r=n;
		int z=l;
		int an=-1;
		while(l<=r)
		{
			int mid=l+r>>1;
			int kk=z;
			int ans=dp[z][0];
			while(kk<=mid)
			{
				ans=(ans&dp[kk][(int)log2(mid-kk+1)]);
				kk+=(1<<(int)log2(mid-kk+1));
			}
			if(ans>=k)an=mid,l=mid+1;
			else r=mid-1;
		}
		cout<<an<<'\n';
	}
	cout<<'\n';
	// cout<<dp[1][2]<<endl;
}
int main() {
	int t;
	cin>>t;
	while(t--)solve();
	return 0;	
}


Comments

Submit
0 Comments
More Questions

1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions