Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9618 Accepted Submission(s): 4074
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.)
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
3
1
2
1
5
1
Source
题意就是求区间小于等于H的数的个数。
这道题写的时间有点长,错在这几个地方:
(1)因为数是从0开始的,所以查询的时候,区间l[i]和r[i]应该+1,查询的时候就l[i]-1,r[i],或者区间l[i]和r[i]不变化,查询的时候,就l[i],r[i]+1。
(2)因为查询的是小于等于H的数,所以H也应该离散化,找对应的数,而不是直接查询,这里错了好久才发现。
(3)数组开小了,杭电这道题数组开小了报的是WA,把maxn=1e5+10改成2e5+10就过了。
还有一点,其实是自己脑子不好特意测了一下。
因为数据已经离散化处理过了,所以查询的时候不会有重复的数,所以查询的时候,lower_bound()和upper_bound()都可以。
//int cnt=lower_bound(b+1,b+1+d,h[i])-b;
int cnt=upper_bound(b+,b++d,h[i])-b-;
以上两种都是对的。
代码:
//无修改区间-可持久化线段树(权值线段树+可持久化)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=2e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m
#define rson m+1,r int a[maxn],b[maxn],sum[maxn<<],ls[maxn<<],rs[maxn<<];//sum线段树里保存的值,L左儿子,R右儿子
int n,m,sz=; void build(int &rt,int l,int r)//建棵空树
{
rt=++sz;sum[rt]=;//动态开点,初始值为0,空树
if(l==r){
return ;
} int m=(l+r)>>;
build(ls[rt],lson);
build(rs[rt],rson);
} void update(int pre,int &rt,int l,int r,int p,int c)
{
rt=++sz;sum[rt]=sum[pre]+c;//插入序列,首先继承以前的线段树 然后直接单点+1就可以
ls[rt]=ls[pre];rs[rt]=rs[pre];
if(l==r){
return ;
} int m=(l+r)>>;
if(p<=m) update(ls[pre],ls[rt],lson,p,c);//因为右边不需要更新,所以覆盖掉左边
else update(rs[pre],rs[rt],rson,p,c);
//sum[rt]=sum[ls[rt]]+sum[rs[rt]];
} int query(int pre,int rt,int L,int R,int l,int r)//查询l到r区间就是第r次插入减去第l-1次插入后的线段树的样子
{
if(L>R) return ;
if(L<=l&&r<=R){
return sum[rt]-sum[pre];
} int ret=;
int m=(l+r)>>;
if(L<=m) ret+=query(ls[pre],ls[rt],L,R,lson);
if(R> m) ret+=query(rs[pre],rs[rt],L,R,rson);
return ret;
} int rt[maxn],l[maxn],r[maxn],h[maxn]; int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d%d",&n,&m);
sz=;
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],&h[i]);
b[i+n]=h[i];
l[i]++,r[i]++;
}
sort(b+,b++n+m);//首先把值全部排序去重,用于建权值线段树,权值线段树保存的内容是值的数量。
int d=unique(b+,b++n+m)-(b+);
build(rt[],,d);
for(int i=;i<=n;i++) //按照序列顺序插入值
{
int p=lower_bound(b+,b++d,a[i])-b;
update(rt[i-],rt[i],,d,p,);
}
printf("Case %d:\n",cas);
for(int i=;i<=m;i++)
{
//int L=1,R=upper_bound(b+1,b+1+d,h)-b-1;
//int cnt=lower_bound(b+1,b+1+d,h[i])-b;
int cnt=upper_bound(b+,b++d,h[i])-b-;
//printf("%d\n",query(rt[l[i]],rt[r[i]+1],1,cnt,1,d));
//printf("%d\n",query(rt[l],rt[r+1],1,cnt,1,d));
printf("%d\n",query(rt[l[i]-],rt[r[i]],,cnt,,d));
}
}
return ;
}
菜的难受。。。