poj 2352 & Ural 1028 the number of stars explanations

A water problem, because the x-coordinate y-coordinate increment increment so prefix and statistics also can be achieved by Fenwick tree.

#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\n",ans[i]);
    }
    
    return 0;
}
/*
5 
1 1
5 1
7 1
3 3
5 5
*/

Guess you like

Origin www.cnblogs.com/donkey2603089141/p/11415476.html