zoukankan      html  css  js  c++  java
  • 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大神的划分树板子算了
     
    这个就把划分树的板子改一下就好了、
     
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 typedef long long LL;
     7 const int maxn = 1e5 + 10;
     8 int a[maxn], sorted[maxn];
     9 int num[20][maxn], val[20][maxn];
    10 
    11 void build(int l, int r, int dep) {
    12     if (l == r) return ;
    13     int mid = (l + r) >> 1, same = mid - l + 1 ;
    14     for (int i = l ; i <= r ; i++)
    15         if (val[dep][i] < sorted[mid]) same--;
    16     int lpos = l, rpos = mid + 1;
    17     for (int i = l ; i <= r ; i++ ) {
    18         if (val[dep][i] < sorted[mid]) val[dep + 1][lpos++] = val[dep][i];
    19         else if (  val[dep][i] == sorted[mid] && same > 0) {
    20             val[dep + 1][lpos++] = val[dep][i];
    21             same--;
    22         } else val[dep + 1][rpos++] = val[dep][i];
    23         num[dep][i] = num[dep][l - 1] + lpos - l;
    24     }
    25     build(l, mid, dep + 1);
    26     build(mid + 1, r, dep + 1);
    27 }
    28 
    29 int query(int L, int R, int l, int r, int dep, int k) {
    30     if (l == r)  {
    31         if (val[dep][l] <= k) return 1;
    32         else return 0;
    33     }
    34     int mid = (L + R) >> 1;
    35     int cnt = num[dep][r] - num[dep][l - 1];
    36     if (sorted[mid] <= k) {
    37         int newr = r + num[dep][R] - num[dep][r];
    38         int newl = newr - (r - l + 1 - cnt) + 1;
    39         return cnt + query(mid + 1, R, newl, newr, dep + 1, k);
    40     } else {
    41         int newl = L + num[dep][l - 1] - num[dep][L - 1];
    42         int newr = newl + cnt - 1;
    43         if (newr >= newl) return query(L, mid, newl, newr, dep + 1, k);
    44         else return 0;
    45     }
    46 }
    47 int main() {
    48     int t, n, m, l, r, k, cas = 1;
    49     scanf("%d", &t);
    50     while(t--) {
    51         scanf("%d%d", &n, &m);
    52         for (int i = 1 ; i <= n ; i++ ) {
    53             scanf("%d", &val[0][i]);
    54             sorted[i] = val[0][i];
    55         }
    56         sort(sorted + 1, sorted + n + 1);
    57         build(1, n, 0);
    58         printf("Case %d:
    ", cas++);
    59         while(m--) {
    60             scanf("%d%d%d", &l, &r, &k);
    61             printf("%d
    ", query(1, n, l + 1, r + 1, 0, k));
    62         }
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    多元高斯分布(斯坦福machine learning week 9)
    异常检测(斯坦福machine learning week 9)
    Python编码透析
    nlp Python库之pynlpir
    降维(斯坦福machine learning week 8)
    主成分分析PCA之协方差矩阵的理解
    聚类(斯坦福machine learning week 8)
    svm之使用SVM(斯坦福machine learning week 7)
    java泛型总结
    Java之IO流学习总结
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9107513.html
Copyright © 2011-2022 走看看