NekoMio's Blog

美しいものが起こることを常に信じている

  1. 1. Description
  2. 2. Input
  3. 3. Output
  4. 4. Sample Input & Output
  5. 5. HINT
  • 题解
  • Description

    T3des(2).gif

    Input

    T3input(2).gif

    Output

    T3output(2).gif

    Sample Input & Output

    HINT

    T3hint(2).gif

    题解

    KD-Tree带插入的板子。
    应该rebuild的。
    但没rebuild就过了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>

    using namespace std;

    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-'0';ch=getchar();}
    return x*f;
    }

    const int INF = 0x3f3f3f3f;
    const double alpha = 0.756;
    const int MAXN = 5e5 + 5;

    int now;

    struct Point
    {
    int d[2];
    int &operator[](const int &x)
    {
    return d[x];
    }
    inline bool operator < (const Point &x) const
    {
    return d[now] == x.d[now] ? d[now ^ 1] < x.d[now ^ 1] : d[now] < x.d[now];
    }
    }a[MAXN], cur;

    #define dis(_, __) (\
    int(abs(_[0] - __[0]) + abs(_[1] - __[1]))\
    )

    #define size(_) ((_) ? (_)->s : 0)

    struct Node
    {
    Node *ch[2];
    Point v;
    int s, d;
    int Max[2], Min[2];
    Node(Point x)
    {
    ch[0] = ch[1] = NULL;
    v = x;
    s = 1, d = now;
    Max[0] = Min[0] = x[0];
    Max[1] = Min[1] = x[1];
    }
    Node(){;}
    inline bool operator < (const Node &x) const
    {
    return v < x.v;
    }
    bool IsBad()
    {
    return ((size(ch[0]) > s * alpha) || (size(ch[1]) > s * alpha));
    }
    void Pushup(Node *x)
    {
    if (!x) return;
    for (int i = 0; i <= 1; i++) Min[i] = min(Min[i], x->Min[i]);
    for (int i = 0; i <= 1; i++) Max[i] = max(Max[i], x->Max[i]);
    s += x->s;
    }
    int min_dis()
    {
    int ans = 0;
    ans += max(Min[0] - cur[0], 0) + max(cur[0] - Max[0], 0);
    ans += max(Min[1] - cur[1], 0) + max(cur[1] - Max[1], 0);
    return ans;
    }
    }*root;

    inline void Build(Node *&rt, int l, int r, int d = 0)
    {
    if (l > r) return;
    int mid = l + r >> 1;
    now = d;
    nth_element(a + l, a + mid, a + r + 1);
    rt = new Node(a[mid]);
    Build(rt->ch[0], l, mid - 1, d ^ 1);
    Build(rt->ch[1], mid + 1, r, d ^ 1);
    rt->s = 1;
    rt->Pushup(rt->ch[0]);
    rt->Pushup(rt->ch[1]);
    }

    Node **res;

    inline void Insert(Node *&rt)
    {
    if (rt == NULL)
    {
    rt = new Node(cur);
    res = NULL;
    return;
    }
    now = rt->d;
    if (cur < rt->v) Insert(rt->ch[0]);
    else Insert(rt->ch[1]);
    rt->s = 1;
    rt->Pushup(rt->ch[0]);
    rt->Pushup(rt->ch[1]);
    if (rt->IsBad()) res = &rt;
    }


    inline void Insert(Point x)
    {
    cur = x;
    Insert(root);
    }

    int Min_ans;

    inline void Query(Node *rt)
    {
    if (!rt) return;
    // if (rt->min_dis() > Min_ans) return;
    Min_ans = min(Min_ans, dis(rt->v, cur));
    int dis_l = rt->ch[0] ? rt->ch[0]->min_dis() : INF;
    int dis_r = rt->ch[1] ? rt->ch[1]->min_dis() : INF;
    if (dis_l < dis_r)
    {
    Query(rt->ch[0]);
    if (dis_r < Min_ans) Query(rt->ch[1]);
    }
    else
    {
    Query(rt->ch[1]);
    if (dis_l < Min_ans) Query(rt->ch[0]);
    }
    }

    inline int Query(Point x)
    {
    cur = x;
    Min_ans = INF;
    Query(root);
    return Min_ans;
    }

    int main()
    {
    // freopen("1.in", "r", stdin);
    // freopen("2.out", "w", stdout);
    int n, m;
    n = read(), m = read();
    for (int i = 1; i <= n; i++)
    a[i][0] = read(), a[i][1] = read();
    Build(root, 1, n);
    Point x;
    while (m--)
    {
    int t = read();
    if (t == 1)
    {
    x[0] = read(), x[1] = read();
    Insert(x);
    }
    else
    {
    x[0] = read(), x[1] = read();
    printf ("%d\n", Query(x));
    }
    }
    }

    本文作者 : NekoMio
    知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
    本文链接 : https://www.nekomio.com/2017/12/14/121/

    本文最后更新于 天前,文中所描述的信息可能已发生改变