Stars
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34539 | Accepted: 15053 |
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.
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
题意:给出平面上一些点,每个点的level值为其左下角的点的个数(包括正下和左侧)。输出level值在[0,n-1] 的点各有多少个
因为是按照y坐标顺序给出的,所以讲x插入treap,每次统计x左边有多少个就可以了
树状数组:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=32010;
int ans[maxn];
int N;
int sum[maxn];
void add(int x,int val)
{
while(x<maxn)
{
sum[x]+=val;
x+=(x&(-x));
}
}
int getsum(int x)
{
int res=0;
while(x)
{
res+=sum[x];
x-=(x&(-x));
}
return res;
}
int main()
{
while(scanf("%d",&N)!=EOF)
{
memset(ans,0,sizeof(ans));
memset(sum,0,sizeof(sum));
for(int i=1;i<=N;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x++;
ans[getsum(x)]++;
add(x,1);
}
for(int i=0;i<N;i++)printf("%d\n",ans[i]);
}
return 0;
}
treap:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=32010;
int N;
int root,tot;
struct Node
{
int ch[2];
int r;//优先级
int v;//值
int s;
int cnt;//自身重复次数
void init(int val){v=val;ch[0]=ch[1]=0;s=cnt=1;r=rand();}
int cmp(int x)const
{
if(x==v)return -1;
return x<v?0:1;
}
}tree[maxn];
void maintain(int x)
{
tree[x].s=tree[x].cnt;
tree[x].s+=tree[tree[x].ch[0]].s+tree[tree[x].ch[1]].s;
}
void rotate(int &o,int d)
{
int k=tree[o].ch[d^1];
tree[o].ch[d^1]=tree[k].ch[d];
tree[k].ch[d]=o;
maintain(o);
maintain(k);
o=k;
}
void insert(int &o,int x)
{
if(!o)
{
o=++tot;
tree[o].init(x);
}
else
{
if(x==tree[o].v)tree[o].cnt++;
else
{
int d=(x<tree[o].v?0:1);
insert(tree[o].ch[d],x);
if(tree[tree[o].ch[d]].r>tree[o].r)
rotate(o,d^1);
}
}
maintain(o);
}
void remove(int &o,int x)
{
if(!o)return;
int d=tree[o].cmp(x);
if(d==-1)
{
int u=o;
if(tree[o].ch[0]&&tree[o].ch[1])
{
int d2=(tree[tree[o].ch[0]].r>tree[tree[o].ch[1]].r?1:0);
rotate(o,d2);
remove(tree[o].ch[d2],x);
}
else
{
if(!tree[o].ch[0])o=tree[o].ch[1];
else o=tree[o].ch[0];
}
}
else remove(tree[o].ch[d],x);
if(o)maintain(o);
}
//返回最大值
int get_max(int o)
{
while(tree[o].ch[0])o=tree[o].ch[0];
return tree[o].v;
}
//返回最小值
int get_min(int o)
{
while(tree[o].ch[1])o=tree[o].ch[1];
return tree[o].v;
}
//返回val的前驱,如果没有的话返回y
//y的初值可赋成0,表示没有前驱
int get_pred(int o,int val,int y)
{
if(!o)return y;
if(tree[o].v<=val)//注意大于等于号
return get_pred(tree[o].ch[1],val,tree[o].v);
else return get_pred(tree[o].ch[0],val,y);
}
//返回val的后继,如果没有的话返回y
//y的初值可赋成0,表示没有后继
int get_succ(int o,int val,int y)
{
if(!o)return y;
if(tree[o].v>=val)return get_succ(tree[o].ch[0],val,tree[o].v);
else return get_succ(tree[o].ch[1],val,y);
}
//返回第k大的元素的值
int get_kth(int o,int k)
{
if(k<tree[tree[o].ch[0]].s+1)return get_kth(tree[o].ch[0],k);
else if(k>tree[tree[o].ch[0]].s+tree[o].cnt)
return get_kth(tree[o].ch[1],k-tree[tree[o].ch[0]].s-tree[o].cnt);
return tree[o].v;
}
//返回val的排名
int get_rank(int o,int val)
{
if(!o)return 0;
int lsize=tree[tree[o].ch[0]].s;
if(val<tree[o].v)
return get_rank(tree[o].ch[0],val);
else if(val>tree[o].v)
return get_rank(tree[o].ch[1],val)+lsize+tree[o].cnt;
return lsize+tree[o].cnt;
}
void debug(int r)
{
if(!r)return;
debug(tree[r].ch[0]);
printf("%d&&",tree[r].v);
debug(tree[r].ch[1]);
}
int ans[maxn];
int main()
{
while(scanf("%d",&N)!=EOF)
{
memset(ans,0,sizeof(ans));
root=tot=0;
for(int i=1;i<=N;i++)
{
int x,y;
scanf("%d%d",&x,&y);
insert(root,x);
int r=get_rank(root,x);
ans[r-1]++;
}
for(int i=0;i<N;i++)printf("%d\n",ans[i]);
}
return 0;
}