First, this question surfaces curved around school do not understand
Glanced at the solution to a problem is to find the number of occurrences of the mode inside a range.
Because this interval does not change, offline, then the team on the line Mo
Mo team seeking the mode, write x number of occurrences cnt [x], record the number of occurrences is the number n is the number of num [n], the number of times the current number is now public
Add then directly modify cnt and num, with cnt to update now
Deleted, if the time num [cnt [x]] == 1 and cnt [x] == now, then later delete x, there is no cnt == now the number, now--. You can not be handled like code
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> #include <algorithm> #define MAXN 200010 using namespace std; int n,m,a[MAXN],b[MAXN],pos[MAXN],ans[MAXN],cnt[MAXN],num[MAXN],now; struct Ques{ int l,r,id; }q[MAXN]; bool cmp(Ques x,Ques y){return pos[x.l]<pos[y.l]||pos[x.l]==pos[y.l]&&x.r<y.r;} void add(int x){ num[cnt[x]]--; num[++cnt[x]]++; now=max(now,cnt[x]); } void del(int x){ num[cnt[x]]--; if(cnt[x]==now&&!num[cnt[x]]) now--; num[--cnt[x]]++; } int main(){ scanf("%d%d",&n,&m); int sz=sqrt(n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i],pos[i]=i/sz; sort(b+1,b+n+1); int tot=unique(b+1,b+n+1)-b-1; for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+tot+1,a[i])-b; for(int i=1;i<=m;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i; sort(q+1,q+m+1,cmp); int L=0,R=0; num[0]=tot; for(int i=1;i<=m;i++){ while(R<q[i].r) add(a[++R]); while(R>q[i].r) del(a[R--]); while(L>q[i].l) add(a[--L]); while(L<q[i].l) del(a[L++]); ans[q[i].id]=-now; } for(int i=1;i<=m;i++) printf("%d\n",ans[i]); return 0; }