树状数组:解决比某个数小的数的出现次数

 1 //输入一串n个数字,然后进行m次询问
 2 //每次询问中询问一个在上述数字串出现过的一个数,问比这个数字小的数字有几个
 3 //出现的重复的数字当作一个数字处理
 4 //n<=1000000, m<=100000
 5
 6 //  sample input:
 7 //  11
 8 //  4 5 5 8 9 1 0 0 100 99 45
 9 //  3
10 //  5
11 //  100
12 //  9
13
14 //  output:
15 //  3
16 //  8
17 //  5
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int a[1000001];
int c[1000002];

int lowbit(int x)
{
    return x&(-x);
}

int sum(int x)
{
    int cnt=0;
    while(x>0)
    {
        cnt+=c[x];
        x-=lowbit(x);
    }
    return cnt;
}

int main()
{
    int n, m, dd, flag=0;
    int i, j;
    scanf("%d", &n);
    memset(a, 0, sizeof(a));
    int mm=-1;
    for(i=0; i<n; i++)
    {
        scanf("%d", &dd);
        if(dd==0)
            falg=1;
        if(dd>mm) mm=dd;
        a[dd]=1; //
    }
    //
    for(i=0; i<=mm; i++)
    {
        c[i]=0;
        for(j=i-lowbit(i)+1; j<=i; j++ )
        {
            c[i]+=a[j];
        }
    }
    scanf("%d", &m);
    while(m--)
    {
        scanf("%d", &dd);
        printf("%d\n", sum(dd)-1+flag );
    }
    return 0;
}
时间: 2024-08-06 11:56:35

树状数组:解决比某个数小的数的出现次数的相关文章

hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4766    Accepted Submission(s): 1727 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening.

hdu4417 树状数组(求指定区间比指定数小的数的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4417 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.

[扫描线+树状数组]解决矩形包含点的问题

今天做到第二题,大部分的思路都理解了之后最后剩下一个问题 zzx:“然后扫描线+树状数组搞一下就好了” 看到这两个算法就产生了一种我肯定会的错觉... 然后后来发现并不会的时候很惭愧... 然后十分感谢YDC,在之前完全陌生的情况下 我去问这样一个问题 超级超级超级耐心地给我解答  我问着每一个他们看起来显然的具体处理方法 突然发现真的像张老师说的:“你普及升提高的这条路没有怎么走,中间那块知识是空着的啊” 今天才切实体会到...但是或许正是这样今天学到这样的一个东西更让我觉得开心吧 树状数组解

【转载】【树状数组区间第K大/小】

原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=sum of A[ans-lowbit(ans)+1 ... ans];二,当ans=2^k时, c[ans]=sum of A[1 ... ans]; 下面说明findK(k)如何运作:1,设置边界条件ans,ans'<maxn且cnt<=k:2,初始化cnt=c[ans],其中ans=2^k且k

URAL 1521 War Games 2 树状数组解决约瑟夫环,输出离队顺序

1521. War Games 2 Time limit: 1.0 second Memory limit: 64 MB Background During the latest war games (this story is fully described in the problem "War games") the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to m

POJ3067(树状数组:统计数字出现个数)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24151   Accepted: 6535 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

主席树套树状数组 动态区间第k小

先打上代码以后更新解释 1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #define REP(i, s, n) for(int i = s; i <= n; i ++) 7 #define RAP(i, n, s) for(int i = n; i >= s; i --

A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 69589   Accepted: 21437 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ2481(树状数组:统计数字 出现个数)

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15405   Accepted: 5133 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in hi