萌新刚学oi,想做分块题 全WA了求解

P4168 [Violet] 蒲公英

yijan @ 2018-10-18 17:48:20

为啥wa啊QAQ。。。

(我是不是被骗了。。)

感觉思路没毛病啊 自己对拍了几组小数据都没问题,但是大数据就炸掉了QAQ

具体思路和题解区别

/*Heroes Never Die!*/
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
using namespace std;
#define MAXN 40006
int n,m;
int A[MAXN],S[250][MAXN],P[250][250],idx[MAXN],bl[MAXN],blo,bac[MAXN];
//S i j 前i块j的个数
//P i j i块和j块中的众数
bool cmp(int a,int b){ return A[a] < A[b]; }
void init();
int QuerY(int l,int r);
int main(){
    //freopen("pwq","w",stdout);
    cin >> n >> m;
    for(int i=1;i<=n;++i) scanf("%d",&A[i]),idx[i] = i;
    sort(idx+1,idx+1+n,cmp);
    for(int i=0,cur=1,old;i<=n;++cur)
        do old = A[idx[i+1]],bac[cur] = A[idx[i+1]],A[idx[++i]] = cur; while(old == A[idx[i+1]] && i < n);//离散化
    init();int last = 0,l,r;
    while(m--){
        scanf("%d%d",&l,&r);
        l = (l + last - 1) % n + 1,r = (r + last - 1) % n + 1;
        cout << (last = bac[QuerY(l,r)]) << endl;
    }
}
int c[MAXN];
void init(){
    blo = sqrt(n);
    for(int i=1;i<=n;++i) bl[i] = (i-1)/blo + 1,++S[bl[i]][A[i]];
    for(int i=1;i<=bl[n];++i)
        for(int j=1;j<=n;++j)  S[i][j] += S[i-1][j];
    for(int i=bl[1];i<=bl[n];++i){//i : current start block
        int maxres = 0,curans = 0;memset(c,0,sizeof c);
        for(int j = i; j <= bl[n];++j){//j : current ending block
            for(int k = (j-1)*blo+1;k <= min((j)*blo,n);++k) {
                ++c[A[k]];
                if(c[A[k]] > maxres)  maxres = c[A[k]],curans = A[k];
                else if(c[A[k]] == maxres) if(A[k] < curans) maxres = c[A[k]],curans = A[k];
            }
            P[i][j] = curans;
        }
    }//O(nsqrt(n));
}
int vis[MAXN];
int QuerY(int l,int r){
    if(l > r) swap(l,r);
    int res = 0,cur;
    memset(c,0,sizeof c);
    if(bl[r] - bl[l] <= 1) {
        for(int i=l;i<=r;++i){
            ++c[A[i]];
            if(c[A[i]] > res) res = c[A[i]],cur = A[i];
            else if(c[A[i]] == res) if(A[i] < cur) res = c[A[i]],cur = A[i];
        }
        return cur;
    }
    memset(vis,0,sizeof vis);
    int ansm = P[bl[l]+1][bl[r]-1],an = S[bl[r]-1][ansm] - S[bl[l]][ansm];
    for(int i = l;i <= min(n,(bl[l])*blo);++i){
        if(!vis[A[i]]) vis[A[i]] = true,c[A[i]] += S[bl[r]-1][A[i]] - S[bl[l]][A[i]];
        ++c[A[i]];
        if(c[A[i]] > res) res = c[A[i]],cur = A[i];
        else if(c[A[i]] == res) if(A[i] < cur) res = c[A[i]],cur = A[i];
        if(A[i] == ansm) ++an;
    }
    for(int i = r;i >= (bl[r]-1)*blo+1;--i) {
        if(!vis[A[i]]) vis[A[i]] = true,c[A[i]] += S[bl[r]-1][A[i]] - S[bl[l]][A[i]];
        ++c[A[i]];
        if(c[A[i]] > res) res = c[A[i]],cur = A[i];
        else if(c[A[i]] == res) if(A[i] < cur) res = c[A[i]],cur = A[i];
        if(A[i] == ansm) ++an;
    }
    int an1 = res + S[bl[r]-1][cur] - S[bl[l]][cur];
    if(an1 == an) return cur < ansm?cur:ansm;
    return an1 > an?cur:ansm;
}

by yijan @ 2018-10-18 17:54:12

求解 啊 QaQ


by C201914 @ 2018-10-18 17:59:59

刚学OI就会分块%%%


by 基础不牢 @ 2018-10-18 19:40:39

你谷竟有入此之多的大佬刚学OI(震惊


by ExBritainia @ 2019-05-26 10:17:39

刚学OI就做黑题(震惊


|