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
 
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define N 100005
using namespace std;
struct chairmantree
{
int tot;
int rt[*N],ls[*N],rs[*N],sum[*N];
void init()
{
tot=;
}
void buildtree(int l,int r,int &pos)
{
pos=++tot;
sum[pos]=;
if(l==r) return ;
int mid=(l+r)>>;
buildtree(l,mid,ls[pos]);
buildtree(mid+,r,rs[pos]);
}
void update(int p,int c,int pre,int l,int r,int &pos)
{
pos=++tot;
ls[pos]=ls[pre];
rs[pos]=rs[pre];
sum[pos]=sum[pre]+c;
if(l==r) return ;
int mid=(l+r)>>;
if(p<=mid)
update(p,c,ls[pre],l,mid,ls[pos]);
else
update(p,c,rs[pre],mid+,r,rs[pos]);
}
int query(int L,int R,int s,int t,int l,int r)
{
if(s<=l&&r<=t)
return sum[R]-sum[L];
int mid=(l+r)>>;
int ans=;
if(s<=mid)
ans+=query(ls[L],ls[R],s,t,l,mid);
if(t>mid)
ans+=query(rs[L],rs[R],s,t,mid+,r);
return ans;
}
}tree; int t;
int n,m;
int a[N];
int b[*N];
int l[N],r[N],x[N];
int getpos(int x,int cnt)
{
int pos=lower_bound(b+,b++cnt,x)-b;
return pos;
}
int main()
{
scanf("%d",&t);
int T=t;
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=;i<=m;i++){
scanf("%d %d %d",&l[i],&r[i],&x[i]);
b[i+n]=x[i];
}
sort(b+,b++n+m);
int num=unique(b+,b++n+m)-b;
tree.init();
tree.buildtree(,num-,tree.rt[]);
for(int i=;i<=n;i++)
{
int pos=getpos(a[i],num-);
tree.update(pos,,tree.rt[i-],,num-,tree.rt[i]);
}
printf("Case %d:\n",T-t);
for(int i=;i<=m;i++)
{
int pos=getpos(x[i],num-);
printf("%d\n",tree.query(tree.rt[l[i]],tree.rt[r[i]+],,pos,,num-));
}
}
return ;
}

HDU 4417 主席树写法的更多相关文章

  1. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  2. HDU 4417 划分树写法

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  3. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  4. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  6. HDU 2852 主席树

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 2655 主席树

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  9. HDU 2665(主席树,无修改第k小)

    Kth number                                                 Time Limit: 15000/5000 MS (Java/Others)   ...

随机推荐

  1. Oracle-数据库增删改查基本操作

    一.创建数据表 1).创建不存在的新表: create table tname(  Data_Name Date_Type [default][默认值]  );2).创建已存在表的副本 create ...

  2. Oracle作业练习题

    第一问 //登陆scott用户 //解锁 alter user scott account unlock; //给用户申请密码 alter user scott identified by tiger ...

  3. 【springmvc+mybatis项目实战】杰信商贸-3.需求分析与数据库建模

    开发步骤需求:生产厂家信息维护基础表FACTORY_C 1.业务需求:a)<需求说明书>     1)描述业务功能     生产厂家模块     功能:为在购销合同模块中的货物信息和附件信 ...

  4. spark操作数据库的几种方法

    一.使用jdbcRDD的接口: SparkConf conf = new SparkConf(); conf.setAppName("Simple Application").se ...

  5. [Clr via C#读书笔记]Cp9参数

    Cp9参数 可选参数和命名参数 参数设置了默认值(设置要从右到左,有默认值的参数必须放在没有默认值的参数的后面,默认值必须是常量),就可以使用可选参数和命名参数了.向方法传递实参的时候,编译器按照从左 ...

  6. 关于C#中如何使用wmi获得操作系统信息?

    最近项目中用到了windows server 2012操作系统中的存储池和ISCSI Disk的技术.前期,我们整个操作都是用power shell脚本去实现了.带来了不方便,后期要使用wmi API ...

  7. URAL 1297 Palindrome(Manacher)

    The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...

  8. POJ 2986 A Triangle and a Circle(三角形和圆形求交)

    Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...

  9. 物联网常见通信协议RFID、NFC、Bluetooth、ZigBee等梳理

    1  概述 在上一篇文章<物联网常见通信协议与通讯协议梳理[上]-通讯协议>中,对物联网常用通信协议和通讯协议作了区分,并对通讯协议进行了分享:本文将对常用的通信协议进行剖析,重点面向市场 ...

  10. File Searching

    Description Have you ever used file searching tools provided by an operating system? For example, in ...