+ Scan line segment tree study notes

If you look at the scanner will work found, the scanner is a line scan from start to finish imaging scan again. This algorithm figurative representation as well.

The first is the question of scan lines board: a rectangular area and.

The meaning of problems: in the plane rectangular coordinate system, given a number of rectangles, and find all the area of ​​a rectangle.

Edition not look too: for all endpoints in accordance with a rectangular ordinate sorted, then added sequentially scanning each line segment covering a rectangular, longitudinal segment tree query can be covered in all sections. 

In detail, what we abstracting processing for each rectangle, two sides of the horizontal rectangle separate out, left and right endpoint and records the height of this edge. This strip edges and to assign a special value, +1 or -1, +1 for forming the bottom side of the rectangle, the upper edge forming -1, indicating that the process of entering and leaving the scanning rectangle. Then maintaining the length of the line section with a segment tree, then multiplied by the height corresponding to the line.

Then one needs a little bit of construction problems:

Everyone has a capacity value ai, and if the ability value gap between two people is too large, so two people can not communicate, but each person's ability to communicate is different, specifically, the ability to value each person can only accept people [li, ri] in his teammates do, want the ranks of the number of people as much as possible, you output the largest number of people.

We note that the assumptions used in the largest size of the crew, all of them acceptable interval [L, R], so for everyone li <= L <= a, a <= R <= ri, so we can of each person as a rectangle, a rectangular horizontal direction edge is [li, ai], a vertical direction edge is [ai, ri], to find the problem is transformed into a point so as to cover the maximum number of rectangle, then scan directly line, each scan interval on one side to +1 or -1, the maintenance interval maximum value on it. Complexity of O (nlogn).

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<ctime>
#define B printf("Break\n");
#define A(x) cout << #x << " " << (x) << endl;
#define ll long long
using namespace std;
int read()
{
    int x = 0,f = 1;
    char c = getchar();
    while(c < '0' || c > '9') 
    {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9')
    {
        x = (x << 3) + (x << 1) + c - '0';
        c = getchar();
    }
    return f * x;
}
#define N 300010
int ans;
struct Rec
{
    int l,r,h;
    int op;
    bool operator < (const Rec &x) const
    {
        if(h == x.h) return op > x.op;
        return h < x.h;
    }
}rec[N << 1];
#define ls x << 1,l,mid
#define rs (x << 1 | 1),mid + 1,r
#define mid ((l + r) >> 1)
int tree[N << 2],lazy[N << 2];
void add(int x,int l,int r,int k)
{
    tree[x] += k;
    lazy[x] += k;
    return;
}
void push_down(int x,int l,int r)
{
    if(lazy[x] == 0) return;
    add(ls,lazy[x]);
    add(rs,lazy[x]);
    lazy[x] = 0;
    return;
}
void modify(int x,int l,int r,int p,int q,int k)
{
    if(p <= l && r <= q)
    {
        add(x,l,r,k);
        return;
    }
    push_down(x,l,r);
    if(p <= mid) modify(ls,p,q,k);
    if(q > mid) modify(rs,p,q,k);
    tree[x] = max(tree[x << 1],tree[x << 1 | 1]);
    return;
}
int cnt;
void Add(int l,int a,int r)
{
    rec[++cnt].l = l;
    rec[cnt].r = a;
    rec[cnt].h = a;
    rec[cnt].op = 1;
    rec[++cnt].l = l;
    rec[cnt].r = a;
    rec[cnt].h = r;
    rec[cnt].op = -1;
    return;
}
int main()
{
    int n = read();
    for(int i = 1;i <= n;i++) 
    {
        int l = read(),a = read(),r = read();
        Add(l,a,r);
    }
    sort(rec + 1,rec + 1 + cnt);
    for(int i = 1;i <= cnt;i++)
    {
        int l = rec[i].l,r = rec[i].r;
        modify(1,1,300000,l,r,rec[i].op);
        ans = max(ans,tree[1]);
    }
    printf("%d\n",ans);
}

 

Guess you like

Origin www.cnblogs.com/lijilai-oi/p/11103766.html