HDU 1541 Stars 树状数组

看完题解自己敲了一遍 还需要加深理解吧

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int c[50000],num[50000];
int N=32100;
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);
    }
}
int main()
{
    int t;
    int i,j;
    while(cin>>t)
    {
        memset(c,0,sizeof(c));
        memset(num,0,sizeof(num));
        for(i=0;i<t;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            num[sum(x+1)]++;
            add(x+1,1);
        }
        for(i=0;i<t;i++)
        {
            printf("%d\n",num[i]);
        }
    }
    return 0;
}

  

时间: 2024-10-26 13:43:28

HDU 1541 Stars 树状数组的相关文章

POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

一开始想,总感觉是DP,可是最后什么都没想到.还暴力的交了一发. 然后开始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得很清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?其实y根本木有用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.edu

POJ 2352 &amp;amp;&amp;amp; HDU 1541 Stars (树状数组)

一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得非常清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?事实上y根本木实用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.e

hdu 1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5210    Accepted Submission(s): 2053 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 1541 Stars 树状数组简单应用

点击打开链接 Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5140    Accepted Submission(s): 2020 Problem Description Astronomers often examine star maps where stars are represented by points o

hdu 1541 Stars 树状数组模板题

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int  maxn=15010; const int maxlen=32010; int tree[4*maxn]; int lowbit(int n) { return (n&-n); } int getsum(int i) { int sum=0; while(i>0) { sum+=tree

HDU 1541 star (树状数组)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5400    Accepted Submission(s): 2133 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cow

hdu 5193 分块 树状数组 逆序对

题意: 给出n个数,a1,a2,a3,...,an,给出m个修改,每个修改往数组的某个位置后面插入一个数,或者把某个位置上的数移除.求每次修改后逆序对的个数. 限制: 1 <= n,m <= 20000; 1 <= ai <= n 思路: 插入和删除用分块来处理,块与块之间用双向链表来维护,每一块用树状数组来求小于某个数的数有多少个. 外层可以使用分块维护下标,这样添加和删除元素的时候,也很方便,直接暴力.查找权值个数时,使用树状数组比较方便.内层通过树状数组维护权值. 每次更新即

HDU 5493 Queue 树状数组

Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Description N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now,