题面
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
把每条询问以结构体储存起来,按照H的值从小到大排列
同时记录每条询问的序号;
把数列也从小到大排列,记录序列号;
然后从“最小”的询问开始遍历,如果node[j].cnt<=op[i].sum,将这个数更新到线段树中,然后依次将每个询问
范围内的数用query()求出来储存再res数组中
代码
//离线+线段树
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 1e5+10;
struct nod{
int l,r,sum;
}tree[N<<2];
struct np{
int id,cnt;
}node[N];
struct nd{
int l,r,sum,id;
}op[N];
int a[N];
void pushup(int k)
{
tree[k].sum = tree[k<<1].sum + tree[k<<1|1].sum;
}
void build(int k, int l, int r)
{
tree[k] = {l,r,0};
if(l==r)
{
return ;
}
int mid = (l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
pushup(k);
}
void update(int k, int pos)
{
if(tree[k].r==tree[k].l)
{
tree[k].sum = 1;
return ;
}
int mid = (tree[k].l+tree[k].r)>>1;
if(pos<=mid)
update(k<<1,pos);
else
update(k<<1|1,pos);
pushup(k);
}
int query(int k, int l, int r)
{
if(tree[k].l>r||tree[k].r<l)
return 0;
if(tree[k].l>=l&&tree[k].r<=r)
return tree[k].sum;
return query(k<<1,l,r)+query(k<<1|1,l,r);
}
bool cmp(np a, np b)
{
return a.cnt <b.cnt ;
}
bool cmp1(nd a, nd b)
{
return a.sum < b.sum ;
}
int main()
{
// freopen("E:\\000.txt","r",stdin);
int t; cin >> t;
for(int k=1;k<=t;k++)
{
int n,m,x;
cin >> n >> m;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
node[i].id = i;
node[i].cnt = x;
}
sort(node+1,node+1+n,cmp);
for(int i=1;i<=m;i++)
{
// scanf("%d%d%d",op[i].l ,op[i].r ,op[i].sum );
cin >>op[i].l >> op[i].r >>op[i].sum;
op[i].l++;
op[i].r++;
op[i].id = i;
}
sort(op+1,op+1+m,cmp1);
build(1,1,n);
int j=1;
int res[N];
for(int i=1;i<=m;i++)
{
while(op[i].sum>=node[j].cnt&&j<=n)
{
update(1,node[j].id);
j++;
}
res[op[i].id] = query(1,op[i].l ,op[i].r );
}
printf("Case %d:\n",k);
for(int i=1;i<=m;i++)
printf("%d\n",res[i]);
}
return 0;
}