Question:
The width of a poster on a bulletin board is the same and the length may be different. The poster may overwrite the previous one. Ask how many different posters can be seen at the end.
Ideas:
This question was previously done and was written by the Section Dyeing of the Line Segment tree. Record whether each interval is solid or mixed color. Finally, all colors are counted.
Today we found that we can use a scanning line-like idea. Imagine a scan line going from left to right. Use set to maintain the set of posters corresponding to the current position. Record the latest (visible) poster in the current position.
Finally, count the number of visible posters.
Code:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # include <string> 8 # include <queue> 9 # include <stack> 10 # include <vector> 11 # include <map> 12 # include <set> 13 # include <functional> 14 # include <time. h> 15 16 using namespace STD; 17 18 const int INF = 1 <30; 19 const int maxn = (INT) 1e4 + 55; 20 21 struct event {22 int X, t; // Location, time 23 int status; // enter or output 24 25 void Init (int xx, int TT, int SS) {26 x = xx; 27 T = tt; 28 Status = SS; 29} 30 31 bool operator <(const event & _) const {32 If (X! = _. X) return x <_. x; 33 // at the same position, the inbound side is 34 before the outbound side // when the inbound side is the same as the outbound side, the greater the time is 35 before the outbound side, if the time is small, the first 36 return status * T> _. status *_. t; 37} 38} A [maxn <1]; 39 40 set <int> S; 41 bool vis [maxn]; 42 int N; 43 44 void solve () {45 s. clear (); 46 memset (VIS, false, sizeof (VIS); 47 int L, R; 48 49 scanf ("% d", & N ); 50 for (INT I = 0; I <n; I ++) {51 scanf ("% d", & L, & R ); 52 A [I <1]. init (L, I + 1, 1); 53 A [I <1 | 1]. init (R + 1, I + 1,-1); 54} 55 sort (, A + N * 2); 56 57 int ans = 0; 58 set <int >:: iterator P; 59 60 for (INT I = 0; I <n * 2; I ++) {61 if (a [I]. status> 0) s. insert (a [I]. t); 62 else S. erase (A [I]. t); 63 If (! S. Empty () {64 p = S. End (); p --; 65 if (! Vis [* p]) {66 ans ++; 67 vis [* p] = true; 68} 69} 70} 71 printf ("% d \ n", ANS ); 72} 73 74 int main () {75 # ifdef phantom0176 freopen ("pku2528.txt", "r", stdin); 77 # endif // phantom0178 79 int T; 80 scanf ("% d", & T); 81 While (t --) {82 solve (); 83} 84 85 return 0; 86}
View code