题目大意:中文题目,就不说了。
这题的具体做法是:对于覆盖区间[a,b],让h[a] = h[a] + 1,h[b + 1] = h[b + 1] - 1。
然后要查询x被覆盖了多少次,h[1]+h[2]+h[3]+......+h[x-1]+h[x]的值就是答案
我的代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int MAXN = 1e5 + 1000;
int h[MAXN];
int n;
int lowbit(int x)
{
return x & (-x);
}
void updata(int i, int val)
{
while (i <= n)
{
h[i] += val;
i += lowbit(i);
}
}
int query(int i)
{
int ans;
ans = 0;
while (i > 0)
{
ans += h[i];
i -= lowbit(i);
}
return ans;
}
int main()
{
int x, y, i, ans;
while (scanf("%d",&n))
{
if (n == 0)
break;
for (i=1; i<=n+10; i++)
h[i] = 0;
for (i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
updata(x, 1);
updata(y+1, -1);
}
for (i=1; i<n; i++)
{
ans = query(i);
printf("%d ", ans);
}
ans = query(n);
printf("%d\n", ans);
}
return 0;
}