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): 9118    Accepted Submission(s): 3840


    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
      询问静态数组中[L,R]之间<=H的数的个数,用第R棵树的query()减去第L-1棵树的query()就是答案了。
    WA了几发是因为离散化的问题,我只是离散化了ai却忘记了hi,离线把询问的数值和ai一起离散化了,
    还要记住这样的话根节点的区间就是[1,N+Q]了。
      
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long 
     4 #define inf 0x3f3f3f3f
     5 #define pb push_back
     6 #define mid ((L+R)>>1)
     7 const int maxn=100010;
     8 int a[maxn],root[maxn],N,tot;
     9 int l[maxn],r[maxn],h[maxn];
    10 vector<int>vi;
    11 struct node{int lc,rc,sum;}T[maxn*40];
    12 int getid(int x){return lower_bound(vi.begin(),vi.end(),x)-vi.begin()+1;}
    13 void update(int &x,int y,int L,int R,int d){
    14     T[++tot]=T[y],T[tot].sum++,x=tot;
    15     if(L==R)return;
    16     if(d<=mid) update(T[x].lc,T[y].lc,L,mid,d);
    17     else update(T[x].rc,T[y].rc,mid+1,R,d);
    18 }
    19 int query(int x,int L,int R,int d){
    20      if(L==R) return T[x].sum;
    21      if(d<=mid) return query(T[x].lc,L,mid,d);
    22      else return T[T[x].lc].sum+query(T[x].rc,mid+1,R,d);
    23 }
    24 int main(){
    25     int t,q,cas=0;
    26     cin>>t;
    27     while(t--){
    28         tot=0;
    29         vi.clear();
    30         scanf("%d%d",&N,&q);
    31         for(int i=1;i<=N;++i) scanf("%d",a+i),vi.pb(a[i]);
    32         printf("Case %d:
    ",++cas);
    33         for(int i=1;i<=q;++i){
    34             scanf("%d%d%d",l+i,r+i,h+i);
    35             vi.pb(h[i]);
    36         }
    37         sort(vi.begin(),vi.end()),vi.erase(unique(vi.begin(), vi.end()),vi.end());
    38         for(int i=1;i<=N;++i) update(root[i],root[i-1],1,N+q,getid(a[i]));
    39         for(int i=1;i<=q;++i) h[i]=getid(h[i]),printf("%d
    ",query(root[r[i]+1],1,N+q,h[i])-query(root[l[i]],1,N+q,h[i]));
    40     }
    41     return 0;
    42 }
     
  • 相关阅读:
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    跨平台将终结
    一万字详解 Redis Cluster Gossip 协议
    Java实现简单的计算器
    CSDN开发者周刊第 22期:谷歌 DeepMind 第四代:不学规则就可以玩游戏;图灵奖得主 Edmund Clarke 因感染“新冠”逝世;
    理解Python闭包,这应该是最好的例子
    sscanf函数用法详解
    web项目中配置多个数据源
    web项目中配置多个数据源
    动态表格之查看、删除、编辑
  • 原文地址:https://www.cnblogs.com/zzqc/p/9381408.html
Copyright © 2011-2022 走看看