Article From:https://www.cnblogs.com/MisakaAzusa/p/9216221.html

Title Link: https://www.luogu.org/problemnew/show/P3709
Discretization + interval numbers..?

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 500000+10;
inline int read()
{
    int k=0;
    char c;
    c=getchar();
    while(!isdigit(c))c=getchar();
    while(isdigit(c)){k=(k<<3)+(k<<1)+c-'0';c=getchar();}
    return k;
}
int n, m, a[maxn], ans[maxn], cnt[maxn], bl, curL = 1, curR = 0, answer = 0, b[maxn], tot, sum[maxn];
struct query{
    int l, r, p;
}e[maxn];
bool cmp(query a, query b)
{
    return (a.l/bl) == (b.l/bl) ? a.r < b.r : a.l < b.l; 
}
void add(int pos)
{
    sum[cnt[a[pos]]]--;
    cnt[a[pos]]++;
    sum[cnt[a[pos]]]++;
    answer = max(answer, cnt[a[pos]]);
}
void remove(int pos)
{
    sum[cnt[a[pos]]]--;
    cnt[a[pos]]--;
    sum[cnt[a[pos]]]++;
    while(!sum[answer]) answer--;
}
int main()
{
    n = read(); m = read();
    
    bl = sqrt(n);
    
    for(int i = 1; i <= n; i++)
    a[i] = b[++tot] = read();
    
    sort(b+1, b+1+tot);
    tot = unique(b+1, b+1+tot)-b-1;
    for(int i = 1; i <= n; i++)
    a[i] = lower_bound(b+1,b+1+tot,a[i])-b;//DiscretizationFor (int i = 1; I < = m; i++){E[i].l = read (); e[i].r = read (); e[i].p= I;}Sort (e+1, e+1+m, CMP);For (int i = 1; I < = m; i++){Int L =E[i].l, R = e[i].r;While (curL < L) remove (curL++);While (curL > L) add (--cur)L);While (curR < R) add (++curR);While (curR > R) remove (curR--);Ans[e[i].p] = answer;}For (int i = 1; I < = m; i++)Printf ("%d\n", 0-ans[i]);Return 0;}/ *7552152521000000000 1000000000 10000000001312162522* * /

Leave a Reply

Your email address will not be published. Required fields are marked *