faebdc troubles Mo team

faebdc troubles Mo team

Face questions

Thinking

A bit difficult to think Mo team.

First, we will certainly be a cnt[i]difficult record inumber of occurrences, but we found that after each deletion of a difficulty, that difficulty if the number happens to be the current highest number, we may want to update your answers, and depending on how much difficulty the number happens to be the current highest number , so we have to open a sum[i]number of records how much more difficult is i.

After the above analysis easy to get MO fleet renewal rules:

inline void add(int x){
    --sum[cnt[a[x]]];
    ++cnt[a[x]];
    ++sum[cnt[a[x]]];
    ans=max(cnt[a[x]], ans);
}
inline void del(int x){
    --sum[cnt[a[x]]];
    if(cnt[a[x]]==ans&&sum[cnt[a[x]]]==0) --ans;
    --cnt[a[x]];
    ++sum[cnt[a[x]]];
}

Also note because the range of \ (- 10 ^ 5 \ Le A [i] \ Le 10 ^ 5 \) , so we can all add to the number \ (10 ^ 5 \)

Example code

#include <cstdio>
#include <algorithm>
#include <cmath>
#define MAXN 100010
#define MAXQ 200010
using namespace std;
int ans,cnt[100001*2],sum[100001];
int a[MAXN];
inline void add(int x){
    --sum[cnt[a[x]]];
    ++cnt[a[x]];
    ++sum[cnt[a[x]]];
    ans=max(cnt[a[x]], ans);
}
inline void del(int x){
    --sum[cnt[a[x]]];
    if(cnt[a[x]]==ans&&sum[cnt[a[x]]]==0) --ans;
    --cnt[a[x]];
    ++sum[cnt[a[x]]];
}
struct nod{
    int l,r,bid,qid;
} q[MAXQ];
bool cmp(const nod &a, const nod &b){
    return (a.bid^b.bid?(a.l<b.l):((a.bid&1)?(a.r<b.r):(a.r>b.r)));
}
int n,m,blo,res[MAXQ];
int main()
{
    scanf("%d %d", &n, &m);
    blo=n/sqrt(m*2/3);
    for(int i=1;i<=n;++i) scanf("%d", &a[i]),a[i]+=100000;
    for(int i=1;i<=m;++i){
        scanf("%d %d", &q[i].l, &q[i].r);
        q[i].bid=q[i].l/blo;
        q[i].qid=i;
    }
    sort(q+1, q+1+m, cmp);
    int l=1,r=1;cnt[a[1]]=1;sum[1]=1;ans=1;
    for(int i=1;i<=m;++i){
        while(l<q[i].l) del(l++);
        while(l>q[i].l) add(--l);
        while(r>q[i].r) del(r--);
        while(r<q[i].r) add(++r);
        res[q[i].qid]=ans;
    }
    for(int i=1;i<=m;++i) printf("%d\n", res[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/santiego/p/11323090.html