您的位置:首页 > 其它

hdu 4417 Super Mario(树状数组+离线处理)

2012-09-23 23:18 399 查看
按H排序,每次查询把不大于H的值插入到树状数组中的相应位置并完成查询。

171MS

View Code

/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
const int INF = 2147483647;

const int maxn = 100005;
int C[maxn];
struct query
{
int l,r,h,pos,ans;
}q[maxn];
struct bri
{
int num,pos;
}a[maxn];
int n,m;

int cmp1(bri a,bri b)
{
return a.num<b.num;
}
int cmp2(query a,query b)
{
return a.h<b.h;
}
int cmp3(query a,query b)
{
return a.pos<b.pos;
}
void add(int x,int pos)
{
while(pos<=n)
{
C[pos] += x;
pos += lowbit(pos);
}
}
int sum(int pos)
{
int res = 0;
while(pos > 0)
{
res += C[pos];
pos -= lowbit(pos);
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
#endif
int T;
int cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(C,0,(n+1)*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].num);
a[i].pos = i + 1;
}
sort(a,a+n,cmp1);
printf("Case %d:\n",++cas);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
q[i].pos=i + 1;
}
sort(q,q+m,cmp2);
int k=0;
for(int i=0;i<m;i++)
{
while(a[k].num <= q[i].h && k < n)
{
add(1,a[k].pos);
k++;
}
q[i].ans = sum(q[i].r+1) - sum(q[i].l);
}
sort(q,q+m,cmp3);
for(int i=0;i<m;i++)
{
printf("%d\n",q[i].ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: