zoukankan      html  css  js  c++  java
  • HDU 4417 离线+树状数组

    Super Mario

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6174    Accepted Submission(s): 2675


    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
     
     
    Source
     题意:给你n个数 m个查询 l r x  问区间[l,r]中小于等于x的数的个数
     题解:离线+树状数组 将n个数的的位置存入树状数组。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll  __int64
    11 #define mod 1000000007
    12 #define dazhi 2147483647
    13 using namespace  std;
    14 int t;
    15 int n,m;
    16 int tree[100005];
    17 int ans[100005];
    18 int lowbit(int tt)
    19 {
    20     return tt&(-tt);
    21 }
    22 void add(int x,int y)
    23 {
    24     for(int i=x; i<=n; i+=lowbit(i))
    25         tree[i]+=y;
    26 }
    27 int getsum(int x)
    28 {
    29     int ans=0;
    30     for(int i=x; i>0; i-=lowbit(i))
    31         ans+=tree[i];
    32     return ans;
    33 }
    34 struct node
    35 {
    36     int w;
    37     int pos;
    38 } N[100005];
    39 struct xx
    40 {
    41     int l,r;
    42     int x;
    43     int pos;
    44 } M[100005];
    45 bool cmp1 (struct node aa,struct node bb)
    46 {
    47     return aa.w<bb.w;
    48 }
    49 bool cmp2 (struct xx aa,struct xx bb)
    50 {
    51     return aa.x<bb.x;
    52 }
    53 int main()
    54 {
    55         scanf("%d",&t);
    56         for(int i=1; i<=t; i++)
    57         {
    58             memset(tree,0,sizeof(tree));
    59             scanf("%d %d",&n,&m);
    60             for(int j=1; j<=n; j++)
    61             {
    62                 scanf("%d",&N[j].w);
    63                 N[j].pos=j;
    64             }
    65             sort(N+1,N+1+n,cmp1);
    66             for(int j=1; j<=m; j++)
    67             {
    68                 scanf("%d %d %d",&M[j].l,&M[j].r,&M[j].x);
    69                 M[j].l++;
    70                 M[j].r++;
    71                 M[j].pos=j;
    72             }
    73             sort(M+1,M+1+m,cmp2);
    74             int exm=1;
    75             for(int j=1; j<=m; j++)
    76             {
    77                 while(exm<=n&&N[exm].w<=M[j].x)
    78                 {
    79                     add(N[exm].pos,1);
    80                     exm++;
    81                 }
    82                 ans[M[j].pos]=getsum(M[j].r)-getsum(M[j].l-1);
    83             }
    84             printf("Case %d:
    ",i);
    85             for(int j=1; j<=m; j++)
    86                 printf("%d
    ",ans[j]);
    87         }
    88     return 0;
    89 }
  • 相关阅读:
    mysql相关笔记
    qt杂项
    rpm离线安装整理
    linux fopen个数限制的问题(文件描述符限制)
    解决free():invalid pointer:0x00000000000000155455 ****的问题。
    linux c获取系统时间戳
    ubuntu QT Creater 安装
    LinkedHashMap如何保证顺序性
    HashMap原理(二) 扩容机制及存取原理
    HashMap原理(一) 概念和底层架构
  • 原文地址:https://www.cnblogs.com/hsd-/p/6435882.html
Copyright © 2011-2022 走看看