poj 2352 Stars 树状数组

简介:

     统计x之前出现数字的次数,线段树和树状数组都可以,但明显树状数组更简洁


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int ans[15005];
int c[32005],n;
inline int low(int x)
{
    return x&-x;
}
int add(int pos,int num)
{
    while(pos<=32001)
    {
        c[pos]+=num;
        pos+=low(pos);
    }
}
int query(int pos)
{
    int ans=0;
    while(pos>0)
    {
        ans+=c[pos];
        pos-=low(pos);
    }
    return ans;
}
int main()
{
    memset(ans,0,sizeof(ans));
    memset(c,0,sizeof(c));
    scanf("%d",&n);
    int i,x,y,t;
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        x++;
        ans[query(x)]++;
        add(x,1);
    }
    for(i=0;i<n;i++)
      printf("%d\n",ans[i]);
}


目录
相关文章
|
8月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
22 0
|
8月前
poj 2352 Stars 树状数组
树状数组,简单题,我刚刚开始学的时候就a了,不多说什么了,直接贴代码。
24 0
|
8月前
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
25 0
UVa 11292 - Dragon of Loowater(排序贪心)
UVa 11292 - Dragon of Loowater(排序贪心)
80 0
|
人工智能 BI 存储
|
Java
HDU 2689 Sort it【树状数组】
Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4672    Accepted Submission(s): 3244 ...
1086 0
|
Java
HDU 1754 I Hate It(线段树之单点更新,区间最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 70863    Accepted Submission(s): 27424 Problem Description 很多学校流行一种比较的习惯。
1085 0