Q: I have T group of test data, N posters, and the posters are covered in the order given by the question. I can see several posters at last.
Discretization is required. Because the number given by the question is corresponding to the interval, you can set the corresponding boundary to a [I] -- or B [I] ++. When building a tree, the boundary condition is left + 1 = right, because the interval is continuous. The input data corresponds to a value. When querying, if the current color is solid, you do not need to query it, the flag of the corresponding color flag [I] Table is marked and directly returned. Finally, it is judged that several values in the flag table are marked once to be scanned for statistics.
/* After the code style is updated */# include <iostream> # include <cstdio> # include <cstring> # include <vector> # include <map> # include <algorithm> using namespace STD; # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define mid (A, B) (A + (B-a)> 1) const int n = 20005; struct node {int LFT, rht, CO; int mid () {return mid (LFT, RHT) ;}}; int A [n], B [N], n; vector <int> Y; bool flag [n/2]; Map <int, int> H; struct segtree {node tree [N * 4]; void build (int lft, int R HT, int IND) {tree [ind]. LFT = LFT; tree [ind]. rht = rht; tree [ind]. CO = 0; If (LFT + 1! = RHT) {int mid = tree [ind]. mid (); Build (LFT, mid, LL (IND); Build (MID, rht, RR (IND) ;}} void updata (INT St, int Ed, int ind, int Co) {int LFT = tree [ind]. LFT, rht = tree [ind]. rht; If (ST <= LFT & rht <= ed) tree [ind]. CO = Co; else {If (tree [ind]. CO) {tree [LL (IND)]. CO = tree [ind]. CO; tree [RR (IND)]. CO = tree [ind]. CO; tree [ind]. CO = 0;} int mid = tree [ind]. mid (); If (ST <mid) updata (St, Ed, LL (IND), CO); If (Ed> mid) updata (St, Ed, rr (IND), Co) ;}} void Qu Ery (INT St, int ed, int IND) {int LFT = tree [ind]. LFT, rht = tree [ind]. rht; If (tree [ind]. CO! = 0 | LFT + 1 = RHT) {flag [tree [ind]. CO] = 1; return;} query (St, Ed, LL (IND); query (St, Ed, RR (IND) ;}} seg; int main () {int t; scanf ("% d", & T); While (t --) {Y. clear (); H. clear (); scanf ("% d", & N); For (INT I = 0; I <n; I ++) {flag [I] = 0; scanf ("% d", & A [I], & B [I]); B [I] ++; Y. push_back (A [I]); Y. push_back (B [I]);} Sort (Y. begin (), Y. end (); Y. erase (unique (Y. begin (), Y. end (), Y. end (); For (INT I = 0; I <(INT) Y. size (); I ++) H [Y [I] = I; int Len = (INT) Y. size (); seg. build (0, len-1, 1); For (INT I = 0; I <n; I ++) seg. updata (H [A [I], H [B [I], 1, I + 1); seg. query (1, Len, 1); int res = 0; For (INT I = 1; I <= N; I ++) if (flag [I]) res ++; printf ("% d \ n", Res);} return 0 ;}
/* Before code style update */# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # define bug puts ("here "); using namespace STD; const int n = 21005; int A [n], B [N]; int lisan (INT, INT); struct POS {int X, Y ;} pos [N]; struct node {int left, right, CO; int mid () {return left + (right-left)/2 ;}}; struct segtree {node tree [N * 4]; void build (INT left, int right, int R) {tree [R]. left = left; tree [R]. right = right; tree [R]. c O = 0; If (left + 1 <right) {int mid = tree [R]. mid (); Build (left, mid, R * 2); Build (MID, right, R * 2 + 1) ;}} void updata (INT be, int end, int R, int Co) {If (be <= tree [R]. left & tree [R]. right <= END) {tree [R]. CO = Co;} else {If (tree [R]. CO! = 0) {tree [R * 2]. CO = tree [R * 2 + 1]. CO = tree [R]. CO; tree [R]. CO = 0;} int mid = tree [R]. mid (); If (be <mid) updata (BE, end, R * 2, CO); If (end> mid) updata (BE, end, R * 2 + 1, Co) ;}} void query (int r) {If (tree [R]. CO! = 0 | tree [R]. left + 1 = tree [R]. right) {A [tree [R]. CO] = 1; return;} else {query (R * 2); query (R * 2 + 1) ;}} seg; int main () {int T; scanf ("% d", & T); While (t --) {memset (A, 0, sizeof (a); int N, CNT = 0, ANS = 0; scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d ", & Pos [I]. x, & Pos [I]. y); POS [I]. Y ++; B [CNT ++] = POS [I]. x; B [CNT ++] = POS [I]. y;} int IMAX = lisan (n, CNT); seg. build (0, IMAX, 1); For (INT I = 0; I <n; I ++) {seg. updata (Pos [I]. x, POS [I]. y, 1, I + 1);} seg. query (1); For (INT I = 1; I <= N; I ++) {if (a [I]) ans ++; // printf ("% d", a [I]);} // puts (""); printf ("% d \ n", ANS );} return 0;} int lisan (int n, int CNT) {sort (B, B + CNT); CNT = unique (B, B + CNT)-B; int IMAX = 0; For (INT I = 0; I <n; I ++) {pos [I]. X = lower_bound (B, B + CNT, POS [I]. x)-B; IMAX = max (IMAX, POS [I]. x); POS [I]. y = lower_bound (B, B + CNT, POS [I]. y)-B; IMAX = max (IMAX, POS [I]. y); // printf ("x = % d, y = % d, I = % d \ n", POS [I]. x, POS [I]. y, I + 1);} return IMAX ;}