Stars(树状数组+线段树)
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6676 Accepted Submission(s): 2659
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.
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 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.
1 1
5 1
7 1
3 3
5 5
2
1
1
0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
/*struct Node{
int x,y;
};*/
int tree[MAXM+],ans[MAXN];//,die[MAXN];
//Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x>){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
/*int erfen(int l,int r,int x){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(die[mid]>x)r=mid-1;
else l=mid+1;
}
return l;
}*/
int main(){
/* #ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif*/
int N;
int a;
while(~scanf("%d",&N)){
memset(tree,,sizeof(tree));
memset(ans,,sizeof(ans));
for(int i=;i<N;i++){
scanf("%d%*d",&a);
a++;
ans[SUM(a)]++;update(a);
// scanf("%d%d",&dt[i].x,&dt[i].y);
}
//sort(dt,dt+N,cmp);
/* for(int i=0;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=0;i<N;i++){
int temp=0,t=erfen(0,N-1,i);
// printf("%d**%d**%d\n",die[t-1],i,t);
temp=t;
ans[i]=temp;
// printf("%d\n",ans[i]);
}*/
for(int i=;i<N;i++){
/* if(i){
printf(" ");
}*/
// printf("%d",i==0?ans[i]:ans[i]-ans[i-1]);
printf("%d\n",ans[i]);
} }
return ;
}
刚开始写的也ac了:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
struct Node{
int x,y;
};
int tree[MAXM+],die[MAXN],ans[MAXN];
Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++){
scanf("%d%d",&dt[i].x,&dt[i].y);
dt[i].x++;
}
//sort(dt,dt+N,cmp);
memset(tree,,sizeof(tree));
for(int i=;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=,j=;i<N;i++){
int temp=;
while(die[j]==i){
j++;temp++;
}
ans[i]=temp;
}
for(int i=;i<N;i++){
//if(i)printf(" ");
printf("%d\n",ans[i]);
}
// puts("");
}
return ;
}
刚开始发现超时改成二分找了,更快点的最后也ac了:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
struct Node{
int x,y;
};
int tree[MAXM+],die[MAXN],ans[MAXN];
Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
int erfen(int l,int r,int x){
int mid;
while(l<=r){
mid=(l+r)>>;
if(die[mid]>x)r=mid-;
else l=mid+;
}
return l;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++){
scanf("%d%d",&dt[i].x,&dt[i].y);
dt[i].x++;
}
//sort(dt,dt+N,cmp);
memset(tree,,sizeof(tree));
for(int i=;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=;i<N;i++){
int temp=,t=erfen(,N-,i);
// printf("%d**%d**%d\n",die[t-1],i,t);
temp=t;
ans[i]=temp;
// printf("%d\n",ans[i]);
}
for(int i=;i<N;i++){
//if(i)printf(" ");
printf("%d\n",i==?ans[i]:ans[i]-ans[i-]);
}
// puts("");
}
return ;
}
线段树来一发:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
#define L tree[root].l
#define R tree[root].r
#define S tree[root].sum
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r struct Node{
int l,r,sum;
};
Node tree[MAXN<<];
int res[MAXN>>];
int ans;
void build(int root,int l,int r){
L=l;R=r;
S=;
if(l==r)return;
int mid=(l+r)>>;
build(lson);
build(rson);
}
void update(int root,int x){
if(L==x&&R==x){
S++;
return;
}
int mid=(L+R)>>;
if(mid>=x)update(root<<,x);
else update(root<<|,x);
S=tree[root<<].sum+tree[root<<|].sum;
}
void query(int root,int x){
if(L>=&&R<=x){
ans+=S;
return;
}
int mid=(L+R)>>;
if(mid>=)query(root<<,x);
if(mid<x)query(root<<|,x);
}
int main(){
int N,a;
while(~scanf("%d",&N)){
build(,,MAXN-);
memset(res,,sizeof(res));
for(int i=;i<N;i++){
scanf("%d%*d",&a);
a++;
ans=;
query(,a);
res[ans]++;
update(,a);
}
for(int i=;i<N;i++)printf("%d\n",res[i]);
}
return ;
}
Stars(树状数组+线段树)的更多相关文章
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 数据结构--树状数组&&线段树--基本操作
随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...
- BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...
- BZOJ 3333 排队计划 树状数组+线段树
题目大意:给定一个序列.每次选择一个位置,把这个位置之后全部小于等于这个数的数抽出来,排序,再插回去,求每次操作后的逆序对数 首先我们每一次操作 对于这个位置前面的数 因为排序的数与前面的数位置关系不 ...
- 第十四个目标(dp + 树状数组 + 线段树)
Problem 2236 第十四个目标 Accept: 17 Submit: 35 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- Curious Robin Hood(树状数组+线段树)
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...
随机推荐
- listview 点击条目 自动置顶或者自动置底部
关于Listview点击条目,自动滑动到点击条目实现: map_searchresult_list.post(new Runnable() { @Override public void run() ...
- java面向对象之 封装 Encapsulation
什么是封装:对象中的成员该隐藏的隐藏.该公开的要公开 封装:顾名思义,隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有 ...
- Probability theory
1.Probability mass functions (pmf) and Probability density functions (pdf) pmf 和 pdf 类似,但不同之处在于所适用的分 ...
- American tour(If you have a chance)
去美国旅游,或出国旅游,英语不精没关系,词汇量不多也没关系,但有一些实用的口语一定要学会哟~ 酒店住宿常用英文 I would like to have a morning Call at 8:00 ...
- 解决Qt程序发布时中文乱码问题(通过QApplication.addLibraryPath加载QTextCodec插件)
Qt程序的文字编码,是通过插件来解决的,所以我们发布的时候需要把相应的插件也发布出去,在开发者电脑上程序会自动从插件目录加载到插件,但是如果发布给别的电脑使用,需要手动指定插件路径,如下所示: int ...
- Ubuntu 14.04安装地里编码软件Nominatim过程
一.必须软件: 在Ubuntu系统编译执行Nominatim软件系统必须安装的软件有: 1.GCC 编译器 2.postgresql 数据库 3.proj4 4.geos 5.postgis 6.PH ...
- xcode UIImage图片拉伸
图片拉伸 +(UIImage*)wlisWithImage:(NSString *)name{ //获取图片 UIImage * img=[UIImage imageNamed:name]; //获取 ...
- 内容高度小于窗口高度时版权div固定在底部
<!doctype html><html><head><meta charset="utf-8"><title>文档内容 ...
- [编程题] 最大的LeftMax与rightMax之差绝对值
[编程题] 最大的LeftMax与rightMax之差绝对值 给定一个长度为N的整型数组arr,可以划分成左右两个部分: 左部分arr[0..K],右部分arr[K+1..arr.length-1], ...
- left outer join
table A: Field_K, Field_A 1 a 3 b 4 ...