HDU 4417 划分树写法

Problem Description
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.
 
Input
The 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.)
 
Output
For 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
 
 
还是用kuangbin大神的板子好 ,以后就用kuangbin大神的划分树板子算了
 
这个就把划分树的板子改一下就好了、
 
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + ;
int a[maxn], sorted[maxn];
int num[][maxn], val[][maxn]; void build(int l, int r, int dep) {
if (l == r) return ;
int mid = (l + r) >> , same = mid - l + ;
for (int i = l ; i <= r ; i++)
if (val[dep][i] < sorted[mid]) same--;
int lpos = l, rpos = mid + ;
for (int i = l ; i <= r ; i++ ) {
if (val[dep][i] < sorted[mid]) val[dep + ][lpos++] = val[dep][i];
else if ( val[dep][i] == sorted[mid] && same > ) {
val[dep + ][lpos++] = val[dep][i];
same--;
} else val[dep + ][rpos++] = val[dep][i];
num[dep][i] = num[dep][l - ] + lpos - l;
}
build(l, mid, dep + );
build(mid + , r, dep + );
} int query(int L, int R, int l, int r, int dep, int k) {
if (l == r) {
if (val[dep][l] <= k) return ;
else return ;
}
int mid = (L + R) >> ;
int cnt = num[dep][r] - num[dep][l - ];
if (sorted[mid] <= k) {
int newr = r + num[dep][R] - num[dep][r];
int newl = newr - (r - l + - cnt) + ;
return cnt + query(mid + , R, newl, newr, dep + , k);
} else {
int newl = L + num[dep][l - ] - num[dep][L - ];
int newr = newl + cnt - ;
if (newr >= newl) return query(L, mid, newl, newr, dep + , k);
else return ;
}
}
int main() {
int t, n, m, l, r, k, cas = ;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for (int i = ; i <= n ; i++ ) {
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted + , sorted + n + );
build(, n, );
printf("Case %d:\n", cas++);
while(m--) {
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", query(, n, l + , r + , , k));
}
}
return ;
}
上一篇:HDU 4417 (划分树+区间小于k统计)


下一篇:hdu 3473 划分树