注意输入有0出现,BIT不好处理,所以在输入的时候加1
// 树状数组
// http://blog.youkuaiyun.com/niushuai666/article/details/7389273
#include <cstdio>
#include <string.h>
#define MAX 32000 + 10
using namespace std;
typedef long long LL;
LL c[MAX];
LL ans[MAX];
LL lowbit( LL i ) {
return i & ( -i );
}
void update( LL i, LL val, LL n ) {
while( i <= n ) {
c[i] = c[i] + val;
i = i + lowbit( i );
}
}
LL getSum( LL i ) {
LL ans = 0;
while( i > 0 ) {
ans = ans + c[i];
i = i - lowbit( i );
}
return ans;
}
int main() {
LL n;
while( scanf( "%lld", &n ) != EOF ) {
memset( c, 0, sizeof( c ) );
memset( ans, 0, sizeof( ans ) );
LL x, y;
for( LL i = 1; i <= n; i++ ) {
scanf( "%lld%lld", &x, &y );
x++; //有0出现,树状数组无法处理。故+1
ans[getSum( x )]++; //先统计,不包括本身
update( x, 1, MAX ); //加入
}
for( LL i = 0; i < n; i++ ) {
printf( "%lld\n", ans[i] );
}
}
return 0;
}

本文介绍了一种使用树状数组处理包含0的输入的方法。为了避免树状数组处理0时的困难,在输入过程中将所有数值加1,并通过更新操作维护树状数组的状态。文章提供了完整的C++代码实现,演示了如何统计特定前缀和出现的次数。
437

被折叠的 条评论
为什么被折叠?



