点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1556
蓝桥杯时看到树状数组,于是就学习了下,没什么好解释的
/* *********************************************** Author :小蔡虎 Created Time :2016/ File Name :E:\2016ACM\HDU 状态 : ************************************************ */ #include<stdio.h> #include<string.h> #include<string> #include<algorithm> #include<math.h> #include<iostream> #include<time.h> #define PI 3.141592654 #define MA 100010 using namespace std; /* */ int tr[MA],n; int getsum(int x) { int sum=0; while(x<=n) { sum+=tr[x]; x+=x&(-x); } return sum; } void updata(int x,int mun) { while(x>0) { tr[x]+=mun; x-=x&(-x); } } int main() { int a,b,i; while(scanf("%d",&n),n) { memset(tr,0,sizeof(tr)); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); updata(a-1,-1); updata(b,1); } for(i=1;i<n;i++) { printf("%d ",getsum(i)); } printf("%d",getsum(n)); printf("\n"); } return 0; }