求任意区间里比x小的数的个数(树状数组)

题目变形:http://codeforces.com/contest/811/problem/B

题意:对任意区间排序,问你第x个位置上的数的位置是否发生变化

在得知n^2能过这个题我的内心是崩溃的.........


#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
#include <iostream>  
using namespace std;  
const int maxn=30005;  
int aa[maxn],bb[maxn];
int C[maxn],ans[maxn];  
int n,m;  
  
int lowbit(int x)  
{  
    return x&(-x);  
}  
  
int sum(int x)  
{  
    int ret=0;  
    while(x>0)  
    {  
        ret+=C[x];  
        x-=lowbit(x);  
    }  
    return ret;  
}  
  
void add(int x,int d)  
{  
    while(x<=n)  
    {  
        C[x]+=d;  
        x+=lowbit(x);  
    }  
}  
  
struct node  
{  
    int id,num;  
    bool operator < (const node &other) const  
    {  
        return num < other.num;  
    }  
}a[maxn];  
struct note  
{  
    int l,r,id,value;  
    bool operator < (const note &other)const  
    {  
        return value <other.value;  
    }  
}b[maxn];  
  
int main()  
{   
        scanf("%d%d",&n,&m);  
        for(int i=1;i<=n;i++)  
        {  
            int x;  
            scanf("%d",&a[i].num);  
            a[i].id=i;  
        }  
        for(int i=1;i<=m;i++)  
        {  
            scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].value); 
			bb[i]=b[i].value;
			b[i].value=a[b[i].value].num;
            b[i].id=i; 
			aa[i]=b[i].l;
        }  
        sort(a+1,a+n+1);  
        sort(b+1,b+m+1);  
       // memset(C,0,sizeof(C));  
        int k=1;  
        for(int i=1;i<=m;i++)  
        {  
            while(k<=n&&a[k].num<b[i].value)  
            {  
                add(a[k].id,1);  
                k++;  
            }  
            ans[b[i].id]=sum(b[i].r)-sum(b[i].l-1);  
        }   
        for(int i=1;i<=m;i++)  
        {  
			if(ans[i]!=bb[i]-aa[i])
				printf("No\n");
			else
				printf("Yes\n");
           // printf("%d\n",ans[i]);  
        }    
    return 0;  
}  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值