Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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小于等于h的数目;
思路:主席树求法,单点更新,区间查询;
   

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
struct Chairmantree
{
int rt[N*],ls[N*],rs[N*],sum[N*];
int tot;
void init()
{
tot=;
}
void build(int l,int r,int &pos)
{
pos=++tot;
sum[pos]=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(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 s,int t,int L,int R,int l,int r)
{
if(L<=l&&r<=R)
return sum[t]-sum[s];
int mid=(l+r)>>;
int ans=;
if(L<=mid)
ans+=query(ls[s],ls[t],L,R,l,mid);
if(R>mid)
ans+=query(rs[s],rs[t],L,R,mid+,r);
return ans;
}
};
Chairmantree tree;
int a[N],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()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
//for(int i=1;i<num;i++)
// printf("%d ",tree.rt[i]);
//printf("\n");
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.build(,num-,tree.rt[]);
for(int i=;i<=n;i++)
{
int p=getpos(a[i],num);
tree.update(p,,tree.rt[i-],,num-,tree.rt[i]);
}
printf("Case %d:\n",cas++);
for(int i=;i<=m;i++)
{
int p=getpos(x[i],num);
printf("%d\n",tree.query(tree.rt[l[i]],tree.rt[r[i]+],,p,,num-));
}
}
return ;
}

树状数组求法:

  按照值的大小排序;

  标记区间的下标,详见代码;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<" "<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int n,m;
struct treearray
{
int tree[N];
void init()
{
memset(tree,,sizeof(tree));
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int query(int x)
{
int sum=;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
}treearray;
struct a
{
int x,pos;
bool operator <(const a b)const
{
return x<b.x;
}
}a[N];
struct q
{
int l,r,pos,h;
bool operator <(const q b)const
{
return h<b.h;
}
}q[N];
int ans[N];
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i].x),a[i].pos=i;
printf("Case %d:\n",cas++);
for(int i=;i<=m;i++)
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h),q[i].pos=i;
sort(q+,q++m);
sort(a+,a++n);
treearray.init();
int st=;
for(int i=;i<=m;i++)
{
//bug(q[i].pos);
while(st<=n&&a[st].x<=q[i].h)
{
//cout<<a[st].pos<<endl;
treearray.update(a[st].pos,);
st++;
}
ans[q[i].pos]=treearray.query(q[i].r+)-treearray.query(q[i].l);
}
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
return ;
}
 

hdu 4417 Super Mario 树状数组||主席树的更多相关文章

  1. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  2. BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树

    BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树 题意: 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i, ...

  3. 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树

    题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...

  4. 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树

    题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...

  5. BZOJ_2120_数颜色_Set+树状数组+主席树

    BZOJ_2120_数颜色_Set+树状数组+主席树 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L ...

  6. P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]

    题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...

  7. HDU 3333 Turing Tree(树状数组/主席树)

    题意 给定一个长度为 \(n​\) 的序列,\(m​\) 个查询,每次查询区间 \([L,R]​\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...

  8. 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...

  9. hdu_5788_Level Up(树状数组+主席树)

    题目链接:hdu_5788_Level Up 题意: 有一棵树,n个节点,每个节点有个能力值A[i],mid[i],mid的值为第i节点的子树的中位数(包括本身),现在让你将其中的一个节点的A值改为1 ...

随机推荐

  1. 聊聊对APM的理解

    本文主要从以下几个列举对APM的认识: -什么是APM工具 -为什么要用APM工具,APM工具的价值在哪里: -什么样的APM工具适合于传统金融业: -如何用好APM工具:    -精准告警    - ...

  2. 查询hadoop参数变量

    [hadoop@master hadoop]$ hive -S -e 'set -v'|grep querylog|grep -E -v 'CLASSPATH|class'hive.querylog. ...

  3. 从零打造在线网盘系统之Struts2框架配置全解析

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  4. Win7系统右上角没有搜索怎么办?Win7找回资源管理器中的搜索框

    最近有win7系统用户发现打开资源管理器,文件夹等右上角没有搜索框,这让人十分不方便无法进行搜索,那么如何找回呢?下面小编就分享一下方法给大家.推荐 最好用的Win7系统下载 操作步骤: 1.打开Wi ...

  5. 170710、springboot编程之启动器Starter详解

    此文系参考网络大牛的,如有侵权,请见谅! Spring Boot应用启动器基本的一共有N(现知道的是44)种:具体如下: 1)spring-boot-starter 这是Spring Boot的核心启 ...

  6. jq和axios的取消请求

    场景: 分页: 每次点击分页会发送请求,如果上一次请求还未获取到,下一次请求已经开始且先一步获取到,那么数据上会出现问题. 快速点击会发送多次请求,多次点击的时候一般的做法我们会定义一个flag,此时 ...

  7. 优云老王的心路历程(二):下一站Web体验监控产品

    在上一篇文章中,和大家聊到了建立Web应用体验监控体系,经过了概念阶段,也完成了技术选型,就进入了把实质性的产品研发阶段.作为产品经理,时刻不敢忘记我们的产品目标:无限感知你的用户,建立完备的体验监控 ...

  8. request对象的常用属性和方法

    request的属性 /* 1.HttpRequest.GET 一个类似于字典的对象,包含 HTTP GET 的所有参数.详情请参考 QueryDict 对象. 2.HttpRequest.POST ...

  9. [随感]GIS开发的困惑

    从事GIS应用开发也有3年了,但是做了些东西自己始终不满意,不是不稳定就是效率低,不是功能杂就是不实用! 首先是AE开发,我必须说自己很欣赏ArcGIS的软件设计架构和思想.但是在开发的过程中也确实遇 ...

  10. [svc]ip地址划分

    网络界有2个计算题,一个是子网掩码,另一个就是三次握手,四次回收序列号计算了. 学会如何划分等长子网 学会如何合并网段 学会ip是否能分配 理解特殊的ip地址 ip头部 ip地址分类 现在的IP网络使 ...