Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 50647 | Accepted: 21833 |
Description

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
Output
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
题意:在一个坐标系中输入n个点。求每个点在(0,0)与当前点成对角线的矩形内,包含多少点(包括边界但不包括当前点)。输出n行,分别输出包含点数为i的点的数目(i∈[0,n-1])。输入保证y坐标升序排列。
思路:题目中由于y坐标升序排列,那么某一个点的星星数就一定等于它之前输入的,x坐标小于等于当前点的数目。
上图中,由于y坐标保证升序排列,点的输入顺序一定为1,2,3。3号点的星星数就是3号点之前输入的点里,x坐标小于3号点的点的数目,即为1。
由此,问题与y无关。每输入一个点,先查询x在[0,当前点x坐标]内的点总数,此即为当前点的星星数,使用一个num数组记录星星数为i的点的总数,完成查询后,num[查询的结果]++。之后再使位置为:当前点x坐标的点的总数+1。两个操作即为线段树的区间查询,单点更新。
由于初始时所有区间内的点数都为0,那么就不用建树辣。
代码:
线段树:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
typedef long long ll;
using namespace std;
int tree[200010];
int num[15010];
void change(int p , int l , int r , int x){
if(l == r){
tree[p]++;
return;
}
int mid = (l+r)>>1;
if(x<=mid){
change(p<<1 , l , mid , x );
}
else{
change(p<<1|1 , mid+1 , r , x);
}
tree[p] = tree[p<<1] + tree[p<<1|1];
}
int find(int p , int l , int r , int x , int y){
if(x<=l && r<=y)
return tree[p];
int mid = (l+r)>>1;
if(y <= mid){
return find(p<<1 , l , mid , x , y);
}
if(x > mid){
return find(p<<1|1 , mid+1 , r , x , y);
}
return ( find(p<<1 , l , mid , x , mid)+find(p<<1|1 , mid+1 , r , mid+1, y) );
}
int main(){
int n;
int x,y;
scanf("%d",&n);
for(int i = 0 ; i<n ; i++){
scanf("%d %d",&x,&y);
int t = find(1,1,32001,1,x+1);
num[t]++;
change(1,1,32001,x+1);
}
for(int i = 0 ; i<n ; i++){
printf("%d\n",num[i]);
}
return 0;
}
树状数组:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeace
typedef long long ll;
using namespace std;
const int maxn = 1e5+10;
int n;
int C[maxn];
int k[maxn];
int lowbit(int x){
return x&(-x);
}
int getsum(int x){
int res = 0;
for( ; x ; x-=lowbit(x)){
res += C[x];
}
return res;
}
void change(int x , int c){
for( ; x<maxn ; x += x&(-x)){
C[x] += c;
}
}
int main(){
#ifdef Reast1nPeace
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
int x,y;
cin>>n;
for(int i = 0 ; i<n ; i++){
cin>>x>>y;
int t = getsum(x+1);
change(x+1,1);
k[t]++;
}
for(int i = 0 ; i<n ; i++){
cout<<k[i]<<endl;
}
return 0;
}