Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

树状数组AC

Posted by sunjianzhao at 2020-02-27 22:14:29 on Problem 2352
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=32005;
int c[maxn]={0};
struct node {
	int x,y;
}a[maxn];
int l[15000]={0};
int p[15000];
int cmp(struct node a1,struct node a2){
	if(a1.x==a2.x) return a1.y<a2.y;
	return a1.x<a2.x;
}
void Modify(int x){
	for(int i=x;i<maxn;i+=(i&(-i)))
		c[i]++;
}
int Sum(int x){
	int sum=0;
	for(int i=x;i;i-=(i&(-i)))
		sum+=c[i];
	return sum;
}
int n;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp);
	int k=0,px;
	for(int i=1;i<=n;i++){
	//	cout<<a[i].x<<" "<<Sum(a[i].y+1)<<endl;
		l[Sum(a[i].y+1)]++;
		Modify(a[i].y+1);
	}
	for(int i=0;i<n;i++) cout<<l[i]<<endl;
	return 0; 
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator