/******
分块+二分
***********/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1e5+50;
long n;// n个元素
long block;//每块元素个数
long num;// 块的个数
long belong[maxn]; // 第i个元素属于第几块
long l[1000], r[1000]; // 每块的左端点, 右端点
void build()
{
block = sqrt(n);
num = n/block;
if(n%block!= 0)num++;
for(long i = 1; i<= num; i++)
{
l[i] = (i-1)*block+1;
r[i] = i*block;
}
r[num] = n;
for(long i = 1; i<= n; i++)
{
belong[i] = (i-1)/block +1;
}
}
long arr[maxn];
long after[maxn];
void input()
{
for(long i = 1; i<= n; i++)
{
scanf("%ld", &arr[i]);
after[i] = arr[i];
}
for(int i = 1;i <= num;i++)
{
sort(after+l[i], after+r[i]+1);
}
}
long query(long x, long y, long h)
{
long res = 0;
if(belong[x] == belong[y])
{
for(long i = x; i <= y; i++)
{
if(arr[i] <= h)res++;
}
}
else
{
for(long i = x; i <= r[belong[x]]; i++)
{
if(arr[i] <= h)res++;
}
for(long i = l[belong[y]]; i <= y; i++)
{
if(arr[i]<= h)res++;
}
for(long i = belong[x]+1; i < belong[y]; i++)
{
long l1 = l[i], r1 = r[i];
long p = upper_bound(after+l1, after+r1+1, h)- (after+l1);
res+=p;
}
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
long m;
int T;
scanf("%d", &T);
int c = 0;
while(T--)
{
c++;
scanf("%ld %ld", &n, &m);
build();
input();
printf("Case %d:\n",c);
long x, y, h;
for(long i = 1; i <= m; i++)
{
scanf("%ld %ld %ld", &x, &y,&h);
long res = query(x+1, y+1, h);
printf("%ld\n", res);
}
}
return 0;
}
C - Super Mario(分块3)————分块
最新推荐文章于 2020-03-17 23:36:54 发布