/* 首先对问题进行转换,将数据分别用数组做存储,按h大小进行排序,然后从小到大进行询问操作,每次操作之前,首先将第一个数组中h值比这个值小或等于的入队, 于是问题转换为线段树问题 */ #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int nMax = 100007; struct Tree { int l, r; int w; Tree(){} Tree(int l, int r, int w):l(l), r(r), w(w){} }tree[4 * nMax]; struct Dat { int id, num; }dat[nMax]; struct Ask { int id, l, r, h; }ask[nMax]; int ans[nMax]; void build(int rt, int l, int r) { if(l < r) { int mid = (l + r) / 2; build(rt * 2, l, mid); build(rt * 2 + 1, mid + 1, r); } tree[rt] = Tree(l, r, 0); } void update(int rt, int id) { if(tree[rt].l <= id && tree[rt].r >= id) { tree[rt].w ++; if(tree[rt].l == tree[rt].r) return; update(rt * 2, id); update(rt * 2 + 1, id); } } int sum(int rt, int l, int r) { //int ans = 0; if(r < 0) return 0; if(tree[rt].l == l && tree[rt].r == r) return tree[rt].w; int mid = (tree[rt].l + tree[rt].r) / 2; if(r <= mid) return sum(rt * 2, l, r); else if(mid + 1 <= l) return sum(rt * 2 + 1, l, r); else return sum(rt * 2, l, mid) + sum(rt * 2 + 1, mid + 1, r); } int cmp1(const void *a, const void *b) { Dat *pa = (Dat *) a; Dat *pb = (Dat *) b; return pa ->num - pb ->num; } int cmp2(const void *a, const void *b) { Ask *pa = (Ask *) a; Ask *pb = (Ask *) b; return pa ->h - pb ->h; } int main() { //cout << "Hello world!" << endl; //freopen("e://data.in", "r", stdin); int T; scanf("%d", &T); int cas; int n, m; for(cas = 1; cas <= T; cas ++) { scanf("%d%d", &n, &m); build(1, 0, n - 1); int i, j; for(i = 0; i < n; ++ i) { scanf("%d", &dat[i].num); dat[i].id = i; } for(i = 0; i < m; ++ i) { scanf("%d%d%d", &ask[i].l, &ask[i].r, &ask[i].h); ask[i].id = i; } qsort(dat, n, sizeof(dat[0]), cmp1); qsort(ask, m, sizeof(ask[0]), cmp2); for(i = 0, j = 0; i < m; ++ i) { while(j < n && dat[j].num <= ask[i].h) { update(1, dat[j].id); j ++; } ans[ask[i].id] = sum(1, 0, ask[i].r) - sum(1, 0, ask[i].l - 1); } printf("Case %d:\n", cas); for(i = 0; i < m; ++ i) printf("%d\n", ans[i]); } return 0; }