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[i];

i-=lowbit(i);

}

return sum;

}

void update(int i,int dx)

{

while(i<=maxlen)

{

tree[i]+=dx;

i+=lowbit(i);

}

}

int main()

{

int n;

int x,y;int i;

int ans[maxn];

while(scanf("%d",&n)!=EOF)

{

memset(ans,0,sizeof(ans));

memset(tree,0,sizeof(tree));

for(i=0;i<n;i++)

{

int x,y;

scanf("%d%d",&x,&y);

x++;

ans[getsum(x)]++;

update(x,1);

}

for(i=0;i<n;i++)

printf("%d\n",ans[i]);

}

return 0;

}

时间: 2024-08-28 18:39:01

hdu 1541 Stars 树状数组模板题的相关文章

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

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): 5140    Accepted Submission(s): 2020 Problem Description Astronomers often examine star maps where stars are represented by points o

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-=lowbi

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ

树状数组模板题(特强浓雾

#include<bits/stdc++.h> using namespace std; int a,b; int c[100005]; void add(int x,int y)//树状数组初始化 { while(x<=a+b) c[x]+=y,x+=x&-x;//露馅了噗哈哈哈哈哈哈蛤哈哈哈哈哈哈 } int ask(int x)//询问 { int ans=0; while(x) ans+=c[x],x-=x&-x; return ans; } int main()

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

hdu1541 Stars 树状数组水题

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