题解:P14340 [JOISC 2019] 考试 / Examination

· · 题解

一个几乎裸的三维偏序。

注意到时间限制给了 3 秒,那么众所周知这个三维偏序不但可以用 CDQ 分治做,还可以用一些神秘的数据结构做。

比如,K-D Tree。

按照朴素 K-D Tree 模板,我们需要维护一个子树内每一维的最大最小值 mx 和 mn,以及子树内点的数量 cnt。

设询问给出的三元组为 (d_0,d_1,d_2),那么查询时就会有以下三种情况:

那么这个复杂度按照正常来说应该是 O(n^{\frac{5}{3}}) 的,如果数据随机应该能跑挺快,3 秒应该能稳过。

但是这题的数据不随机。

那咋办?上优化呗。

首先非递归函数都加一个内联,然后你注意到这个数据范围都在 int 以内,并且都是正数,那你就改成 unsigned int,这样能快一点。还有什么快读快写都加上去。

然后是最关键的优化,我们注意到朴素 K-D Tree 的优化是随机插入点的顺序来防止极端点导致递归过多,但是这样还是可能有大量极端点,所以我们直接以第一维,第二维,第三维分别为第一第二第三关键字来排序,然后在插入点即可。

于是你就使用 K-D Tree 过掉了这道 O(nq) 都能过但朴素 O(n^{\frac{5}{3}}) 过不了的题。

::::success[代码]

#include<bits/stdc++.h>
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
    static unsigned int precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
    static char obuf[IOSIZE], *p3 = obuf;
#ifdef ONLINE_JUDGE
    static char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#endif
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    inline int64_t read() { int64_t s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> static inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    template<typename T> static inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
    static inline bool read(char &s) { while (s = getchar(), isspace(s) and s != EOF); return s != EOF; }
    static inline void print(char x) { putchar(x); }
    static inline bool read(char *s) { char ch; while (ch = getchar(), (ch<33) && ch != EOF); if (ch == EOF) return false; while (!(ch<33) and (ch != EOF)) *s++ = ch, ch = getchar(); *s = '\0'; return true; }
    static inline void print(char *x) { while (*x) putchar(*x++); }
    static inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    static inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), (ch<33) and ch != EOF); if (ch == EOF) return false; while (!(ch<33)) s += ch, ch = getchar(); return true; }
    static inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    static inline bool read(bool &b) { char ch; while(ch=getchar(), (ch<33) and ch != EOF); return ch == EOF ?false :(b=ch^48, true); }
    static inline void print(bool b) { putchar(b+48); }
    static inline bool read(double &x) { char ch = getchar(); int f = 1; for(; (ch<48 or 57<ch) and (ch != EOF); ch=getchar()) if(ch == '-')    f = -1; if(ch == EOF)    return false; for(x=0; 47<ch and ch<58; ch=getchar())    x = x*10 + (ch^48); if(ch != '.')    return x *= f, true; double y = 0.1; for(ch=getchar(); 47<ch and ch<58; ch=getchar())    x += y*(ch^48), y /= 10; return x *= f, true; }
    static inline void print(double x) { if(x < 0) putchar('-'), x = -x; if(!precision) print((unsigned long long)(x-(unsigned long long)(x)>=0.5 ?x+1 :x)); else { unsigned long long xx = x; double y = ((x-xx)*POW[precision]); unsigned long long yy = (y-(unsigned long long)(y)>=0.5 ?y+1 :y); if(yy == POW[precision])    xx++, yy = 0; print(xx), putchar('.'); for(int j=precision-1; ~j; j--)    putchar(48+yy/POW[j]%10); } }
    template<typename T, typename... T1> static inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> static inline void print(T a, T1... other) { print(a), print(other...); }
    static struct Fast_IO {
        bool flag = 1;
        inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
        inline void SetPrecision(int x) { if(x > 9)    throw runtime_error("Precision too high!"); else if(x < 0) throw runtime_error("Precision too low!"); else precision = x; }
        inline operator bool() { return flag; }
    } io;
    Fast_IO& operator >> (Fast_IO &io, char *b) { return io.flag &= read(b), io; }
    template<typename T> static Fast_IO& operator >> (Fast_IO &io, T &b) { return io.flag &= read(b), io; }
    template<typename T> static Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;

#define int unsigned int
const int N=2e5+10,M=18;
int n,q;
struct node{
    int ls,rs;
    int d[3],minn[3],maxn[3];
    int sum; 
}t[N];
int b[N],cnt=0,rt[M];
inline void push_up(int x){
    t[x].sum=t[t[x].ls].sum+t[t[x].rs].sum+1;
}
inline void update(int x){
    for(int i=0;i<=2;i++){
        t[x].minn[i]=t[x].maxn[i]=t[x].d[i];
        int ls=t[x].ls,rs=t[x].rs;
        if(ls){
            t[x].minn[i]=min(t[x].minn[i],t[ls].minn[i]);
            t[x].maxn[i]=max(t[x].maxn[i],t[ls].maxn[i]);
        }
        if(rs){
            t[x].minn[i]=min(t[x].minn[i],t[rs].minn[i]);
            t[x].maxn[i]=max(t[x].maxn[i],t[rs].maxn[i]);
        }
    }
    push_up(x);
}
inline void dfs(int &x){
    if(x==0)return;
    b[++cnt]=x;
    dfs(t[x].ls);
    dfs(t[x].rs);
    x=0;
}
inline int build(int l,int r,int op){
    int mid=l+r>>1;
    nth_element(b+l,b+mid,b+r+1,[op](int x,int y){return t[x].d[op]<t[y].d[op];});
    int x=b[mid];
    if(l<mid)t[x].ls=build(l,mid-1,(op+1)%3);
    if(r>mid)t[x].rs=build(mid+1,r,(op+1)%3);
    update(x);
    return x;
}
int p[3];
inline int ask(int x){
    if(x==0)return 0;
    bool f=true;//整个区间是否都可以 
    for(int i=0;i<=2;i++){
        if(t[x].minn[i]<p[i])f=false;
    }
    if(f)return t[x].sum;
    f=false;
    for(int i=0;i<=2;i++){
        if(t[x].maxn[i]<p[i])f=true;
    }
    if(f)return 0;
    f=false;
    for(int i=0;i<=2;i++){
        if(t[x].d[i]<p[i])f=true;
    }
    int res=0;
    if(!f)res++;
    res=res+ask(t[x].ls);
    res=res+ask(t[x].rs);
    return res;
}
struct point{
    int x,y;
}a[N];
inline bool cmp(point &x,point &y){
    return x.x<y.x;
}
signed main(){
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
    srand(time(0));
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y;
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++){
        t[i].d[0]=a[i].x;t[i].d[1]=a[i].y;
        t[i].d[2]=t[i].d[0]+t[i].d[1];
        b[cnt=1]=i;t[i].sum=1;
        for(int j=0;j<M;j++){
            if(!rt[j]){
                rt[j]=build(1,cnt,0);
                break;
            }
            else dfs(rt[j]);
        }
    }
    while(q--){
        cin>>p[0]>>p[1]>>p[2];
        int ans=0;
        for(int j=0;j<M;j++)ans+=ask(rt[j]);
        cout<<ans<<"\n";
    }
    return 0;
}

::::