题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
题意:区间更新,单点查询。IUPQ模型(Interval Update Point Query 段更新,点求值)。
题解:树状数组变形。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=100000+10;
typedef long long LL;
LL c[MAX];
int N;
int lowbit(int x)
{
return x&(-x);
}
//查询【1,x】区间和
LL query(int x)
{
LL s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
//单点更新,对节点x加data
void update(int x,int data)
{
while(x<=N)
{
c[x]+=data;
x+=lowbit(x);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&N)&&N)
{
memset(c,0,sizeof(c));
//如果有初始值,初始化c为初始值即可
for(int i=1;i<=N;i++)
{
int a,b;
scanf("%d%d",&a,&b);
update(a,1);
update(b+1,-1);
}
for(int i=1;i<=N;i++)
{
printf("%lld%c",query(i),i==N?'\n':' ');
}
}
return 0;
}