http://acm.hdu.edu.cn/showproblem.php?pid=4417

你妹妹的 cin cout scanf printf.是差了好远呀,cin cout 会wa但scanf printf就ac了。。

了解了eaual_range()的神奇操作。。

做法就是线段树,vector很强大。。

View Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
using namespace std;
const int maxn=100001;
typedef vector < int >vi;
typedef pair < vi::iterator,vi::iterator> pii;
int h[maxn];
vi tree[maxn*4];
int n,m;
void build(int l,int r,int root)
{
    tree[root].clear();
    for(int i=l;i<=r;i++)
        tree[root].push_back(h[i]);
    sort(tree[root].begin(),tree[root].end());
    if(l==r)return ;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
int query(int ll,int rr,int h,int l,int r,int root)
{

    if(ll<=l&&r<=rr)
    {
        pii result;
        result=equal_range(tree[root].begin(),tree[root].end(),h);
        return result.second-tree[root].begin();
    }
    int ans=0;
    int m=(l+r)>>1;
    if(ll<=m)
    ans=query(ll,rr,h,lson);
    if(rr>m)
    ans+=query(ll,rr,h,rson);
    return ans;
}
void solve()
{
       int l,r,h;
       for(int j=1;j<=m;j++)
      {
        scanf("%d%d%d",&l,&r,&h);
        printf("%d\n",query(l,r,h,0,n-1,1));
      }
}
int main()
{
    int test;
    scanf("%d",&test);
    for(int i=1;i<=test;i++)
    {
        scanf("%d%d",&n,&m);
        for(int j=0;j<n;j++)
        scanf("%d",&h[j]);
        printf("Case %d:\n",i);
        build(0,n-1,1);
        solve();

    }
    return 0;
}
posted on 2012-09-24 20:05  一把刷子  阅读(174)  评论(0编辑  收藏  举报