BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地(CDQ分治+离散化+线段树)

来源:互联网 发布:画结构式的软件 编辑:程序博客网 时间:2024/05/29 13:57

Description

欢乐岛上最著名的游戏是一个寻宝游戏,小可可来到宝藏的埋藏地,这是一块开阔地,宝藏被分散的埋藏在这块地下,现在要做的是一件件的把宝藏挖出来。为了提示宝藏的埋藏点,游戏的主办方把这块开阔地当作第一象限,将所有可能埋藏宝藏的地方划成一个个矩形的土地,并把这些矩形土地的坐标都告诉了参赛者。挖宝的提示很简单,只要某一个矩阵土地至少被另外一个矩阵土地所包含,那么这个矩阵土地里肯定埋有宝藏。其实这些宝藏都是一些精美的纪念品,如果谁挖出来了纪念品就归谁了,小可可很想为这次旅程画上完美的句号,有你的帮助他信心十足,你只要告诉他.有多少个矩形土地里肯定埋有宝藏就行了。胜利就在眼前,加油吧!!


Input

第一行包含一个整数N(N≤200000),表示矩形的个数。接下来N行,每行用4个整数x1,y1,x2,y2,描述了一个矩形。其中(x1,y1)表示这个矩形左下角的坐标,(x2,y2)表示右上角的坐标,一个xi值或yi值最多出现一次.


Output

只包含一个整数,表示肯定埋有宝藏的矩形土地的个数。


Sample Input

3
0 0 5 5
1 2 3 4
2 1 4 3


Sample Output

2


HINT

100%的数据中,N<=200000
70%的数据申,N<=50000
30%的数据中,N<=5000
所有数据中,一个x值或Y值最多出现一次


Solution

这是一道CDQ的入门题,适合我这种菜鸡。

我设左下角的坐标为(a,b), 右上角为(c,d)。对于一个被包围的矩形i,必然存在一个矩形j,满足ai>ajbi>bjci<cjdi<dj。那么一开始按a排好序,这就变成了一个四维偏序的问题了。

直接上CDQ分治解决。由于只是判断存不存在,不用维护个数,那么利用线段树能维护两维,离散化后下标维护一维,再维护个最大值。实际上这题就是个三维偏序的果题了。

排好第一维后,划分序列,左边为修改,右边为查询。然后按第二维排序,并根据操作类型(依据第一维)以及排完序后的前后顺序(第二维),执行权值线段树上的修改和询问。若满足前三维顺序成立的前提下,第四维的最大值比当前大,代表出现答案,标记它,最后再统计答案。这题有点像之前模拟赛的“指纹”那题,由于多了一维偏序,所以要多搞个CDQ。最后别忘了线段树反向标记和操作类型标记要清掉(和整体二分一样不能忘),然后处理左右之前别忘了排回原序。

思路大概就是这样,时间复杂度O(nlog2n)

ps:这题我的程序又被卡时限了,注意不要读入long long,用int永保平安。


Code

#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <iostream>#define MAXN 200010#define Max(a, b) ((a) > (b) ? (a) : (b))using namespace std;int n, rank[MAXN], ans;bool is_ok[MAXN], Left[MAXN];struct Data{    int a, b, c, d;    int id;}rec[MAXN];int tree[MAXN<<2];bool Cmpa(Data A, Data B){    return A.a < B.a;}bool Cmpb(Data A, Data B){    return A.b < B.b;}bool Cmpc(Data A, Data B){    return A.c > B.c;}void update(int root, int L, int R, int x, int v){    if(x > R || x < L)  return;    if(L == x && x == R){      tree[root] += v;      return;    }    int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;    update(Lson, L, mid, x, v);    update(Rson, mid+1, R, x, v);    tree[root] = Max(tree[Lson], tree[Rson]);}int query(int root, int L, int R, int x, int y){    if(x > R || y < L)  return 0LL;    if(x <= L && y >= R)  return tree[root];    int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;    int temp1 = query(Lson, L, mid, x, y);    int temp2 = query(Rson, mid+1, R, x, y);    return Max(temp1, temp2);}void CDQ(int L, int R){    if(L == R)  return;    int mid = (L + R) >> 1;    for(int i = L; i <= mid; i++)  Left[rec[i].id] = true;    sort(rec+L, rec+R+1, Cmpb);    for(int i = L; i <= R; i++){      if(Left[rec[i].id])  update(1, 1, n, rank[rec[i].id], rec[i].d);      else{        int temp = query(1, 1, n, 1, rank[rec[i].id]);        if(temp > rec[i].d)  is_ok[rec[i].id] = true;      }    }    for(int i = L; i <= R; i++)        if(Left[rec[i].id]){          update(1, 1, n, rank[rec[i].id], -rec[i].d);        Left[rec[i].id] = false;     }    sort(rec+L, rec+R+1, Cmpa);    CDQ(L, mid);    CDQ(mid+1, R);}int main(){    freopen("bzoj1790.in", "r", stdin);    freopen("bzoj1790.out", "w", stdout);    scanf("%d", &n);    for(int i = 1; i <= n; i++){      rec[i].id = i;      scanf("%d%d%d%d", &rec[i].a, &rec[i].b, &rec[i].c, &rec[i].d);    }    sort(rec+1, rec+n+1, Cmpc);    for(int i = 1; i <= n; i++)  rank[rec[i].id] = i;    sort(rec+1, rec+n+1, Cmpa);    CDQ(1, n);    for(int i = 1; i <= n; i++)  if(is_ok[i])  ans ++;    printf("%d\n", ans);    return 0;}

这里写图片描述

不相信世界上还有温情的你
却比任何人都温柔善良

原创粉丝点击