P1527 [国家集训队]矩阵乘法 题解

· · 题解

2022.10.11 添加了必要的卡常技巧,修改了部分笔误。

题意

给你一个 n×n 的矩阵,每次询问一个子矩形的第 k 小数。

思路

这是一道二维莫队的一眼题,先离散化,用莫队维护出矩形中出现的数,再值域分块即可。

这里主要讲一下二维莫队。

二维莫队,顾名思义,是将莫队放在二维上跑,同时有四个指针,表示上下左右。

单次移动指针一次加入或删除一整排,故复杂度略高。

关于最优块长与复杂度,设块长为 B, 上指针共移动 \dfrac{n}{B} 块,每块左右指针共 (\dfrac{n}{B})^2 次,

故上指针共 qB 次,取左指针移动 \dfrac{n^4}{B^3} 次,让 \dfrac{n^4}{B^3}=qB, 得 B=nq^{-\tfrac{1}{4}}, 时间复杂度为 O(n^2q^\tfrac{3}{4}), 在本题要开到这个块长的 \dfrac{1}{3} 左右,稍加卡常即可。

至于值域分块,就是对于数的值域进行分块,记录出现在该值域和值域块的数的个数。

于是查询时就可以暴力跳块,再在块内找即可,单次时间复杂度 O(\sqrt{n})。

优化

仅做到上面的部分只能得到 60pts, 剩下还需要加上一个莫队常用的卡常技巧——奇偶排序。

就是代码中的这一行:

return c[x.u]==c[y.u]?(c[x.l]==c[y.l]?(c[x.r]==c[y.r]?((c[x.r]&1)?x.d<y.d:x.d>y.d):((c[x.l]&1)?x.r<y.r:x.r>y.r)):((c[x.u]&1)?x.l<y.l:x.l>y.l)):x.u<y.u;

模拟一下莫队过程可以发现,每次一端指针跑到边缘后又会跑回来,再开始往外跑,这里浪费了一遍移动指针的过程。

那么怎么利用上这一段呢?

我们可以将指针跑回来的再跑出去的这一段询问逆序排序,那么再指针往回跑时就可以顺带处理这部分询问了。

而这一部分恰好是另一端指针处于偶数块的时候,故按奇偶分顺逆序即可。

加上这个优化就可以过了,通过记录。

代码

#include<bits/stdc++.h>
using namespace std;
namespace io {
    const int __SIZE = (1 << 21) + 1;
    char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
    #define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
    inline void gc (char &x) { x = Gc(); }
    inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
    inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
    inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
        for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) *s = __c; *s = 0; }
    template <class I> inline bool gi (I &x) { _eof = 0;
        for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
        for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
    template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
        while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;
int n,m,b[250001],ans[60001],p[501][501];
int cl,c[501],s[250001],sum[501],ccl,cc[250001];
struct xw
{
    int u,l,d,r,h,id;
}q[60001];
inline bool cmp(xw x,xw y){return c[x.u]==c[y.u]?(c[x.l]==c[y.l]?(c[x.r]==c[y.r]?((c[x.r]&1)?x.d<y.d:x.d>y.d):((c[x.l]&1)?x.r<y.r:x.r>y.r)):((c[x.u]&1)?x.l<y.l:x.l>y.l)):x.u<y.u;}
inline int Q(register int x)
{
    int pos=1,cnt=0;
    while(cnt+sum[pos]<x&&pos<=cc[n*n])cnt+=sum[pos],++pos;
    pos=(pos-1)*ccl+1;
    while(cnt+s[pos]<x&&pos<=n*n)cnt+=s[pos],++pos;
    return b[pos];
}
int main()
{
    register int uu=1,dd=1,ll=1,rr=1;
    gi(n);
    gi(m);
    cl=n/pow(m,0.25)/3+1;
    ccl=n+1;
    for(register int i=1;i<=n;++i)c[i]=(i-1)/cl+1;
    for(register int i=1;i<=n*n;++i)cc[i]=(i-1)/ccl+1;
    for(register int i=1;i<=n;++i)
    {
        for(register int j=1;j<=n;++j)
        {
            gi(p[i][j]);
            b[++b[0]]=p[i][j];
        }
    }
    sort(b+1,b+1+b[0]);
    b[0]=unique(b+1,b+1+b[0])-b-1;
    for(register int i=1;i<=n;++i)
    {
        for(register int j=1;j<=n;++j)
        {
            p[i][j]=lower_bound(b+1,b+1+b[0],p[i][j])-b;
        }
    }
    for(register int i=1;i<=m;++i)gi(q[i].u),gi(q[i].l),gi(q[i].d),gi(q[i].r),gi(q[i].h),q[i].id=i;
    sort(q+1,q+1+m,cmp);
    ++s[p[1][1]];++sum[cc[p[1][1]]];
    for(register int i=1;i<=m;++i)
    {
        while(rr<q[i].r)
        {
            ++rr;
            for(register int i=uu;i<=dd;++i)++s[p[i][rr]],++sum[cc[p[i][rr]]];
        }
        while(ll>q[i].l)
        {
            --ll;
            for(register int i=uu;i<=dd;++i)++s[p[i][ll]],++sum[cc[p[i][ll]]];
        }
        while(dd<q[i].d)
        {
            ++dd;
            for(register int i=ll;i<=rr;++i)++s[p[dd][i]],++sum[cc[p[dd][i]]];
        }
        while(uu>q[i].u)
        {
            --uu;
            for(register int i=ll;i<=rr;++i)++s[p[uu][i]],++sum[cc[p[uu][i]]];
        }
        while(rr>q[i].r)
        {
            for(register int i=uu;i<=dd;++i)--s[p[i][rr]],--sum[cc[p[i][rr]]];
            --rr;
        }
        while(ll<q[i].l)
        {
            for(register int i=uu;i<=dd;++i)--s[p[i][ll]],--sum[cc[p[i][ll]]];
            ++ll;
        }
        while(dd>q[i].d)
        {
            for(register int i=ll;i<=rr;++i)--s[p[dd][i]],--sum[cc[p[dd][i]]];
            --dd; 
        }
        while(uu<q[i].u)
        {
            for(register int i=ll;i<=rr;++i)--s[p[uu][i]],--sum[cc[p[uu][i]]];
            ++uu;
        }
        ans[q[i].id]=Q(q[i].h);
    }
    for(register int i=1;i<=m;++i)printf("%d\n",ans[i]);
    return 0;
}