![]() | ||||||||||
| ||||||||||
Super MarioTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2378 Accepted Submission(s): 1155
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
Sample Output
|
题意:n个数,m次查询,每次查询区间内小于h的数的个数。
方法一:离线处理,对数从小到大排序,对查询按照高度h从小到大排序,然后依次添加到树状数组里,然后看小于询问的值得数是否都加到里面,然后查询区间内的个数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=1000000000;
int n,m;
int ans[maxn];
struct D
{
int val,id;
bool operator < (const D & a)const
{
return val<a.val;
}
}a[maxn];
struct Q
{
int x,y,h,id;
bool operator < (const Q & a)const
{
return h<a.h;
}
}qu[maxn];
struct BIT
{
int val[maxn];
void clear(){memset(val,0,sizeof(val));}
int lowbit(int x)
{
return x&(-x);
}
void update(int pos,int x)
{
while(pos<=n)
{
val[pos]+=x;
pos+=lowbit(pos);
}
}
int getsum(int x)
{
int sum=0;
while(x>0)
{
sum+=val[x];
x-=lowbit(x);
}
return sum;
}
}tree;
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id=i;
}
for(int i=1;i<=m;i++)
{
int x,b,h;
scanf("%d%d%d",&x,&b,&qu[i].h);
x++,b++;
qu[i].x=x,qu[i].y=b;
qu[i].id=i;
}
sort(a+1,a+n+1);
sort(qu+1,qu+1+m);
tree.clear();
memset(ans,0,sizeof(ans));
int cnt=1;
for(int i=1;i<=n;i++)
{
while(cnt<=m&&qu[cnt].h<a[i].val)
{
ans[qu[cnt].id]=tree.getsum(qu[cnt].y)-tree.getsum(qu[cnt].x-1);
cnt++;
}
tree.update(a[i].id,1);
}
for(int i=cnt;i<=m;i++)
ans[qu[i].id]=tree.getsum(qu[i].y)-tree.getsum(qu[i].x-1);
printf("Case %d:\n",cas++);
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}
return 0;
}
方法二:划分树。对于每个查询,二分区间里的第k大数,找出大于要查询的c的第一个数的是第k大,那么答案就是这个k-1。PS:划分树是一种基于线段树的数据结构。主要用于快速求出(在log(n)的时间复杂度内)序列区间的第k大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MID(a,b) (a+((b-a)>>1))
const int N=1e5+5;
struct P_Tree
{
int n,order[N];
int valu[20][N],num[20][N];
void init(int len)
{
n=len;
for(int i=0;i<20;i++) valu[i][0]=0,num[i][0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&order[i]);
valu[0][i]=order[i];
}
sort(order+1,order+1+n);
build(1,n,0);
}
void build(int lft,int rht,int ind)
{
if(lft==rht) return;
int mid=MID(lft,rht);
int ln=lft,rn=mid+1,same=mid-lft+1;
for(int i=lft;i<=rht;i++)
if(valu[ind][i]<order[mid]) same--;
for(int i=lft;i<=rht;i++)
{
int flag=0;
if((valu[ind][i]<order[mid])||(valu[ind][i]==order[mid]&&same))
{
flag=1;
valu[ind+1][ln++]=valu[ind][i];
if(valu[ind][i]==order[mid]) same--;
}
else valu[ind+1][rn++]=valu[ind][i];
num[ind][i]=num[ind][i-1]+flag;
}
build(lft,mid,ind+1);
build(mid+1,rht,ind+1);
}
int query(int st,int ed,int k,int lft,int rht,int ind)
{
if(k==0) return -1;
if(ed-st+1<k) return (1<<30);
if(lft==rht) return valu[ind][lft];
int mid=MID(lft,rht);
int lx=num[ind][st-1]-num[ind][lft-1];
int ly=num[ind][ed]-num[ind][st-1];
int rx=st-1-lft+1-lx;
int ry=ed-st+1-ly;
if(ly>=k) return query(lft+lx,lft+lx+ly-1,k,lft,mid,ind+1);
else
{
st=mid+1+rx;
ed=mid+1+rx+ry-1;
return query(st,ed,k-ly,mid+1,rht,ind+1);
}
}
}tree;
int main()
{
int t,t_cnt=0;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
tree.init(n);
printf("Case %d:\n",++t_cnt);
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
a++; b++;
int lft=0,rht=b-a+2;
while(lft<rht)
{
int mid=MID(lft,rht);
int ele=tree.query(a,b,mid,1,n,0);
if(ele<=c) lft=mid+1;
else rht=mid;
}
printf("%d\n",lft-1);
}
}
}
这个题还可以用主席树做
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int maxm=maxn*30;
int n,q,m;
int a[maxn];
int order[maxn];
int T[maxm],cnt[maxm],lson[maxm],rson[maxm];
int num;
void init()
{
for(int i=1;i<=n;i++)order[i]=a[i];
sort(order+1,order+1+n);
m=unique(order+1,order+1+n)-order-1;
num=0;
}
int find(int x)
{
return lower_bound(order+1,order+1+m,x)-order;
}
int build(int l,int r)
{
int root=num++;
cnt[root]=0;
if(l==r)return root;
int mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
return root;
}
int update(int root,int pos,int val)
{
int newroot=num++,tmp=newroot;
cnt[newroot]=cnt[root]+val;
int l=1,r=m;
while(l<r)
{
int mid=(l+r)>>1;
if(pos<=mid)
{
r=mid;
lson[newroot]=num++;rson[newroot]=rson[root];
newroot=lson[newroot],root=lson[root];
}
else
{
l=mid+1;
lson[newroot]=lson[root],rson[newroot]=num++;
newroot=rson[newroot],root=rson[root];
}
cnt[newroot]=cnt[root]+val;
}
return tmp;
}
int query(int left,int right,int k)
{
int ans=0;
int l=1,r=m;
while(l<r)
{
int mid=(l+r)>>1;
if(k<=mid)
{
r=mid;
left=lson[left];
right=lson[right];
}
else
{
l=mid+1;
ans+=cnt[lson[right]]-cnt[lson[left]];
left=rson[left],right=rson[right];
}
}
return ans+(k<l?0:cnt[right]-cnt[left]);
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
init();
T[0]=build(1,m);
for(int i=1;i<=n;i++)
T[i]=update(T[i-1],find(a[i]),1);
printf("Case %d:\n",cas++);
while(q--)
{
int l,r,h;
scanf("%d%d%d",&l,&r,&h);
h=upper_bound(order+1,order+1+m,h)-order-1;
printf("%d\n",query(T[l],T[r+1],h));
}
}
return 0;
}