您的位置:首页 > 其它

hdu 1556 Color the ball(线段树 区间更新单点查询)

2015-10-05 16:25 661 查看


Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13341    Accepted Submission(s): 6697


Problem Description

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

 

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。

当N = 0,输入结束。

 

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。

 

Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

 

Sample Output

1 1 1
3 2 1

 

Author

8600

 

题目跟这个差不多 http://acm.nyist.net/JudgeOnline/problem.php?pid=123

对需要更新的区间进行更新 而不需要更新到叶子节点  然后查询的时候从上至下 把经过的节点上的值相加

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}

void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}

struct node
{
int l,r;
int num;
};
node no[MAXN*4];

void build(int l,int r,int k)
{
if(l==r)
{
no[k].l=l;
no[k].r=r;
no[k].num=0;
return;
}
int mid=(l+r)/2;
no[k].l=l;
no[k].r=r;
no[k].num=0;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}

void update(int l,int r,int k)
{
if(l<=no[k].l&&r>=no[k].r)
{
no[k].num++;
return;
}
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) update(l,r,k*2);
else if(l>mid) update(l,r,k*2+1);
else
{
update(l,mid,2*k);
update(mid+1,r,k*2+1);
}
}

int ans;
void query(int pos,int k)
{
ans+=no[k].num;
if(no[k].l==no[k].r&&no[k].l==pos)
{
return;
}
int mid=(no[k].l+no[k].r)/2;
if(pos<=mid) query(pos,k*2);
else query(pos,k*2+1);
}

int main()
{
// fread;
int n;
while(scanf("%d",&n)&&n)
{
build(1,n,1);
for(int i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
update(l,r,1);
}
for(int i=1;i<=n;i++)
{
if(i>1)
printf(" ");
ans=0;
query(i,1);
printf("%d",ans);
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: