According to the x-axis, we find the maximum interval of the segment tree.
The model transformation is: matrix maximum intersection
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define Lson (pos< <1) #define RSON (pos<<1|1) const int ADD = 25555;const int maxn = 80005;int n,w,h,cnt;struct seg{int l,r,h,c; Seg (int l = 0,int r = 0,int h = 0,int c = 0): L (L), R (R), H (H), C (c) {}; friend bool operator < (Seg p,seg q) {return p.h < q.h; }}ss[maxn];int col[maxn << 2],max_value[maxn << 2];void pushdown (int pos) {if (Col[pos]) {Col[lson] + = Col[pos]; Max_value[lson] + = Col[pos]; Col[rson] + = Col[pos]; Max_value[rson] + = Col[pos]; Col[pos] = 0; }}void pushup (int pos) {Max_value[pos] = max (Max_value[lson],max_value[rson]);} void update (int l,int r,int l,int r,int Pos,int v) {if (L <= l && R <= R) {Col[pos] + = V; Max_value[pos] + = V; Return } pushdown (POS); int mid = (L + R) >> 1; if (l <= mid) update (L,MID,L,R,LSON,V); if (R >MID) Update (mid + 1,r,l,r,rson,v); Pushup (POS);} int main () {while (scanf ("%d", &n) && n > 0) {scanf ("%d%d", &w,&h); CNT = 0; memset (col,0,sizeof (col)); memset (max_value,0,sizeof (max_value)); int x, y; for (int i = 0; i < n; i++) {scanf ("%d%d", &x,&y); x + = ADD; Y + = ADD; ss[cnt++] = Seg (x,x + w,y,1); ss[cnt++] = Seg (x,x + w,y + h,-1); } sort (Ss,ss + cnt); int ans = 0; for (int i = 0; i < cnt; i++) {int L = Ss[i].l,r = Ss[i].r,c = ss[i].c; Update (0,MAXN,L,R,1,C); ans = max (ans,max_value[1]); } printf ("%d\n", ans); } return 0;}
"HDU 5091" Beam Cannon (scan line)