P1997 AC,这道题一直卡在90分
查看原帖
P1997 AC,这道题一直卡在90分
303714
Luckin_Coffee楼主2020/7/12 11:15

救救孩子,其他众数的题都AC了,就这道题死也过不去

#include <bits/stdc++.h>
using namespace std;
#define limit (200000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a ; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\administrator01\\CLionProjects\\untitled24\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}//快读
void write(ll x){
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n, m;
struct node{
    int l, r, qid, blo;
    int len(){
        return r - l + 1;
    }
    bool operator<(const node &rhs)const{
        if(blo ^ rhs.blo)return l < rhs.l;
        return blo & 1 ? r < rhs.r : rhs.r < r;
    }
}query[limit];
int cnt[limit],a[limit],sum[limit];
int res;
void add(int x){
    --sum[cnt[a[x]]];//减去多余部分
    ++cnt[a[x]];//增加一个count
    res = max(res, cnt[a[x]]);
    ++sum[cnt[a[x]]];
}
void del(int x){
    --sum[cnt[a[x]]];//撤回一个count
    if(res == cnt[a[x]] && !sum[cnt[a[x]]])--res;//如果没有了那就增加
    --cnt[a[x]];
    ++sum[cnt[a[x]]];
}
map<int, int>mp;
int tot, vis[limit];
int main(){
#ifdef LOCAL
    FOPEN;
    //freopen("C:\\Users\\administrator01\\CLionProjects\\untitled24\\out.txt", "w", stdout);
#endif
    cin>>n>>m;
    tot = 0;
    rep(i ,1, n){
        int input;
        cin>>input;
        if(!mp[input]){
            a[i] = mp[input] = ++tot;
        }else{
            a[i] = mp[input];
        }
    }
    int block = sqrt(n);//分块
    rep(i, 1, m){
        cin>>query[i].l>>query[i].r; query[i].qid = i;
        query[i].blo = query[i].l / block;//分成块
    }
    sort(query + 1, query + 1 + m);
    ll l = 1 , r =0;
    //res = 0;
    rep(i ,1, m){
        while(l < query[i].l)del(l++);//缩进
        while(l > query[i].l)add(--l);
        while(r < query[i].r)add(++r);
        while(r > query[i].r)del(r--);
        vis[query[i].qid] = res;
    }
    rep(i ,1, m){
        printf("%d\n", -vis[i]);
    }
    return 0;
}
2020/7/12 11:15
加载中...