Mn Zn 刚学KDT 1ms 求调
查看原帖
Mn Zn 刚学KDT 1ms 求调
817044
cjwdyzxfblzs楼主2023/7/3 19:43

不知道怎么回事,只能得27pts,只有#2, #7, #6 AC了。其它的点全 WA 了

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sq(u) (u) * (u)
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N = 1e6;
const double alpha = 0.70113;
const int K = 2;
struct node { int dim[K], size, d; } tr[N], Que[N];
int n, m, tot; int order[N], cnt, cur;
int L[N], R[N], D[N], U[N], lc[N], rc[N], root;
inline bool cmpx(int a, int b) { return Que[a].dim[0] < Que[b].dim[0]; }
inline bool cmpy(int a, int b) { return Que[a].dim[1] < Que[b].dim[1]; }
inline void update(int x)
{
    L[x] = R[x] = Que[x].dim[0];
    D[x] = U[x] = Que[x].dim[1];
    tr[x].size = tr[lc[x]].size + tr[rc[x]].size + 1;
    if (lc[x]) 
        L[x] = min(L[x], L[lc[x]]), R[x] = max(R[x], R[lc[x]]),
        D[x] = min(D[x], D[lc[x]]), U[x] = max(U[x], U[lc[x]]);
    if (rc[x])
        L[x] = min(L[x], L[rc[x]]), R[x] = max(R[x], R[rc[x]]),
        D[x] = min(D[x], D[rc[x]]), U[x] = max(U[x], U[rc[x]]);
}
inline int build(int l, int r, int op)
{
    if (l > r) return 0;
    int mid = (l + r) >> 1;
    if (!op) nth_element(order + l, order + mid, order + r + 1, cmpx);
    else nth_element(order + l, order + mid, order + r + 1, cmpy);
    lc[order[mid]] = build(l, mid - 1, op ^ 1);
    rc[order[mid]] = build(mid + 1, r, op ^ 1);
    update(order[mid]);
    tr[order[mid]].d = op;
    return order[mid];
}
inline void pia(int u)
{
    if (!u) return;
    pia(lc[u]);
    order[ ++ cnt] = u;
    pia(rc[u]);
}
inline void rebuild(int u)
{
    cnt = 0;
    pia(u);
    u = build(1, cnt, 1);
}
inline bool not_balance(int x)
{
    if (tr[lc[x]].size >= (double)tr[x].size * alpha || tr[rc[x]].size >= (double)tr[x].size * alpha)
        return true;
    return false;
}
void insert(int &u, int v)
{
    if (!u)
    {
        u = v;
        update(u);
        return;
    }
    if (!tr[u].d)
    {   
        if (Que[v].dim[0] <= Que[u].dim[0])
            insert(lc[u], v);
        else insert(rc[u], v);
    }
    else 
    {
        if (Que[v].dim[1] <= Que[u].dim[1])
            insert(lc[u], v);
        else insert(rc[u], v);
    }
    update(u);
    if (not_balance(u))
        rebuild(u);
}
// inline void abs(int x) { return x >= 0 ? x : -x; }
inline int dist(int a, int b) 
{ 
    return  abs(tr[a].dim[0] - tr[b].dim[0]) 
                           + 
            abs(tr[a].dim[1] - tr[b].dim[1]);
}
inline int dist(int x1, int y1, int x2, int y2)  { return abs(x1 - x2) + (y1 - y2); }
inline int f(int b, node a)
{
    int res = 0;
    if (L[b] > a.dim[0]) res += abs(L[b] - a.dim[0]);
    if (R[b] < a.dim[0]) res += abs(a.dim[0] - R[b]);
    if (D[b] > a.dim[1]) res += abs(D[b] - a.dim[1]);
    if (U[b] < a.dim[1]) res += abs(a.dim[1] - U[b]);
    return res;
}
inline int dist(node a, node b) { return abs(a.dim[0] - b.dim[0]) + abs(a.dim[1] - b.dim[1]); }
int minn = 0x3f3f3f3f3f;
void query(int p, node a)
{
    if (!p) return;
    minn = min(minn, dist(Que[p], a));
    int distl = f(lc[p], a), distr = f(rc[p], a);
    if (distl <= minn and distr <= minn)
    {
        if (distl < distr)
        {
            query(lc[p], a);
            if (distr <= minn) query(rc[p], a);
        }
        else 
        {
            query(rc[p], a);
            if (distl <= minn) query(lc[p], a);
        }
    }
    else
    {
        if (distl <= minn) query(lc[p], a);
        if (distr <= minn) query(rc[p], a);
    }
}
signed main()
{
    // freopen("P4169_1.in", "r", stdin);
    // freopen("ans.out", "w", stdout);
    n = read(), m = read();
    for (int i = 1; i <= n; i ++ )
    {
        Que[i].dim[0] = read(), Que[i].dim[1] = read();
        order[i] = ++ cur;
    }
    root = build(1, n, 0);
    for (int i = 1; i <= m; i ++ )
    {
        int op = read();
        if (op == 1)
        {
            ++ cur;
            int x = read(), y = read();
            Que[cur].dim[0] = x, Que[cur].dim[1] = y;
            insert(root, cur);
        }   
        else 
        {
            node a;
            minn = 0x3f3f3f3f3f;
            a.dim[0] = read(), a.dim[1] = read();
            query(root, a);    
            cout << minn << endl;
        }
    }
    return 0;
}

2023/7/3 19:43
加载中...