标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6676 Accepted Submission(s): 2659
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 //#define LOCAL 11 const int MAXM=32005; 12 const int MAXN=15010; 13 /*struct Node{ 14 int x,y; 15 };*/ 16 int tree[MAXM+1],ans[MAXN];//,die[MAXN]; 17 //Node dt[MAXN]; 18 /*int cmp(Node a,Node b){ 19 if(a.y!=b.y){ 20 return a.y<b.y; 21 } 22 else return a.x<b.x; 23 }*/ 24 int lowbit(int x){ 25 return x&(-x); 26 } 27 void update(int x){ 28 while(x<=MAXM){ 29 tree[x]++; 30 x+=lowbit(x); 31 } 32 } 33 int SUM(int x){ 34 int temp=0; 35 while(x>0){ 36 temp+=tree[x]; 37 x-=lowbit(x); 38 } 39 return temp; 40 } 41 /*int erfen(int l,int r,int x){ 42 int mid; 43 while(l<=r){ 44 mid=(l+r)>>1; 45 if(die[mid]>x)r=mid-1; 46 else l=mid+1; 47 } 48 return l; 49 }*/ 50 int main(){ 51 /* #ifdef LOCAL 52 freopen("data.in","r",stdin); 53 freopen("data.out","w",stdout); 54 #endif*/ 55 int N; 56 int a; 57 while(~scanf("%d",&N)){ 58 memset(tree,0,sizeof(tree)); 59 memset(ans,0,sizeof(ans)); 60 for(int i=0;i<N;i++){ 61 scanf("%d%*d",&a); 62 a++; 63 ans[SUM(a)]++;update(a); 64 // scanf("%d%d",&dt[i].x,&dt[i].y); 65 } 66 //sort(dt,dt+N,cmp); 67 /* for(int i=0;i<N;i++){ 68 die[i]=SUM(dt[i].x); 69 update(dt[i].x); 70 } 71 sort(die,die+N); 72 for(int i=0;i<N;i++){ 73 int temp=0,t=erfen(0,N-1,i); 74 // printf("%d**%d**%d\n",die[t-1],i,t); 75 temp=t; 76 ans[i]=temp; 77 // printf("%d\n",ans[i]); 78 }*/ 79 for(int i=0;i<N;i++){ 80 /* if(i){ 81 printf(" "); 82 }*/ 83 // printf("%d",i==0?ans[i]:ans[i]-ans[i-1]); 84 printf("%d\n",ans[i]); 85 } 86 87 } 88 return 0; 89 }
刚开始写的也ac了:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 //#define LOCAL 11 const int MAXM=32010; 12 const int MAXN=15010; 13 struct Node{ 14 int x,y; 15 }; 16 int tree[MAXM+1],die[MAXN],ans[MAXN]; 17 Node dt[MAXN]; 18 /*int cmp(Node a,Node b){ 19 if(a.y!=b.y){ 20 return a.y<b.y; 21 } 22 else return a.x<b.x; 23 }*/ 24 int lowbit(int x){ 25 return x&(-x); 26 } 27 void update(int x){ 28 while(x<=MAXM){ 29 tree[x]++; 30 x+=lowbit(x); 31 } 32 } 33 int SUM(int x){ 34 int temp=0; 35 while(x){ 36 temp+=tree[x]; 37 x-=lowbit(x); 38 } 39 return temp; 40 } 41 int main(){ 42 #ifdef LOCAL 43 freopen("data.in","r",stdin); 44 freopen("data.out","w",stdout); 45 #endif 46 int N; 47 while(~scanf("%d",&N)){ 48 for(int i=0;i<N;i++){ 49 scanf("%d%d",&dt[i].x,&dt[i].y); 50 dt[i].x++; 51 } 52 //sort(dt,dt+N,cmp); 53 memset(tree,0,sizeof(tree)); 54 for(int i=0;i<N;i++){ 55 die[i]=SUM(dt[i].x); 56 update(dt[i].x); 57 } 58 sort(die,die+N); 59 for(int i=0,j=0;i<N;i++){ 60 int temp=0; 61 while(die[j]==i){ 62 j++;temp++; 63 } 64 ans[i]=temp; 65 } 66 for(int i=0;i<N;i++){ 67 //if(i)printf(" "); 68 printf("%d\n",ans[i]); 69 } 70 // puts(""); 71 } 72 return 0; 73 }
刚开始发现超时改成二分找了,更快点的最后也ac了:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 //#define LOCAL 11 const int MAXM=32010; 12 const int MAXN=15010; 13 struct Node{ 14 int x,y; 15 }; 16 int tree[MAXM+1],die[MAXN],ans[MAXN]; 17 Node dt[MAXN]; 18 /*int cmp(Node a,Node b){ 19 if(a.y!=b.y){ 20 return a.y<b.y; 21 } 22 else return a.x<b.x; 23 }*/ 24 int lowbit(int x){ 25 return x&(-x); 26 } 27 void update(int x){ 28 while(x<=MAXM){ 29 tree[x]++; 30 x+=lowbit(x); 31 } 32 } 33 int SUM(int x){ 34 int temp=0; 35 while(x){ 36 temp+=tree[x]; 37 x-=lowbit(x); 38 } 39 return temp; 40 } 41 int erfen(int l,int r,int x){ 42 int mid; 43 while(l<=r){ 44 mid=(l+r)>>1; 45 if(die[mid]>x)r=mid-1; 46 else l=mid+1; 47 } 48 return l; 49 } 50 int main(){ 51 #ifdef LOCAL 52 freopen("data.in","r",stdin); 53 freopen("data.out","w",stdout); 54 #endif 55 int N; 56 while(~scanf("%d",&N)){ 57 for(int i=0;i<N;i++){ 58 scanf("%d%d",&dt[i].x,&dt[i].y); 59 dt[i].x++; 60 } 61 //sort(dt,dt+N,cmp); 62 memset(tree,0,sizeof(tree)); 63 for(int i=0;i<N;i++){ 64 die[i]=SUM(dt[i].x); 65 update(dt[i].x); 66 } 67 sort(die,die+N); 68 for(int i=0;i<N;i++){ 69 int temp=0,t=erfen(0,N-1,i); 70 // printf("%d**%d**%d\n",die[t-1],i,t); 71 temp=t; 72 ans[i]=temp; 73 // printf("%d\n",ans[i]); 74 } 75 for(int i=0;i<N;i++){ 76 //if(i)printf(" "); 77 printf("%d\n",i==0?ans[i]:ans[i]-ans[i-1]); 78 } 79 // puts(""); 80 } 81 return 0; 82 }
线段树来一发:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int MAXN=32010; 11 #define L tree[root].l 12 #define R tree[root].r 13 #define S tree[root].sum 14 #define lson root<<1,l,mid 15 #define rson root<<1|1,mid+1,r 16 17 struct Node{ 18 int l,r,sum; 19 }; 20 Node tree[MAXN<<2]; 21 int res[MAXN>>1]; 22 int ans; 23 void build(int root,int l,int r){ 24 L=l;R=r; 25 S=0; 26 if(l==r)return; 27 int mid=(l+r)>>1; 28 build(lson); 29 build(rson); 30 } 31 void update(int root,int x){ 32 if(L==x&&R==x){ 33 S++; 34 return; 35 } 36 int mid=(L+R)>>1; 37 if(mid>=x)update(root<<1,x); 38 else update(root<<1|1,x); 39 S=tree[root<<1].sum+tree[root<<1|1].sum; 40 } 41 void query(int root,int x){ 42 if(L>=1&&R<=x){ 43 ans+=S; 44 return; 45 } 46 int mid=(L+R)>>1; 47 if(mid>=1)query(root<<1,x); 48 if(mid<x)query(root<<1|1,x); 49 } 50 int main(){ 51 int N,a; 52 while(~scanf("%d",&N)){ 53 build(1,1,MAXN-1); 54 memset(res,0,sizeof(res)); 55 for(int i=0;i<N;i++){ 56 scanf("%d%*d",&a); 57 a++; 58 ans=0; 59 query(1,a); 60 res[ans]++; 61 update(1,a); 62 } 63 for(int i=0;i<N;i++)printf("%d\n",res[i]); 64 } 65 return 0; 66 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4900373.html