BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 +CDQ分治

1935: [Shoi2007]Tree 园丁的烦恼

参考与学习:https://www.cnblogs.com/mlystdcall/p/6219421.html

题意

  在一个二维平面中有n颗树,有m次询问,要求回答在一个矩形方框中的树的个数。

思路

  这是一个(x,y)为偏序的题目。这道题先用CDQ对x进行排序降维。然后利用树状数组对y进行处理。复杂度为O(N*logN * logN)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l, mid, rt << 1)
#define rson (mid + 1, r, rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC                      \
    ios::sync_with_stdio(false); \
    cin.tie(0)
#define FT(A, B, C) for (int A = B; A <= C; ++A) //用来压行
#define REP(i, j, k) for (int i = j; i < k; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const double PI = acos(-1.0);

template <typename T>
inline T read(T &x) {
    x = 0;
    int f = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return x = f ? -x : x;
}

/*-----------------------show time----------------------*/
const int maxl = 10000009;
const int maxn = 500009;

struct node {
    int id, x, y, type, val;

    bool operator<(const node &a) const {
        if (a.x == x) return type < a.type;
        return x < a.x; //按照x排序
    }
} a[maxn * 5], b[maxn * 5];
ll ans[maxn];
int n, m, tot, askid, maxy = -1;

void add(int type, int x, int y, int val, int id) {
    tot++;
    a[tot].type = type;
    a[tot].x = x;
    a[tot].y = y;
    a[tot].val = val;
    a[tot].id = id;
}
ll sum[maxl]; //树状数组
int lowbit(int x) {
    return x & (-x);
}
void update(int x, int c) {
    while (x <= maxy) {
        sum[x] += c;
        x += lowbit(x);
    }
}
ll query(int x) {
    ll cnt = 0;
    while (x > 0) {
        cnt += sum[x];
        x -= lowbit(x);
    }
    return cnt;
}
void clearb(int x) {
    while (x <= maxy) {
        if (sum[x])
            sum[x] = 0;
        else
            break;
        x += lowbit(x);
    }
}
void CDQ(int L, int R) {
    if (L >= R) return;
    int mid = (L + R) >> 1;
    CDQ(L, mid);
    CDQ(mid + 1, R);

    int q1 = L, q2 = mid + 1;
    for (int i = L; i <= R; i++) {
        if ((q1 <= mid && a[q1] < a[q2]) || q2 > R) {
            if (a[q1].type == 0) {
                update(a[q1].y, 1);
            }
            b[i] = a[q1++];
        } else {
            if (a[q2].type == 1) ans[a[q2].id] += a[q2].val * query(a[q2].y);
            b[i] = a[q2++];
        }
    }
    for (int i = L; i <= R; i++) {
        a[i] = b[i];
        clearb(b[i].y);
    }
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        int x, y;
        tot++;
        scanf("%d%d", &x, &y);
        x += 2, y += 2;
        a[tot].x = x;
        a[tot].y = y;
        a[tot].id = 0;
        a[tot].type = 0;
        maxy = max(maxy, y);
    }

    for (int i = 1; i <= m; i++) {
        int x1, y1, x2, y2;
        askid++;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        x1 += 2, y1 += 2, x2 += 2, y2 += 2;
        maxy = max(maxy, max(y1, y2));
        add(1, x1 - 1, y1 - 1, 1, askid);
        add(1, x1 - 1, y2, -1, askid);
        add(1, x2, y1 - 1, -1, askid);
        add(1, x2, y2, 1, askid);
    }
    CDQ(1, tot);
    for (int i = 1; i <= askid; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}
View Code

 

posted @ 2018-08-25 23:32  ckxkexing  阅读(398)  评论(1编辑  收藏  举报