以前的树状数组更改从前往后,累计从后往前,这题却反过来。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int sz = 100005;
int a[sz],n;
int lower_bit(int t){
return t&-t;
}
void add(int q,int v){
while(q > 0){
a[q] += v;
q -= lower_bit(q);
}
}
int summ(int q){
int res = 0;
while(q <= n){
res += a[q];
q += lower_bit(q);
}
return res;
}
int main(){
int x,y;
while(cin>>n&&n){
memset(a,0,sizeof(a));
for(int i = 0;i<n;++i){
cin>>x>>y;
add(y,1);
add(x-1,-1);
}
int ok = 0;
for(int i = 1;i<=n;++i){
if(ok == 0)
ok = 1;
else
cout<<" ";
cout<<summ(i);
}
cout<<endl;
}
return 0;
}