Codeforces Round 900 (Div. 3) E. Iva & Pav

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
#define int long long
typedef pair<int,int> pii;

const int N=200010;

int pre[N][32];
int a[N];

int check(int l,int x)
{
	int sum=0;
	int res=1;
	for(int i=0;i<31;i++)
	{
		if(pre[x][i]-pre[l-1][i]==(x-l+1))
		{
			sum+=res;
		}
		res<<=1;
	}
	return sum;
}
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=0;i<31;i++)//外层循环为每一数的每一位
	{
		for(int j=1;j<=n;j++)
		{
			if(a[j]>>i&1)pre[j][i]=pre[j-1][i]+1;
			else pre[j][i]=pre[j-1][i]; 
		}
	} 
	int cnt=0;
	int q;
	cin>>q;
	while(q--)
	{
		int l,k;
		cin>>l>>k;
		int ll=l-1,r=n+1;
		while(ll+1<r)
		{
			int mid=ll+r>>1;
		//	cout<<mid<<"++"<<endl;
			if(check(l,mid)>=k)ll=mid;
			else r=mid; 
		}
		int t=check(l,ll);
		//cout<<ll<<"++"<<endl;
		if(ll!=l-1)cout<<ll<<" ";
		else cout<<"-1 ";
		//cout<<endl;
	}
	cout<<endl;
}
signed main()
{ 
    int good_luck_to_you;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   // cout<<s.count({100,100})<<"__"<<endl;
    cin>>good_luck_to_you;
    while(good_luck_to_you--)
    {
       solve();
    }
    system("pause");   
}
思路:本题是利用位运算和前缀和的知识,因为涉及到&,所以按位运算。
我主要是卡在不知道怎么求出某一个区间的 &,在看完题解后,随着数组长度的增加,该区间的&不会增加,是非递减的。(符合二分的性质)
对于一个区间的&,我们算出每位在[l,r]为0或1,只要该位在[l,r]区间的值为r-l+1,则该位即可以取到1(这步在二分中做。
对于无解的情况,只要判断最后的mid是否等于l-1。                                                                                                                                                               
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。&lt;span class=&quot;em&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;3&lt;/span&gt; #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值