cometoj tea Yan Yue color | + lazy scan lines mark

Portal

Title Description

Tea Yan Yue color also good drink! Linger in a variety of cocktails in Changsha Yan Yue color of tea shops. He found that Changsha has fried chicken and more tea shop Yan Yue color, be able to take two steps encountered one.

"Radius of one kilometer can have ten tea Yan Yue color!" Cocktail sigh up.

So he thought of a question: where the most intensive, to how many Yan Yue color tea shop?

The cocktail Changsha map is represented by a two-dimensional plane, his statistics every tea shop Yan Yue color coordinates.

He wanted to know, in a side length  k and parallel to the bottom edge of  the square in the x-axis, up to how many tea Yan Yue color.

If the tea Yan Yue color just in the edge of the square, are also considered in the square.

 

Enter a description

Input of the first line contains two positive integers  n- , ( n- . 1 0 ^ . 5 , K . 1 0 ^ . 9) representative of the number of colors Yan Yue tea shops and side of the square.

The next  n  lines each have two integers, described in a tea shop Yan Yue color coordinates  XI, Y I  ( 0 X I , Y I . 1 0 ^ . 9) will not guarantee a coordinate repeated.

 

Output Description

Output line a positive integer answers.

 

Sample input 1 

4 2
1 1
3 1
3 4
2 2

Sample output 1

 3

 

Meaning of the questions: a two-dimensional plane there are n points, now have a position in any of k * k rectangles inside it asked to have the maximum number of points.

Solution: We can put this question to be converted to (xi, yi) is the bottom left corner coordinates, (xi + k, yi + k) is a rectangle in the upper right corner coordinates, up to a few rectangles overlap. Then we can have fun with the scanning lines to deal with: we will sweep from left to right vertical straight line of each rectangle, cnt ++ sweeps the left edge of the rectangle, cnt-- sweeps right border. Maintenance maximum cnt value.

   This problem should be noted that: just the edge of the square, are also considered in the square. This means that if there is another rectangle with the left edge of the right boundary of a rectangular coincidence, then, let's add further reduced. For this we can add it in a matrix memory when the right boundary, can also sort processing segment.

   This question and stars in POJ2482 window similar, but a boundary is not considered a border, a first cut after a first decreased post-increment.

Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
struct node{
    ll x,y1,y2;
    int val;
    node(){}
    node(ll x,ll y1,ll y2,int val) {
        this->x = x; this->y1 = y1; this->y2 = y2; this->val = val;
    }
    bool operator <(const node &t)const {
        //if (x==t.x) return val > t.val;    //排序时处理
        return x<t.x;
    }
};
struct Tree{
    int l,r,cnt,lazy;
}T[N<<2];
vector<ll> v;
vector<node> a;
void build(int rt,int l,int r) {
    T[rt].l = l; T[rt].r = r;
    T[rt].cnt = 0; T[rt].lazy = 0;
    if (l==r) return;
    int mid = (l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void down(int rt) {
    if (T[rt].lazy) {
        T[rt<<1].cnt += T[rt].lazy;
        T[rt<<1].lazy += T[rt].lazy;
        T[rt<<1|1].cnt += T[rt].lazy;
        T[rt<<1|1].lazy += T[rt].lazy;
        T[rt].lazy = 0;
    }
}
void update(int rt,int l,int r,int val) {
    if (l <= T[rt].l && r >= T[rt].r) {
        T[rt].cnt += val;
        T[rt].lazy += val;
        return;
    }
    down(rt);
    int mid = (T[rt].l+T[rt].r)>>1;
    if (l <= mid) update(rt<<1,l,r,val);
    if (r > mid) update(rt<<1|1,l,r,val);
    T[rt].cnt = max(T[rt<<1].cnt,T[rt<<1|1].cnt);
}
int main() {
    int n;
    ll k,x,y;
    scanf("%d%lld",&n,&k);
    for (int i = 0; i < n; i++) {
        scanf("%lld%lld",&x,&y);
        v.push_back(y);
        v.push_back(y+k);
        a.push_back(node(x,y,y+k,1));
        a.push_back(node(x+k+1,y,y+k,-1)); //存的时候右边界x坐标+1
    }
    sort(a.begin(),a.end());
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    n = v.size();
    build(1,0,n);
    n = a.size();
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int l = lower_bound(v.begin(),v.end(),a[i].y1) - v.begin();
        int r = lower_bound(v.begin(),v.end(),a[i].y2) - v.begin();
        update(1, l, r, a [i] .val); 
        years = max (years T [ 1 ] .cnt); 
    } 
    Printf ( " % d \ n " , year);
    return  0 ; 
}
View Code

 



Guess you like

Origin www.cnblogs.com/l999q/p/11367568.html
yue