poj 2352 & Ural 1028 数星星 题解

一道水题,由于x坐标递增y坐标也递增于是前缀和统计即可,用树状数组实现。

#include<bits/stdc++.h>
using namespace std;
const int maxn=15010;
const int maxx=32010;
inline long long read(){
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
long long n,c[maxx],ans[maxn];
long long lowbit(long long x){
	return x&(-x);
}
void add(long long x){
	for(x;x<=maxx;x+=lowbit(x)) c[x]+=1;
}
long long ask(long long x){
	long long tmp=0;
	for(;x;x-=lowbit(x)) tmp+=c[x];
	return tmp;
}
int main(){
	n=read();
	for(long long i=1;i<=n;++i){
		long long x,y;
		x=read();y=read();
		long long s=ask(x+1);
		add(x+1);
		ans[s]++;
	}
	for(long long i=0;i<n;++i){
		printf("%lld
",ans[i]);
	}
	
	return 0;
}
/*
5 
1 1
5 1
7 1
3 3
5 5
*/
原文地址:https://www.cnblogs.com/donkey2603089141/p/11415476.html