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): 6208    Accepted Submission(s): 2687


    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
     
      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 #define N 100005
     14 using namespace  std;
     15 struct chairmantree
     16 {
     17     int tot;
     18     int rt[20*N],ls[20*N],rs[20*N],sum[20*N];
     19     void init()
     20     {
     21         tot=0;
     22     }
     23     void buildtree(int l,int r,int &pos)
     24     {
     25         pos=++tot;
     26         sum[pos]=0;
     27         if(l==r) return ;
     28         int mid=(l+r)>>1;
     29         buildtree(l,mid,ls[pos]);
     30         buildtree(mid+1,r,rs[pos]);
     31     }
     32     void update(int p,int c,int pre,int l,int r,int &pos)
     33     {
     34         pos=++tot;
     35         ls[pos]=ls[pre];
     36         rs[pos]=rs[pre];
     37         sum[pos]=sum[pre]+c;
     38         if(l==r) return ;
     39         int mid=(l+r)>>1;
     40         if(p<=mid)
     41             update(p,c,ls[pre],l,mid,ls[pos]);
     42         else
     43             update(p,c,rs[pre],mid+1,r,rs[pos]);
     44     }
     45     int query(int L,int R,int s,int t,int l,int r)
     46     {
     47         if(s<=l&&r<=t)
     48             return sum[R]-sum[L];
     49         int mid=(l+r)>>1;
     50         int ans=0;
     51         if(s<=mid)
     52             ans+=query(ls[L],ls[R],s,t,l,mid);
     53         if(t>mid)
     54             ans+=query(rs[L],rs[R],s,t,mid+1,r);
     55         return ans;
     56     }
     57 }tree;
     58 
     59 int t;
     60 int n,m;
     61 int a[N];
     62 int b[2*N];
     63 int l[N],r[N],x[N];
     64 int getpos(int x,int cnt)
     65 {
     66     int pos=lower_bound(b+1,b+1+cnt,x)-b;
     67     return pos;
     68 }
     69 int main()
     70 {
     71     scanf("%d",&t);
     72     int T=t;
     73     while(t--)
     74     {
     75         scanf("%d %d",&n,&m);
     76         for(int i=1;i<=n;i++){
     77             scanf("%d",&a[i]);
     78             b[i]=a[i];
     79             }
     80         for(int i=1;i<=m;i++){
     81             scanf("%d %d %d",&l[i],&r[i],&x[i]);
     82             b[i+n]=x[i];
     83             }
     84         sort(b+1,b+1+n+m);
     85         int num=unique(b+1,b+1+n+m)-b;
     86         tree.init();
     87         tree.buildtree(1,num-1,tree.rt[0]);
     88         for(int i=1;i<=n;i++)
     89         {
     90             int pos=getpos(a[i],num-1);
     91             tree.update(pos,1,tree.rt[i-1],1,num-1,tree.rt[i]);
     92         }
     93         printf("Case %d:
    ",T-t);
     94         for(int i=1;i<=m;i++)
     95         {
     96             int pos=getpos(x[i],num-1);
     97             printf("%d
    ",tree.query(tree.rt[l[i]],tree.rt[r[i]+1],1,pos,1,num-1));
     98         }
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    css3圆形修边按钮
    TinyCC的Java绑定版本 TCC4Java
    简单实用蓝色jQuery日期选择插件
    QE不是变形金刚
    javascript 进度条的几种方法
    使用Java进行 a + b = c 计算
    Javascript jquery css 写的简单进度条控件
    Drupal SellingSpace模板
    恢复Windows 10自带的微软正黑字体
    提取配置文件中无注释的内容方法--findstr
  • 原文地址:https://www.cnblogs.com/hsd-/p/6502962.html
Copyright © 2011-2022 走看看