跳到正文

题解

题解:CF2149G Buratsuta 3

Collapsarr约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年10月4日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 10 分钟
题目
CF2149G

版本

  1. v2当前
  2. v1

有史以来做过的最水的一道 div.3 的 G 题……

思路 1

看到题目中要求在区间 [l,r][l,r] 中出现次数大于 ⌊r−l+13⌋\lfloor \frac{r-l+1}{3} \rfloor 的数,这样的数如果在区间内存在,那么至多只会出现两个,那么我们在区间内随机选择 5050 个数,用二分依次判断这些数在区间内的出现次数,如果大于了 ⌊r−l+13⌋\lfloor \frac{r-l+1}{3} \rfloor,那么这个数就符合要求。

时间复杂度为 O(qklog⁡(n))O(qk\log(n)),其中 kk 为你随机选择的数的个数,而你答案错误的概率则为 (23)k(\frac{2}{3})^k,选择 5050 个数时大约为 1.5×10−91.5×10^{-9},只要你上辈子不至于伤天害理,总能过的。

code:

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define itn int
#define reaD read
#define int long long
#define intt __int128
#define double long double
#define endl '\n'

int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while(ch<'0'||'9'<ch)
	{
		if(ch=='-')
		{
			f = -1;
		}
		ch = getchar();
	}
	while('0'<=ch&&ch<='9')
	{
		x = (x<<3) + (x<<1) + ch - '0';
		ch = getchar();
	}
	return x * f;
}

const int inf = 1e18,N = 1e6 + 10;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int n,m,a[N];
gp_hash_table<int,vector<int>> mp;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	for(int T = read();T;T--)
	{
		n = read(),m = read();
		for(int i = 1;i<=n;i++)
		{
			mp[a[i]=read()].push_back(i);
		}
		for(int i = 1,l,r,len,lim;i<=m;i++)
		{
			l = read(),r = read();
			len = r - l + 1,lim = len / 3;
			vector<int> f(50);
			for(int j = 1;j<=50;j++)
			{
				f[j-1] = a[rd()%len+l];
			}
			vector<int> ans;
			for(int j = 0;j<50;j++)
			{
				if(upper_bound(mp[f[j]].begin(),mp[f[j]].end(),r)-lower_bound(mp[f[j]].begin(),mp[f[j]].end(),l)>lim)
				{
					ans.push_back(f[j]);
				}
			}
			if(ans.size()==0)
			{
				cout<<-1<<endl;
			}
			else
			{
				sort(ans.begin(),ans.end());
				auto it = unique(ans.begin(),ans.end());
				ans.erase(it,ans.end());
				for(int j = 0;j<ans.size();j++)
				{
					cout<<ans[j]<<' ';
				}
				cout<<endl;
			}
		}
		mp.clear();
	}
	return 0;
}

思路 2

由于 aa 是定的,我们可以用线段树来维护摩尔投票算法,用 log⁡(n)\log(n) 的时间复杂度来查询,故而总体时间复杂度为 O(qlog⁡(n))O(q\log(n))。

码风不好,见谅。

C++
#include<bits/stdc++.h>
using namespace std;
#define reaD read
#define int long long
#define endl '\n'

int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while(ch<'0'||'9'<ch)
	{
		if(ch=='-')
		{
			f = -1;
		}
		ch = getchar();
	}
	while('0'<=ch&&ch<='9')
	{
		x = (x<<3) + (x<<1) + ch - '0';
		ch = getchar();
	}
	return x * f;
}

const int inf = 1e18,N = 2e5 + 10;

int n,m,a[N],b[N],tot;
vector<int> pos[N];

struct tree
{
	pair<pair<int,int>,pair<int,int>> t[N<<2];
	#define ls(i) i<<1
	#define rs(i) i<<1|1
	inline void add(pair<pair<int,int>,pair<int,int>> &a,int x,int y)
	{
		if(x==0||y==0)
		{
			return ;
		}
		if(a.first.first==x)
		{
			a.first.second += y;
		}
		else if(a.second.first==x)
		{
			a.second.second += y;
		}
		else if(a.first.second==0)
		{
			a.first = {x,y};
		}
		else if(a.second.second==0)
		{
			a.second = {x,y};
		}
		else
		{
			int minn = min({a.first.second,a.second.second,y});
			a.first.second -= minn,a.second.second -= minn,y -= minn;
			if(y)
			{
				if(a.first.second==0)
				{
					a.first = {x,y};
				}
				else
				{
					a.second = {x,y};
				}
			}
		}
	}
	inline void push_up(int i)
	{
		t[i] = t[ls(i)];
		add(t[i],t[rs(i)].first.first,t[rs(i)].first.second);
		add(t[i],t[rs(i)].second.first,t[rs(i)].second.second);
	}
	inline void build(int i,int l,int r)
	{
		if(l==r)
		{
			t[i] = {{a[l],1},{0,0}};
			return ;
		}
		int mid = (l+r)>>1;
		build(ls(i),l,mid);
		build(rs(i),mid+1,r);
		push_up(i);
	}
	inline pair<pair<int,int>,pair<int,int>> query(int i,int l,int r,int L,int R)
	{
		if(L<=l&&r<=R)
		{
			return t[i];
		}
		int mid = (l+r)>>1;
		if(R<=mid)
		{
			return query(ls(i),l,mid,L,R);
		}
		else if(L>mid)
		{
			return query(rs(i),mid+1,r,L,R);
		}
		else
		{
			pair<pair<int,int>,pair<int,int>> dl = query(ls(i),l,mid,L,R),dr = query(rs(i),mid+1,r,L,R);
			add(dl,dr.first.first,dr.first.second);
			add(dl,dr.second.first,dr.second.second);
			return dl;
		}
	}
}t;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	for(int T = read();T;T--)
	{
		n = read(),m = read();
		for(int i = 1;i<=n;i++)
		{
			a[i] = b[i] = read();
			pos[i].clear();
		}
		sort(b+1,b+1+n);
		tot = unique(b+1,b+1+n) - b - 1;
		for(int i = 1;i<=n;i++)
		{
			a[i] = lower_bound(b+1,b+1+tot,a[i]) - b;
			pos[a[i]].push_back(i);
		}
		t.build(1,1,n);
		for(int i = 1,l,r;i<=m;i++)
		{
			l = read(),r = read();
			pair<pair<int,int>,pair<int,int>> ans = t.query(1,1,n,l,r);
			set<int> st;
			if(ans.first.second&&upper_bound(pos[ans.first.first].begin(),pos[ans.first.first].end(),r)-lower_bound(pos[ans.first.first].begin(),pos[ans.first.first].end(),l)>(r-l+1)/3)
			{
				st.insert(ans.first.first);
			}
			if(ans.second.second&&upper_bound(pos[ans.second.first].begin(),pos[ans.second.first].end(),r)-lower_bound(pos[ans.second.first].begin(),pos[ans.second.first].end(),l)>(r-l+1)/3)
			{
				st.insert(ans.second.first);
			}
			if(st.empty())
			{
				cout<<-1<<endl;
			}
			else
			{
				auto it = st.begin();
				while(it!=st.end())
				{
					cout<<b[*it]<<' ';
					it++;
				}
				cout<<endl;
			}
		}
	}
	return 0;
}
原文