版本比较
众数问题
正文
增加 47 行删除 1 行
1234567147 行未改动全部展开155156157158159160161162263 行未改动全部展开# [P2397 yyy loves Maths VI (mode) / 摩尔投票](https://www.luogu.com.cn/problem/P2397)高贵的橙题 难点是不可以开不了a数组和cnt数组## 1.玄学随机输出序列中的一个数有 1/2 的概念通过147 行未改动全部展开```[提交记录](https://www.luogu.com.cn/record/246979962)# [P7261 [COCI 2009/2010 #3] PATULJCI](https://www.luogu.com.cn/problem/P7261#ide)注意到前面的做法可以直接搬过来263 行未改动全部展开1234567147 行未改动全部展开155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208263 行未改动全部展开# [P2397 yyy loves Maths VI (mode) / 摩尔投票](https://www.luogu.com.cn/problem/P2397)高贵的橙题 难点是开不了a数组和cnt数组## 1.玄学随机输出序列中的一个数有 1/2 的概念通过147 行未改动全部展开```[提交记录](https://www.luogu.com.cn/record/246979962)## 3.二进制拆分维护各位的前缀和 做法同上题 对ans用二分判断是否为众数```#include<bits/stdc++.h>using namespace std;const int N=5e4+5;inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}int a[N],cnt[N][16];vector < int > pos[N];signed main(){int n=read(),m=read();for (int i=1;i<=n;i++){int x=read();pos[x].push_back(i);for (int j=0;j<=15;j++){cnt[i][j]+=cnt[i-1][j];if(x&(1<<j)) cnt[i][j]++;}}while (m--){int x=read(),y=read(),k=0;for (int i=0;i<=15;i++){if (cnt[y][i]-cnt[x-1][i]>((y-x+1)>>1)) k+=(1<<i);}if (k==0||k>5e4){puts("0");}else{int l=lower_bound(pos[k].begin(),pos[k].end(),x)-pos[k].begin(),r=upper_bound(pos[k].begin(),pos[k].end(),y)-pos[k].begin();if (r-l>(y-x+1)/2) printf("%d\n",k);else puts("0");}}return 0;}```[提交记录](https://www.luogu.com.cn/record/247116658)并获得此题最优解# [P7261 [COCI 2009/2010 #3] PATULJCI](https://www.luogu.com.cn/problem/P7261#ide)注意到前面的做法可以直接搬过来263 行未改动全部展开