Super Mario HDU - 4417 (主席树)

本文介绍了一个基于马里奥游戏背景的算法问题,核心在于使用主席树解决区间查询中特定数值范围内的元素计数问题。文章详细阐述了如何通过离散化处理大量数据,并利用主席树的数据结构来高效地进行区间查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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      '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
  9      :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%`    '!%&#########@$!:.
 10      ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
 11      ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
 12     `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
 13     :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
 14     ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
 15    `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
 19  !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
 20   .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
 21   .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
 22     !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
 23      `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
 24        .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
 25            ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
 26            |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
 27           ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
 28         .%##@|;;;;:::''''''''''::;!%&##$'
 29       `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
 30     ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
 31      .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
 32     :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
 33      `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
 34      |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
 35     :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$:                 !@#$'
 36       |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%:              '%%!%&;
 37       |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||`            '%$|!%&;
 38       |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&:           .|%;:!&%`
 39       !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$;          `%&%!!$&:
 40       '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##;         !$!;:!$&:
 41        |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%'      `%$|%%;|&$'
 42         |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&;  .:%&$!;;;:!$@!
 43         `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
 44         !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
 45        '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
 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

 

转载于:https://www.cnblogs.com/Jiaaaaaaaqi/p/10122368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值