Stars(树状数组或线段树)
Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37323 Accepted: 16278
Description
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it’s formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
Source
Ural Collegiate Programming Contest 1999
线段树和树状数组皆可AC
树状数组(和金巨学的)
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 32010;
int c[MAX];
int level[MAX/2];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void add(int x)
{
while(x<MAX)
{
c[x]++;
x+=lowbit(x);
}
}
int main()
{
int n;
int x,y;
memset(level,0,sizeof(level));
memset(c,0,sizeof(c));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
++x;
level[sum(x)]++;
add(x);
}
for(int i=0;i<n;i++)
{
printf("%d\n",level[i]);
}
return 0;
}
线段树
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const LL MAX = 32000;
int a[MAX*4];
int level[MAX];
int Query(int L,int R,int l,int r,int site)
{
if(L==l&&R==r)
{
return a[site];
}
int mid=(L+R)>>1;
if(r<=mid)
{
return Query(L,mid,l,r,site<<1);
}
else if(l>mid)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
void update(int L,int R,int s,int site)
{
a[site]++;
if(L==R)
{
return ;
}
int mid=(L+R)>>1;
if(s<=mid)
{
update(L,mid,s,site<<1);
}
else
{
update(mid+1,R,s,site<<1|1);
}
}
int main()
{
int n;
int x,y;
memset(level,0,sizeof(level));
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
level[Query(0,MAX,0,x,1)]++;
update(0,MAX,x,1);
}
for(int i=0;i<n;i++)
{
printf("%d\n",level[i]);
}
return 0;
}
Stars(树状数组或线段树)的更多相关文章
- bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1384 Solved: 629[Submit][Stat ...
- [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】
题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...
- [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】
题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...
- POJ 1195 Mobile phones (二维树状数组或线段树)
偶然发现这题还没A掉............速速解决了............. 树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用......... #include < ...
- 【BZOJ3196】二逼平衡树(树状数组,线段树)
[BZOJ3196]二逼平衡树(树状数组,线段树) 题面 BZOJ题面 题解 如果不存在区间修改操作: 搞一个权值线段树 区间第K大--->直接在线段树上二分 某个数第几大--->查询一下 ...
- BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)
题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...
- P3157 [CQOI2011]动态逆序对(树状数组套线段树)
P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树
题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
随机推荐
- jquery网址
各种分布图的插件:http://echarts.baidu.com/demo.html
- linux:主机规划和磁盘分割
1>.在linux系统中,每个装置都被装成一个档案来对待: 2>.各硬体装置在linux当中的档案名:SATA介面的硬碟的档案名为/dev/sd[a-d];在linux中,几乎所以的硬体装 ...
- 转:python webdriver API 之鼠标事件
前面例子中我们已经学习到可以用 click()来模拟鼠标的单击操作,而我们在实际的 web 产品测试中 发现,有关鼠标的操作,不单单只有单击,有时候还要和到右击,双击,拖动等操作,这些操作包含在Act ...
- C++builder 图像字符流的存储和加载
__fastcall TForm6::TForm6(TComponent* Owner) : TForm(Owner) { #if 1 //for debug AllocConsole(); Atta ...
- 【你吐吧c#每日学习】10.30 C#Nullable Types
分两种类型,value type and reference type. By default, value type owns a default value. For integer, the d ...
- .NET: WPF Binding对数据的校验和转换以及多路Binding
一.校验 一般需要对target上的值进行校验. xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns=" ...
- .net 反编译工具
遇到一个需求,做一个专门访问自己网站的浏览器给用户使用,这个浏览器提供登录功能.此时是一个安装在客户端的exe程序,做登录验证要用到webservice,不能将验证逻辑写入exe中,否则客户端可以利用 ...
- logstash配合filebeat监控tomcat日志
环境:logstash版本:5.0.1&&filebeat 5.0.1 ABC为三台服务器.保证彼此tcp能够相互连接. Index服务器A - 接收BC两台服务器的tomcat日志 ...
- MFC 进度条控件
1.进度条 主要用来进行数据读写.文件拷贝和磁盘格式等操作时的工作进度提示情况,如安装程序等,伴随工作进度的进展,进度条的矩形区域从左到右利用当前活动窗口标题条的颜色来不断填充. 2.进度条控制在MF ...
- Angularjs之directive指令学习笔记(二)
1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...