1. 程式人生 > >Super Mario HDU - 4417 (主席樹)

Super Mario HDU - 4417 (主席樹)

Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.

InputThe first line follows an integer T, the number of test data. 
For each test data: 
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries. 
Next line contains n integers, the height of each brick, the range is [0, 1000000000]. 
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)OutputFor each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query. 
Sample Input

1
10 10
0 5 2 7 5 4 3 8 7 7 
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3

Sample Output

Case 1:
4
0
0
3
1
2
0
1
5
1

給出 n 個數,m個詢問,對於每個詢問,求出在區間[l, r]內小於等於 h 的數的個數。
主席樹的模板題,數很大,所以需要離散化一下,維護字首個數和。
查詢的時候先找到第一個小於等於 h 的離散化後對應的下標,利用主席樹找區間內值小於等於 k 的數的和。
  1 /*
  2           .
3 ';;;;;. 4 '!;;;;;;!;` 5 '!;|&#@|;;;;!: 6 `;;!&####@|;;;;!: 7 .;;;!&@$$%|!;;;;;;!'.`:::::'. 8 '!;;;;;;;;[email protected]###&|;;|%!;!$|;;;;|&&;. 9 :!;;;;[email protected]&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
10 ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$; 11 ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%` 12 `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&; 13 :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@| 14 ;|::;;;;;;;;;;;;|&&$|;;[email protected]&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%. 15 `!!;;;;;;;!!!!;;;;;[email protected]@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|. 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##! 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&: 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%. 19 [email protected]|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&' 20 .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@: 21 .%$;;;;;;;;;;;;;;;;;;|[email protected]&|;;;;;;;;;;;;;;;;;;;;%@%. 22 !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#; 23 `%$!;;;;;;;;;;;[email protected]|;;;;;;;;;;;;;;;;;;;;;;;;!%[email protected]#@|. 24 .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%[email protected]#|. 25 ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|. 26 |##$|!;;;;;;::'':;;;;;;;;;;;;;!%[email protected]#@; 27 ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|` 28 .%##@|;;;;:::''''''''''::;!%&##$' 29 `$##@[email protected]@&|!!;;;:'''''::::;;;;;|&#%. 30 ;&@##&$%!;;;;;;::''''''''::;!|%[email protected]#@&@@: 31 .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%. 32 :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;` 33 `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&' 34 |##&%!;;;;;::''''''''''''::;;;;;;;[email protected]&:`!' 35 :;[email protected]$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: [email protected]#$' 36 |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&; 37 |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&; 38 |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%` 39 [email protected]&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&: 40 '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&: 41 |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$' 42 |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:[email protected]! 43 `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@! 44 !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%` 45 '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%[email protected]@|' 46 .!%%&%'`|$; :|$#%|@#&;%#%. 47 */ 48 #include <map> 49 #include <set> 50 #include <list> 51 #include <ctime> 52 #include <cmath> 53 #include <stack> 54 #include <queue> 55 #include <string> 56 #include <vector> 57 #include <cstdio> 58 #include <bitset> 59 #include <cstdlib> 60 #include <cstring> 61 #include <iostream> 62 #include <algorithm> 63 #define lowbit(x) x & (-x) 64 #define mes(a, b) memset(a, b, sizeof a) 65 #define fi first 66 #define se second 67 #define pii pair<int, int> 68 #define INOPEN freopen("in.txt", "r", stdin) 69 #define OUTOPEN freopen("out.txt", "w", stdout) 70 71 typedef unsigned long long int ull; 72 typedef long long int ll; 73 const int maxn = 1e5 + 10; 74 const int maxm = 1e5 + 10; 75 const int mod = 1e9 + 7; 76 const ll INF = 1e18 + 100; 77 const int inf = 0x3f3f3f3f; 78 const double pi = acos(-1.0); 79 const double eps = 1e-8; 80 using namespace std; 81 82 int n, m; 83 int cas, tol, T; 84 85 struct Node { 86 int l, r; 87 int sum; 88 } node[maxn * 20]; 89 int a[maxn]; 90 int rt[maxn]; 91 vector<int> vv; 92 93 void init() { 94 tol = 0; 95 mes(rt, 0); 96 vv.clear(); 97 } 98 99 int getid(int x) { 100 return lower_bound(vv.begin(), vv.end(), x) - vv.begin() + 1; 101 } 102 103 void update(int l, int r, int &x, int y, int pos) { 104 tol++; 105 node[tol] = node[y]; 106 node[tol].sum ++; 107 x = tol; 108 if(l == r) return ; 109 int mid = (l + r) >> 1; 110 if(pos <= mid) 111 update(l, mid, node[x].l, node[y].l, pos); 112 else 113 update(mid+1, r, node[x].r, node[y].r, pos); 114 } 115 116 int query(int l, int r, int x, int y, int pos) { 117 if(l == r) { 118 if(l <= pos) 119 return node[y].sum - node[x].sum; 120 else 121 return 0; 122 } 123 if(r <= pos) 124 return node[y].sum - node[x].sum; 125 int mid = (l +r) >> 1; 126 if(pos <= mid) 127 return query(l, mid, node[x].l, node[y].l, pos); 128 else { 129 int tmp = node[node[y].l].sum - node[node[x].l].sum; 130 return tmp + query(mid+1, r, node[x].r, node[y].r, pos); 131 } 132 } 133 134 int main() { 135 cas = 1; 136 scanf("%d", &T); 137 while(T--) { 138 init(); 139 scanf("%d%d", &n, &m); 140 for(int i=1; i<=n; i++) { 141 scanf("%d", &a[i]); 142 vv.push_back(a[i]); 143 } 144 sort(vv.begin(), vv.end()); 145 vv.erase(unique(vv.begin(), vv.end()), vv.end()); 146 for(int i=1; i<=n; i++) { 147 int id = getid(a[i]); 148 update(1, n, rt[i], rt[i-1], id); 149 } 150 printf("Case %d:\n", cas++); 151 while(m--) { 152 int u, v, d; 153 scanf("%d%d%d", &u, &v, &d); 154 u++, v++; 155 int id = getid(d); 156 if(vv[id-1] != d) 157 id--; 158 int ans = query(1, n, rt[u-1], rt[v], id); 159 printf("%d\n", ans); 160 } 161 } 162 return 0; 163 }
View Code