有史以来做过的最水的一道 div.3 的 G 题……
思路 1
看到题目中要求在区间 中出现次数大于 的数,这样的数如果在区间内存在,那么至多只会出现两个,那么我们在区间内随机选择 个数,用二分依次判断这些数在区间内的出现次数,如果大于了 ,那么这个数就符合要求。
时间复杂度为 ,其中 为你随机选择的数的个数,而你答案错误的概率则为 ,选择 个数时大约为 ,只要你上辈子不至于伤天害理,总能过的。
code:
#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
由于 是定的,我们可以用线段树来维护摩尔投票算法,用 的时间复杂度来查询,故而总体时间复杂度为 。
码风不好,见谅。
#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;
}