ProblemA Circuits
Solved.
题意:
有$n$个矩形,可以放两条平行与$x$轴的线,求怎么放置两条无线长的平行于$x$轴的线,使得他们与矩形相交个数最多
如果一个矩形同时与两条线相交,只算一次。
思路:
离散化后枚举一根线,另一根线用线段树维护,扫描线思想
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 400010 5 int n; 6 int b[N]; 7 int x[N], y[N]; 8 vector <int> in[N], out[N]; 9 int ans[N]; 10 11 namespace SEG 12 { 13 struct node 14 { 15 int Max, lazy; 16 node () {} 17 node (int Max, int lazy) : Max(Max), lazy(lazy) {} 18 void init() { Max = lazy = 0; } 19 void add(int x) 20 { 21 Max += x; 22 lazy += x; 23 } 24 node operator + (const node &other) const 25 { 26 node res; res.init(); 27 res.Max = max(Max, other.Max); 28 return res; 29 } 30 }a[N << 2]; 31 void build(int id, int l, int r) 32 { 33 a[id].init(); 34 if (l == r) 35 return; 36 int mid = (l + r) >> 1; 37 build(id << 1, l, mid); 38 build(id << 1 | 1, mid + 1, r); 39 } 40 void pushdown(int id) 41 { 42 if (!a[id].lazy) return; 43 a[id << 1].add(a[id].lazy); 44 a[id << 1 | 1].add(a[id].lazy); 45 a[id].lazy = 0; 46 } 47 void update(int id, int l, int r, int ql, int qr, int x) 48 { 49 if (l >= ql && r <= qr) 50 { 51 a[id].add(x); 52 return; 53 } 54 int mid = (l + r) >> 1; 55 pushdown(id); 56 if (ql <= mid) update(id << 1, l, mid, ql, qr, x); 57 if (qr > mid) update(id << 1 | 1, mid + 1, r, ql, qr, x); 58 a[id] = a[id << 1] + a[id << 1 | 1]; 59 } 60 int query(int id, int l, int r, int pos) 61 { 62 if (l == r) return a[id].Max; 63 int mid = (l + r) >> 1; 64 pushdown(id); 65 if (pos <= mid) return query(id << 1, l, mid, pos); 66 else return query(id << 1 | 1, mid + 1, r, pos); 67 } 68 } 69 70 void Hash() 71 { 72 sort(b + 1, b + 1 + b[0]); 73 b[0] = unique(b + 1, b + 1 + b[0]) - b - 1; 74 for (int i = 1; i <= n; ++i) x[i] = lower_bound(b + 1, b + 1 + b[0], x[i]) - b; 75 for (int i = 1; i <= n; ++i) y[i] = lower_bound(b + 1, b + 1 + b[0], y[i]) - b; 76 } 77 78 int main() 79 { 80 while (scanf("%d", &n) != EOF) 81 { 82 b[0] = 0; 83 for (int i = 1; i < N; ++i) 84 in[i].clear(), out[i].clear(); 85 for (int i = 1, tmp; i <= n; ++i) 86 { 87 scanf("%d%d%d%d", &tmp, y + i, &tmp, x + i); 88 // cout << x[i] << " " << y[i] << endl; 89 b[++b[0]] = x[i]; 90 b[++b[0]] = y[i]; 91 } 92 Hash(); 93 SEG::build(1, 1, b[0]); 94 for (int i = 1; i <= n; ++i) 95 { 96 in[x[i]].push_back(i); 97 out[y[i]].push_back(i); 98 SEG::update(1, 1, b[0], x[i], y[i], 1); 99 } 100 int res = 0; 101 for (int i = 1; i <= b[0]; ++i) 102 ans[i] = SEG::query(1, 1, b[0], i); 103 for (int i = 1; i <= b[0]; ++i) 104 { 105 for (auto it : in[i]) 106 SEG::update(1, 1, b[0], x[it], y[it], -1); 107 res = max(res, ans[i] + SEG::a[1].Max); 108 for (auto it : out[i]) 109 SEG::update(1, 1, b[0], x[it], y[it], 1); 110 } 111 printf("%d\n", res); 112 } 113 return 0; 114 }