HDU4417

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3839    Accepted Submission(s): 1764


Problem 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.)
 

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
 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online
 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
#define lson L, mid, rt<<1
#define rson mid+1, R, rt<<1|1
#define N 100010

int Max[N << 2];
int n, m;

struct Node
{
    int val, pos;
    bool operator < (const Node& a) const
    {
        return val < a.val;
    }
} node[N];

struct oper
{
    int l, r, h, id;
} op[N];

void pushup(int rt)
{
    Max[rt] = Max[rt<<1] + Max[rt<<1|1];
}

void build(int L, int R, int rt)
{
    if(L == R)
    {
        Max[rt] = 0;
        return ;
    }
    int mid = (L + R) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int t, int L, int R, int rt)
{
    if(L == R)
    {
        Max[rt]++;
        return ;
    }
    int mid = (L + R) >> 1;
    if(t <= mid) update(t, lson);
    else update(t, rson);
    pushup(rt);
}

int query(int l, int r, int L, int R, int rt)
{
    if(l <= L && r >= R) return Max[rt];
    int mid = (L + R) >> 1;
    if(r <= mid) return query(l, r, lson);
    else if(l > mid) return query(l, r, rson);
    else return query(l, mid, lson) + query(mid + 1, r, rson);
}

bool cmp(oper t1, oper t2)
{
    return t1.h < t2.h;
}

int ans[N];

int main()
{
    int _T, kase = 0;
    scanf("%d", &_T);
    while(_T--)
    {
        printf("Case %d:\n", ++kase);
        scanf("%d%d", &n, &m);
        build(1, n, 1);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &node[i].val);
            node[i].pos = i;
        }
        sort(node + 1, node + 1 + n);
        int L, R, H;
        for(int i = 1; i <= m; i++)
            {
                scanf("%d%d%d", &op[i].l, &op[i].r, &op[i].h);
                op[i].id = i;
            }
        sort(op + 1, op + 1 + m, cmp);
        int num = 1;
        for(int i = 1; i <= m; i++)
        {
            while(num <= n && node[num].val <= op[i].h)
            {
                update(node[num].pos, 1, n, 1);
                num++;
            }
            ans[op[i].id] = query(op[i].l + 1, op[i].r + 1, 1, n, 1);
        }
        for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
    }
    return 0;
}


你可能感兴趣的:(ACM)