Sort by H. insert values not greater than h in each query to the corresponding position in the tree array and complete the query.
171 Ms
View code
1 /* 2 Author: zhaofa Fang 3 Lang: C ++ 4 */ 5 # Include <cstdio> 6 # Include <cstdlib> 7 # Include <iostream> 8 # Include <cmath>9 # Include <cstring> 10 # Include <algorithm> 11 # Include < String > 12 # Include <utility> 13 # Include <vector> 14 # Include <queue> 15 # Include <stack> 16 # Include <map> 17 # Include < Set >18 Using Namespace STD; 19 Typedef Long Long Ll; 20 # Define PII pair <int, int> 21 # Define PB push_back 22 # Define MP make_pair 23 # Define Fi first 24 # Define Se second 25 # Define Lowbit (x) (X & (-x )) 26 Const Int INF = 2147483647 ; 27 28 Const Int Maxn = 100005 ; 29 Int C [maxn]; 30 Struct Query 31 { 32 Int L, R, H, POs, ans; 33 } Q [maxn]; 34 Struct Bri 35 { 36 Int Num, Pos; 37 } A [maxn]; 38 Int N, m; 39 40 Int CMP1 (bri a, Bri B) 41 { 42 Return A. Num < B. Num; 43 } 44 Int Cmp2 (query a, query B) 45 { 46 Return A. H < B. H; 47 } 48 Int Cmp3 (query a, query B) 49 { 50 Return A. Pos < B. Pos; 51 } 52 Void Add ( Int X, Int Pos) 53 { 54 While (Pos <= N) 55 { 56 C [POS] + = X; 57 Pos + = Lowbit (POS ); 58 } 59 } 60 Int Sum ( Int Pos) 61 { 62 Int Res = 0 ; 63 While (Pos> 0 ) 64 { 65 Res + = C [POS]; 66 Pos-= Lowbit (POS ); 67 } 68 Return Res; 69 } 70 Int Main () 71 { 72 # Ifndef online_judge 73 Freopen ( " In " , " R " , Stdin ); 74 # Endif 75 Int T; 76 Int CAS = 0 ; 77 Scanf ( " % D " ,& T ); 78 While (T -- ) 79 { 80 Scanf ( " % D " , & N ,& M ); 81 Memset (C, 0 , (N + 1 )*Sizeof ( Int )); 82 For ( Int I = 0 ; I <n; I ++ ) 83 { 84 Scanf ( " % D " ,& A [I]. Num ); 85 A [I]. Pos = I + 1 ; 86 } 87 Sort (A, A + N, CMP1 ); 88 Printf ( " Case % d: \ n " , ++ CAS ); 89 For ( Int I =0 ; I <m; I ++ ) 90 { 91 Scanf ( " % D " , & Q [I]. l, & Q [I]. R ,& Q [I]. H ); 92 Q [I]. Pos = I + 1 ; 93 } 94 Sort (Q, q +M, cmp2 ); 95 Int K = 0 ; 96 For ( Int I = 0 ; I <m; I ++ ) 97 { 98 While (A [K]. Num <= Q [I]. H & K < N) 99 { 100 Add ( 1 , A [K]. Pos ); 101 K ++ ; 102 } 103 Q [I]. ans = sum (Q [I]. R + 1 )- Sum (Q [I]. L ); 104 } 105 Sort (Q, q + M, cmp3 ); 106 For ( Int I = 0 ; I <m; I ++ ) 107 { 108 Printf ( " % D \ n " , Q [I]. ans ); 109 } 110 } 111 Return 0 ; 112 }