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 0 0 3 1 2 0 1 5 1
#include <iostream>
#include <algorithm>
#include <string.h>
#define MAXN 100010
using namespace std;
class Node
{
public:
int l, r;
};
class SGtree
{
public:
Node node[MAXN << 2];
int num_left[20][MAXN];
int sg_node[20][MAXN];
int parray[MAXN];
void init();
void Maketree(int i, int d, int l, int r);
int Query(int i, int d, int x, int y, int k);
}tree;
void SGtree::init()
{
memset(num_left, 0, sizeof(0));
memset(sg_node, 0, sizeof(sg_node));
memset(node, 0, sizeof(node));
}
void SGtree::Maketree(int o, int d, int l, int r)
{
node[o].l = l;
node[o].r = r;
if (l == r)
{
return ;
}
int mid = (l + r) >> 1;
int issame = mid - l + 1;
for (int i = l; i <= r; i++)
{
if (sg_node[d][i] < parray[mid])
{
issame--;
}
}
int pl = l, pr = mid + 1;
for (int i = l; i <= r; i++)
{
if (i == l)
{
num_left[d][i] = 0;
}
else
{
num_left[d][i] = num_left[d][i - 1];
}
if (sg_node[d][i] < parray[mid])
{
num_left[d][i]++;
sg_node[d + 1][pl++] = sg_node[d][i];
}
if (sg_node[d][i] > parray[mid])
{
sg_node[d + 1][pr++] = sg_node[d][i];
}
if (sg_node[d][i] == parray[mid])
{
if (issame > 0)
{
issame--;
num_left[d][i]++;
sg_node[d + 1][pl++] = sg_node[d][i];
}
else
{
sg_node[d + 1][pr++] = sg_node[d][i];
}
}
}
Maketree(2 * o, d + 1, l, mid);
Maketree(2 * o + 1, d + 1, mid + 1, r);
}
int SGtree::Query(int i, int d, int x, int y, int k)
{
int l = node[i].l;
int r = node[i].r;
int mid = (l + r) >> 1;
if (l == r)
{
return sg_node[d][x];
}
int lnum, ltornum;
if (x == node[i].l)
{
lnum = 0;
}
else
{
lnum = num_left[d][x - 1];
}
ltornum = num_left[d][y] - lnum;
if (ltornum >= k)
{
return Query(i * 2, d + 1, l + lnum, l + lnum + ltornum - 1, k);
}
else
{
int a = x - l - lnum;
int b = y - x - ltornum;
return Query(i * 2 + 1, d + 1, mid + a + 1, mid + a + b + 1, k - ltornum);
}
}
void input()
{
int t, n, m, x, y, k, c = 0;
cin >> t;
while (t--)
{
tree.init();
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &tree.parray[i]);
tree.sg_node[1][i] = tree.parray[i];
}
sort(tree.parray + 1, tree.parray + n + 1);
tree.Maketree(1, 1, 1, n);
cout << "Case " << ++c << ':' << endl;
while (m--)
{
scanf("%d %d %d", &x, &y, &k);
int low = 1, high = y - x + 1;
while (low <= high)
{
int mid = ((low + high) >> 1);
if (k >= tree.Query(1, 1, x + 1, y + 1, mid))
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
printf("%d\n", low - 1);
}
}
}
int main()
{
input();
return 0;
}