【NOI2014模拟】数据

Description:

为了写论文,Alex经常要整理大量的数据。 这一次,Alex面临一个严峻的考验:他需要实现一个数据结构来维护一个点集。

现在,二维平面上有N个点。Alex 需要实现以下三种操作:

  1. 在点集里添加一个点;

  2. 给出一个点,查询它到点集里所有点的曼哈顿距离的最小值;

  3. 给出一个点,查询它到点集里所有点的曼哈顿距离的最大值。

两个点的曼哈顿距离定义为它们的横坐标差的绝对值与纵坐标差的绝对值的和。这么困难的问题,Alex当然不会做,只好再次请你帮忙了。

1 ≤ N, Q ≤ 100,000,点的坐标是不超过10^8的非负整数。

题解:

在线好像只能动态开点二维线段树了,时空炸飞。

离线这就是cdq分治的裸题。

用cdq分治去分治掉时间,按x排序,用离散后的y坐标建线段树或树状数组。

线段树要打两个(struct直接上),树状数组要打八个,实测树状数组更快,线段树想要过就要用一些比较玄的剪枝了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define fd(i, x, y) for(int i = x; i >= y; i --)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define low(x) ((x) & -(x))
using namespace std;

const int N = 2e5 + 5, INF = 2e9;

int n, m, Q, tz;

struct node {
    ll x, y; int z, i, p;
} a[N * 2], b[N * 2];


bool cmp(node a, node b) {return a.x < b.x;}

void Li() {
    fo(i, 1, n) b[i].x = a[i].y, b[i].y = i;
    sort(b + 1, b + n + 1, cmp);
    fo(i, 1, n) {
         if(i == 1 || b[i].x != b[i - 1].x) tz ++;
         a[b[i].y].z = tz;
    }
}

ll s;

struct tree1 {
    int f1[N], f2[N], f3[N], f4[N];
    void G() {
        memset(f1, 127, sizeof f1);
        memset(f3, 127, sizeof f3);
        memset(f2, 128, sizeof f2);
        memset(f4, 128, sizeof f4);
    }
    void ch(int x, ll c) {
        int y = x;
        while(y <= tz) f1[y] = min(f1[y], c), f2[y] = max(f2[y], c), y += low(y);
        y = tz - x + 1;
        while(y <= tz) f3[y] = min(f3[y], c), f4[y] = max(f4[y], c), y += low(y);
    }
    void cl(int x) {
        int y = x;
        while(y <= tz) f1[y] = INF, f2[y] = -INF, y += low(y);
        y = tz - x + 1;
        while(y <= tz) f3[y] = INF, f4[y] = -INF, y += low(y);
    }
    void fi(int x, int y, int c) {
        s = c ? -INF : INF;
        if(x == 1) {
            if(!c) {
                while(y) s = min(s, f1[y]), y -= low(y);
            } else {
                while(y) s = max(s, f2[y]), y -= low(y);
            }
        } else {
            y = tz - x + 1;
            if(!c) {
                while(y) s = min(s, f3[y]), y -= low(y);
            } else {
                while(y) s = max(s, f4[y]), y -= low(y);
            }
        }
    }
} t1, t2;

void insert(node a) {
    t1.ch(a.z, a.x + a.y);
    t2.ch(a.z, a.x - a.y);
}

ll s1[N], s2[N];

void C1(node a) {
    int i = a.i; ll x = a.x, y = a.y, z = a.z;
    t1.fi(1, z, 1); s1[i] = min(s1[i], x + y - s);
    t2.fi(z, tz, 1); s1[i] = min(s1[i], x - y - s);
    t1.fi(1, z, 0); s2[i] = max(s2[i], x + y - s);
    t2.fi(z, tz, 0); s2[i] = max(s2[i], x - y - s);
}

void C2(node a) {
    int i = a.i; ll x = a.x, y = a.y, z = a.z;
    t2.fi(1, z, 0); s1[i] = min(s1[i], -x + y + s);
    t1.fi(z, tz, 0); s1[i] = min(s1[i], -x - y + s);
    t2.fi(1, z, 1); s2[i] = max(s2[i], -x + y + s);
    t1.fi(z, tz, 1); s2[i] = max(s2[i], -x - y + s);
}

void dg(int x, int y) {
    if(x == y) return;
    int z = x + y >> 1;
    dg(x, z); dg(z + 1, y);

    int l = x;
    fo(i, z + 1, y) if(b[i].p) {
        while(l <= z && b[l].x <= b[i].x) {
            if(!b[l].p) insert(b[l]);
            l ++;
        }
        C1(b[i]);
    }
    fo(i, x, z) t1.cl(b[i].z), t2.cl(b[i].z);
    int r = z;
    fd(i, y, z + 1) if(b[i].p) {
        while(r >= x && b[r].x >= b[i].x) {
            if(!b[r].p) insert(b[r]);
            r --;
        }
        C2(b[i]);
    }
    fo(i, x, z) t1.cl(b[i].z), t2.cl(b[i].z);

    sort(b + x, b + y + 1, cmp);
}

int ans[N];

int main() {
    scanf("%d", &n);
    fo(i, 1, n) scanf("%d %d", &a[i].x, &a[i].y);
    scanf("%d", &Q);
    fo(i, n + 1, n + Q) scanf("%d %d %d", &a[i].p, &a[i].x, &a[i].y);
    memset(s1, 127, sizeof s1);
    memset(s2, 128, sizeof s2);
    t1.G(); t2.G();
    n += Q;
    Li();
    fo(i, 1, n) b[i] = a[i], b[i].i = i;
    dg(1, n);
    fo(i, 1, n) {
        if(a[i].p == 1) printf("%lld\n", s1[i]);
        if(a[i].p == 2) printf("%lld\n", s2[i]);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值