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 ...
随机推荐
- Apache新版配置虚拟主机的注意事项
1.关于没有默认索引文件(index.php或者index.html)时,列出目录:需要开启模块 LoadModule autoindex_module modules/mod_autoindex.s ...
- 使用Notepad++开发python配置笔记
这是我在python学习过程中,收集整理的一些notepadd++环境配置方法. 1.配置制表符 Notepad++ ->"设置"菜单->"首选项" ...
- 【java】静态方法声明与调用习题
public class dengd { static int getTt(int table[]) { for(int b=0; b<table.length;b++) { System.ou ...
- Oracle单个数据文件超过32G后扩容
Oracle单个数据文件超过32G后扩容 表空间数据文件容量与DB_BLOCK_SIZE的设置有关,而这个参数在创建数据库实例的时候就已经指定.DB_BLOCK_SIZE参数可以设置为4K.8K. ...
- 运行预构建 Linux 映像的 Windows Azure 虚拟机中的交换空间 – 第 1 部分
本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 随着基础结构服务(虚拟机和虚拟网络)近期在 Windows Azure 上正式发布,越来越多的企业工作负荷正在向 ...
- python-操作hive
python访问hive2 HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能.目前使用python的用户可以通过pyhs2这 ...
- City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Hotel(线段树合并)
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14958 Accepted: 6450 Descriptio ...
- c/c++中与字符串处理相关的函数
void *memccpy (void *dest, const void *src, int c, size_t n); 从src所指向的对象复制n个字符到dest所指向的对象中.如果复制过程中遇到 ...
- C++ Primer 读书笔记 第1章
1.1 编写简单的C++程序 每个C++程序都必须包含一个main函数,因为main函数是系统执行入口,且main函数是唯一被系统显示调用的函数. 定义函数必须指定4个元素:返回类型.函数名.形参表. ...