「题解」「JOISC 2014 Day1」历史研究

题目

点这里

考场思考

大概是标准的莫队吧,离散之后来一个线段树加莫队就可以了。

时间复杂度 \(\mathcal O(n\sqrt n\log n)\)

然而被卡常了...只有 \(40pts\) ...

自闭中...

#pragma GCC optimize(2)
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair<int,int>
#define Endl putchar('\n')
// #define FILEOI
#define int long long
// #define int unsigned
#define lc (i<<1)
#define rc (i<<1|1)

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1e5;

int a[MAXN+5],sqrtN,ans[MAXN+5];
int has[MAXN+5];

struct query{
    int id,l,r;
    query(const int I=0,const int L=0,const int R=0):id(I),l(L),r(R){}
    bool operator < (const query a)const{
        if((l/sqrtN)^(a.l/sqrtN))return (l/sqrtN)<(a.l/sqrtN);
        return r<a.r;
    }
}q[MAXN+5];
struct node{
    int l,r,mid,maxx;
    node(const int L=0,const int R=0):l(L),r(R),mid((L+R)>>1),maxx(0){}
}tre[MAXN<<2|2];
inline void buildtre(const int i,const int l,const int r){
    tre[i]=node(l,r);
    if(l==r)return;
    int mid=(l+r)>>1;
    buildtre(lc,l,mid);
    buildtre(rc,mid+1,r);
}
inline void pushup(const int i){
    tre[i].maxx=Max(tre[lc].maxx,tre[rc].maxx);
    return;
}
inline void add(const int i,const int p,const int delta){
    if(tre[i].l==tre[i].r){
        tre[i].maxx+=has[tre[i].l]*delta;
        return;
    }
    if(p<=tre[i].mid)add(lc,p,delta);
    else add(rc,p,delta);
    pushup(i);
}
inline void delet(const int i,const int p,const int delta){
    if(tre[i].l==tre[i].r){
        tre[i].maxx-=has[tre[i].l]*delta;
        return;
    }
    if(p<=tre[i].mid)delet(lc,p,delta);
    else delet(rc,p,delta);
    pushup(i);
}

int N,Q,X[MAXN+5],tmp[MAXN+5];

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    qread(N,Q);
    sqrtN=(int)sqrt(N*1.0);

    rep(i,1,N)tmp[i]=X[i]=qread();
    sort(tmp+1,tmp+N+1);
    int tN=unique(tmp+1,tmp+N+1)-tmp-1;
    int pos;
    rep(i,1,N){
        pos=lower_bound(tmp+1,tmp+tN+1,X[i])-tmp;
        has[pos]=X[i];
        X[i]=pos;
    }

    int x,y;
    rep(i,1,Q){
        qread(x,y);
        q[i]=query(i,x,y);
    }
    sort(q+1,q+Q+1);

    buildtre(1,1,tN);
    int l=1,r=0;
    rep(t,1,Q){
        while(r<q[t].r)add(1,X[++r],1);
        while(r>q[t].r)delet(1,X[r--],1);
        while(l<q[t].l)delet(1,X[l++],1);
        while(l>q[t].l)add(1,X[--l],1);
        ans[q[t].id]=tre[1].maxx;
    }
    rep(i,1,Q)writc(ans[i],'\n');
    return 0;
}

思路分析及标程

其实正解就是莫队,但是加入了一些小优化。

首先思考:如果没有回退操作怎么办?

那很简单,可以不用线段树维护了,这样我们就可以丢掉 \(\log\) 了。

但是怎么才能实现呢?

分析:我们的回退操作是在哪里涉及到的?

其实,对于一般莫队,在左端点都在同一个块里面的时候,我们的右端点都是递增的,这是无疑的。

但是我们的左端点却在进行较小范围的摆动,这让我们十分不爽,并只得加入线段树来进行优化。

如果询问的两个端点在同一个块中,直接暴力计算,时间复杂度 \(\mathcal O(\sqrt N)\)

如果不在同一个块中,这时候右端点是不断递增的,因此暴力计算右端点的复杂度为 \(\mathcal O(N)\)

但是左端点的位置在块内可是飘忽不定的啊,

简单,每次询问之后把左端点移动到所在块的最右段即可,每次计算左端点的复杂度为 \(\mathcal O(\sqrt N)\)

因为有 \(\mathcal O(\sqrt N)\) 个块,因此总的时间复杂度为 \(\mathcal O(N\sqrt N)\)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair<int,int>
#define Endl putchar('\n')
// #define FILEOI
#define int long long
// #define int unsigned
// #define lc (i<<1)
// #define rc (i<<1|1)

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1e5;

int ans[MAXN+5],maxx,alblock;
int N,Q,X[MAXN+5],has[MAXN+5],tN,block[MAXN+5],sqN,cnt[MAXN+5];

struct Query{
    int l,r,id;
    inline Query(const int L=0,const int R=0,const int I=0):l(L),r(R),id(I){}
    inline bool operator <(const Query rhs)const{
        if(block[l]==block[rhs.l])return r<rhs.r;
        return block[l]<block[rhs.l];
    }
}q[MAXN+5];

inline void init(){
    qread(N,Q);sqN=(int)sqrt(N*1.0);
    rep(i,1,N){
        X[i]=has[i]=qread();
        block[i]=(i+sqN-1)/sqN;
        alblock=Max(alblock,block[i]);
    }
    sort(has+1,has+N+1);
    tN=unique(has+1,has+N+1)-has-1;
    rep(i,1,N)X[i]=lower_bound(has+1,has+tN+1,X[i])-has;
}

inline void getQuery(){
    int l,r;
    rep(i,1,Q){
        qread(l,r);
        q[i]=Query(l,r,i);
    }
    sort(q+1,q+Q+1);
}

inline int solve(const int l,const int r){
    static int tmp[MAXN+5];int tmax=0;
    rep(i,l,r)tmp[X[i]]=0;
    rep(i,l,r)++tmp[X[i]],tmax=Max(tmax,tmp[X[i]]*has[X[i]]);
    return tmax;
}

inline void add(const int i){
    ++cnt[X[i]];
    maxx=Max(cnt[X[i]]*has[X[i]],maxx);
}

inline void delet(const int i){
    --cnt[X[i]];
}

inline int Get(int i,const int bid){
    int Br=bid*sqN,l=Br+1,r=l-1,cur;maxx=0;
    memset(cnt,0,sizeof cnt);
    for(;block[q[i].l]==bid;++i){
        if(block[q[i].l]==block[q[i].r]){
            ans[q[i].id]=solve(q[i].l,q[i].r);
            continue;
        }
        while(r<q[i].r)add(++r);
        cur=maxx;
        while(l>q[i].l)add(--l);
        ans[q[i].id]=maxx;
        while(l<Br+1)delet(l++);
        maxx=cur;
    }
    return i;
}

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    init();
    getQuery();

    for(int i=1,id=1;id<=alblock;++id)
        i=Get(i,id);

    rep(i,1,Q)writc(ans[i],'\n');
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/MachineryCountry/p/12149402.html