Uncle Mo string team title

Uncle Mo string team title

First, this is not a string of problems. Need to carefully study nature of the problem, we will find the answer is the number of interval numbers public, because we can be divided into intervals of several public increments the number of columns, such as optimal.

So the question for the sake of converting the number of Interval public. Use Mo team.

#include <cstdio>
#include <algorithm>
#include <cmath>
#define MAXN 400020
using namespace std;
int ans,a[MAXN],cnt[MAXN],sum[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,qid,bid;
} q[MAXN];
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));
}
struct pa{
    int val,pos;
} ta[MAXN];
bool cmp_pair(const pa &a, const pa &b){
    return a.val<b.val;
}
int n,m,blo,qres[MAXN];
int main(){
    freopen("testdata.in", "r", stdin);
    scanf("%d %d", &n, &m);
    for(int i=1;i<=n;++i) scanf("%d", &ta[i].val), ta[i].pos=i;
    sort(ta+1, ta+1+n, cmp_pair);
    int idx=0;
    a[ta[1].pos]=idx;
    for(int i=2;i<=n;++i){
        if(ta[i-1].val!=ta[i].val) ++idx;
        a[ta[i].pos]=idx;
    }
    blo=n/sqrt(m*2.0/3);
    for(int i=1;i<=m;++i){
        scanf("%d %d", &q[i].l, &q[i].r);
        q[i].qid=i;
        q[i].bid=q[i].l/blo;
    }
    sort(q+1, q+1+m, cmp);
    int l=1,r=1;ans=1;cnt[a[1]]=1;sum[1]=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) add(++r);
        while(r>q[i].r) del(r--);
        qres[q[i].qid]=ans;
    }
    for(int i=1;i<=m;++i) printf("%d\n", -1*qres[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/santiego/p/11328910.html
Recommended