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]);
}


目录
相关文章
|
6月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
17 0
|
6月前
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
|
6月前
poj 2352 Stars 树状数组
树状数组,简单题,我刚刚开始学的时候就a了,不多说什么了,直接贴代码。
19 0
|
6月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
20 0
|
6月前
Light oj 1112 - Curious Robin Hood(树状数组)
有n个数,有m组操作,1 i表示将第i个数先输出,然后置0, 2 i v 表示给第i个数加上v, 3 i j 表示求i 到 j 的和,注意,这里数组是从0开始的,而我们构造的树状数组是从1 开始的,使用在程序中要进行一定的处理。
23 0
|
6月前
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
22 0
|
8月前
|
人工智能
POJ 2299 Ultra-QuickSort(树状数组+离散化+求逆序数)
POJ 2299 Ultra-QuickSort(树状数组+离散化+求逆序数)