标签:com struct poj which int space like tar long
Description
Input
Output
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
这题的做法和star那道题差点儿相同,先按x坐标进行升序排列,然后x同样的取对y进行降序排列,然后每次循环推断当前线段和上一条线段是不是x。y都一样,假设一样就直接等于上一条算出的值。不等于就计算。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 100006 struct node{ int x,y,id,num; }a[maxn]; int b[maxn]; bool cmp(node a,node b){ if(a.x==b.x)return a.y>b.y; return a.x<b.x; } bool cmp1(node a,node b){ return a.id<b.id; } int lowbit(int x){ return x&(-x); } void update(int pos,int num){ while(pos<=maxn){ b[pos]+=num;pos+=lowbit(pos); } } int getsum(int pos) { int num=0; while(pos>0){ num+=b[pos];pos-=lowbit(pos); } return num; } int main() { int n,m,i,j,t; while(scanf("%d",&n)!=EOF && n!=0) { memset(a,0,sizeof(a)); for(i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); a[i].x++;a[i].y++; a[i].id=i; } memset(b,0,sizeof(b)); sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++){ if(a[i].x==a[i-1].x && a[i].y==a[i-1].y){ a[i].num=a[i-1].num; } else{ a[i].num=getsum(maxn)-getsum(a[i].y-1); } update(a[i].y,1); } sort(a+1,a+1+n,cmp1); for(i=1;i<=n;i++){ if(i==n)printf("%d\n",a[i].num); else printf("%d ",a[i].num); } } return 0; }
标签:com struct poj which int space like tar long
原文地址:http://www.cnblogs.com/cxchanpin/p/6869573.html