一.题目链接:

HDU-4417

二.题目大意:

查询区间小于 k 的个数

三.分析:

这里只是存个板子~~

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;

const int M = (int)1e5;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

int a[M + 5];
int b[M + 5];
int n, m, len;

int cnt;
struct node
{
    int sum;
    int l, r;
}tree[M * 40 + 5];
int root[M + 5];

void discrete()
{
    memcpy(b, a, sizeof(a));
    sort(b + 1, b + n + 1);
    len = unique(b + 1, b + n + 1) - (b + 1);
}

int tofind(int x)
{
    return upper_bound(b + 1, b + len + 1, x) - b;
}

void update(int &x, int y, int l, int r, int v)
{
    tree[++cnt] = tree[y];
    tree[cnt].sum++;
    x = cnt;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    if(v <= mid)
        update(tree[x].l, tree[y].l, l, mid, v);
    else
        update(tree[x].r, tree[y].r, mid + 1, r, v);
}

int query(int x, int y, int l, int r, int a, int b)
{
    if(l >= a && r <= b)
        return tree[y].sum - tree[x].sum;
    int sum = 0;
    int mid = (l + r) >> 1;
    if(a <= mid)
        sum += query(tree[x].l, tree[y].l, l, mid, a, b);
    if(mid < b)
        sum += query(tree[x].r, tree[y].r, mid + 1, r, a, b);
    return sum;
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ++ca)
    {
        cnt = 0;
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        discrete();
        for(int i = 1; i <= n; ++i)
            update(root[i], root[i - 1], 1, n, tofind(a[i]) - 1);
        int l, r, k;
        printf("Case %d:\n", ca);
        while((m--) > 0)
        {
            scanf("%d %d %d", &l, &r, &k);
            k = tofind(k) - 1;
            l++, r++;
            if(k)
                printf("%d\n", query(root[l - 1], root[r], 1, n, 1, k));
            else
                printf("0\n");
        }
    }
    return 0;
}