Luo Gu P2163 [SHOI2007] gardener troubles (offline sort, Fenwick tree, three-dimensional partial order to solve the problem)

P2163 [SHOI2007] gardener troubles

Title Description

Once upon a time, there is a beautiful country in a distant continent. This beautiful country ruled king is a gardening enthusiast, planted with a variety of exotic plants in his royal garden.

One day King stroll in the garden, thoughtfully, he asked a gardener said: "Recently I was thinking about a problem, if we put the flower beds into six hexagon, then ......"

"So in essence it is a depth-first search, Your Majesty," the gardener to the king bowed deeply.

"Ah ...... I heard there is a monster called Hydra, it is very greedy apple tree ......"

"Yes, obviously this is a classic dynamic programming problem, as early as N yuan in 4002 we had discovered the mystery of His Majesty."

"Damn, what are you backing?"

"Your Majesty pacified, dry our line and OI often inexplicably asked about the subject, I am also a precaution ah!" King's dignity was hurt, this is intolerable.

It seems the problem is generally not beat the gardener, the king finally going to wheel battle to consume his strength: "Young man, every tree in my garden can be represented by an integer coordinates, for a while, I the knights will take turns to ask you a number of trees within a matrix, if you can not answer immediately, you are ready to leave it! "then the king angrily go first.

This is the next turn gardener dumbfounded, he did not prepare such a problem. Fortunately, as the president of the "National Union for the Conservation gardener" - you may be his last straw.

Input Format

The first line has two integers n, m (0≤n≤500000,1≤m≤500000). N represents the total number of trees of the royal gardens, m represents the number of knights asked.

File following n lines, each line has two integers xi, yi, i represents the coordinates of the tree (0≤xi, yi≤10000000).

M last line of the file, each row has four integers aj, bj, cj, dj, represents the j-th interrogation, wherein Q rectangular to (aj, bj) is the lower left coordinates to (cj, dj) is the upper right coordinate.

Output Format

Total output m lines, each an integer, that is the answer to the King (aj, bj) and (cj, dj) is rectangular boundary of how many trees there.

Sample input and output

Input # 1 copy

3 1
0 0 
0 1
1 0
0 0 1 1

Output # 1 copy

3

Ideas:

Dimensional partial order to solve the problem of the number of points comprises a rectangular two-dimensional plane.

Dimensions:

{

The x-axis

y-axis

Action Type

}

We initially given n points into operation to add the coordinates

inquiry into the m ask questions.

So according to x, y ascending order, like when x and y are equal, dotted precedence.

Prefix and a two-dimensional nature of the inquiry into four (0,0) sub-node to the lower left corner inquiry accumulated contribution obtained.

After sorting, use an array to maintain stumps answer.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 3000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

ll tree[maxn];
int lowbit(int x)
{
    return -x & x;
}
ll ask(int x)
{
    ll res = 0ll;
    while (x) {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}
int my=0;
void add(int x, ll val)
{
    while (x < my) {
        tree[x] += val;
        x += lowbit(x);
    }
}
pii a[maxn];
pii b[maxn];
pii c[maxn];
int n, m;
std::vector<int> vx,vy;
struct node {
    int op;
    int x, y;
    int k;
    int id;
    node() {}
    node(int opp, int xx, int yy, int kk, int idd)
    {
        op = opp;
        x = xx;
        y = yy;
        k = kk;
        id = idd;
    }
} info[maxn];
int ans[maxn];
bool cmp(node aa, node bb)
{
    if (aa.x != bb.x) {
        return aa.x < bb.x;
    } else if (aa.op != bb.op) {
        return aa.op < bb.op;
    } else {
        return aa.y < bb.y;
    }
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n >> m;
    repd(i, 1, n) {
        cin >> a[i].fi >> a[i].se;
        vx.push_back(a[i].fi);
        vy.push_back(a[i].se);
    }
    repd(i, 1, m) {
        cin >> b[i].fi >> b[i].se >> c[i].fi >> c[i].se;
        vx.push_back(b[i].fi);
        vy.push_back(b[i].se);
        vx.push_back(c[i].fi);
        vy.push_back(c[i].se);
        vy.push_back(b[i].se - 1);
        vx.push_back(b[i].fi - 1);
    }
    sort(ALL(vx));
    sort(ALL(vy));
    vx.erase(unique(ALL(vx)),vx.end());
    vy.erase(unique(ALL(vy)),vy.end());
    my=sz(vy)+10;
    int cnt = 0;
    int dx, dy;
    repd(i, 1, n) {
        ++cnt;
        dx = lower_bound(ALL(vx), a[i].fi) - vx.begin() + 1;
        dy = lower_bound(ALL(vy), a[i].se) - vy.begin() + 1;
        info[cnt] = node(0, dx, dy, 0, 0);
    }
    repd(i, 1, m) {
        ++cnt;
        dx = lower_bound(ALL(vx), c[i].fi) - vx.begin() + 1;
        dy = lower_bound(ALL(vy), c[i].se) - vy.begin() + 1;
        info[cnt] = node(1, dx, dy, 1, i);

        ++cnt;
        dx = lower_bound(ALL(vx), c[i].fi) - vx.begin() + 1;
        dy = lower_bound(ALL(vy), b[i].se - 1) - vy.begin() + 1;
        info[cnt] = node(1, dx, dy, -1, i);

        ++cnt;
        dx = lower_bound(ALL(vx), b[i].fi - 1) - vx.begin() + 1;
        dy = lower_bound(ALL(vy), c[i].se) - vy.begin() + 1;
        info[cnt] = node(1, dx, dy, -1, i);

        ++cnt;
        dx = lower_bound(ALL(vx), b[i].fi-1) - vx.begin() + 1;
        dy = lower_bound(ALL(vy), b[i].se-1) - vy.begin() + 1;
        info[cnt] = node(1, dx, dy, 1, i);

    }
    sort(info + 1, info + 1 + cnt, cmp);
    repd(i, 1, cnt) {
        if (!info[i].op) {
            add(info[i].y, 1);
        } else {
            ans[info[i].id] += info[i].k * ask(info[i].y);
        }
    }
    repd(i, 1, m) {
        printf("%d\n", ans[i] );
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11594591.html