「杂题乱刷」CF1927D

盲猜没人用此做法

题目链接

CF1927D Find the Different Ones!

解题思路

考虑分块,在一块内的数当且仅当他们是连续的且权值相同,并记录下他们的左右端点同时记录每一个数在哪个块内,对于每个块维护它的左右端点,然后对于每组询问如果 \(l,r\) 在同一个块内,那么 \(l \sim r\) 的所有权值一定相同,输出 -1 -1,否则 \(l \sim r\) 中一定有权值不同的数,这时输出 \(l\) 和下一个块的左端点表示的数即可。

参考代码

#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll t;
void solve()
{
	ll n,m;
	cin>>n;
	ll k=0,a[n+10]={0},b[n+10]={0},l[n+10]={0},r[n+10]={0},num[n+10]={0};
	forl(i,1,n)
	{
		cin>>a[i];
		if(a[i]!=a[i-1])
			r[k]=i,l[++k]=i,num[k]=a[i];
		b[i]=k;
	}
	cin>>m;
	r[k]=n;
	while(m--)
	{
		ll L,R;
		cin>>L>>R;
		if(b[L]==b[R])
			cout<<"-1 -1\n";
		else
		{
			cout<<L<<' '<<l[b[L]+1]<<endl;
		}
	}
	cout<<endl;
}
int main()
{
	IOS;
	t=1;
	cin>>t;
	while(t--)
		solve();
	QwQ;
}
posted @ 2024-02-07 03:15  wangmarui  阅读(10)  评论(0编辑  收藏  举报