Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1515 Accepted Submission(s): 635
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,输入结束。
当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
#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int left,right;
int sum,lnc;
};
node tree[500000];
int ans[100050];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].sum=0,tree[id].lnc=0;
if(l!=r)
{
int mid=(l+r)/2;
buildtree(2*id,l,mid);
buildtree(2*id+1,mid+1,r);
}
}
void add(int id,int l,int r,int val)
{
if(tree[id].left==l&&tree[id].right==r)
{
tree[id].lnc+=val;
return ;
}
tree[id].sum+=val*(r-l+1);
int mid=(tree[id].left+tree[id].right)/2;
if(r<=mid) add(2*id,l,r,val);
else if(l>=mid+1) add(2*id+1,l,r,val);
else
{
add(2*id,l,mid,val);
add(2*id+1,mid+1,r,val);
}
}
int query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
return tree[id].sum+tree[id].lnc*(r-l+1);
}
tree[id].sum+=tree[id].lnc*(tree[id].right-tree[id].left+1);
int val=tree[id].lnc,mid=(tree[id].left+tree[id].right)/2;
add(2*id,tree[id].left,mid,val);
add(2*id+1,mid+1,tree[id].right,val);
tree[id].lnc=0;
if(r<=(tree[id].left+tree[id].right)/2) return query(2*id,l,r);
else if(l>=(tree[id].left+tree[id].right)/2+1) return query(2*id+1,l,r);
else return query(2*id,l,(tree[id].left+tree[id].right)/2)+
query(2*id+1,(tree[id].left+tree[id].right)/2+1,r);
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
buildtree(1,1,n);
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(1,x,y,1);
}
for(int i=1;i<=n;i++)
{
ans[i]=query(1,i,i);
}
printf("%d",ans[1]);
for(int i=2;i<=n;i++) printf(" %d",ans[i]);printf("/n");
}
return 0;
}
#include<cstdio>
using namespace std;
struct node
{
int left,right;
int sum,lnc;
};
node tree[500000];
int ans[100050];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].sum=0,tree[id].lnc=0;
if(l!=r)
{
int mid=(l+r)/2;
buildtree(2*id,l,mid);
buildtree(2*id+1,mid+1,r);
}
}
void add(int id,int l,int r,int val)
{
if(tree[id].left==l&&tree[id].right==r)
{
tree[id].lnc+=val;
return ;
}
tree[id].sum+=val*(r-l+1);
int mid=(tree[id].left+tree[id].right)/2;
if(r<=mid) add(2*id,l,r,val);
else if(l>=mid+1) add(2*id+1,l,r,val);
else
{
add(2*id,l,mid,val);
add(2*id+1,mid+1,r,val);
}
}
int query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
return tree[id].sum+tree[id].lnc*(r-l+1);
}
tree[id].sum+=tree[id].lnc*(tree[id].right-tree[id].left+1);
int val=tree[id].lnc,mid=(tree[id].left+tree[id].right)/2;
add(2*id,tree[id].left,mid,val);
add(2*id+1,mid+1,tree[id].right,val);
tree[id].lnc=0;
if(r<=(tree[id].left+tree[id].right)/2) return query(2*id,l,r);
else if(l>=(tree[id].left+tree[id].right)/2+1) return query(2*id+1,l,r);
else return query(2*id,l,(tree[id].left+tree[id].right)/2)+
query(2*id+1,(tree[id].left+tree[id].right)/2+1,r);
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
buildtree(1,1,n);
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(1,x,y,1);
}
for(int i=1;i<=n;i++)
{
ans[i]=query(1,i,i);
}
printf("%d",ans[1]);
for(int i=2;i<=n;i++) printf(" %d",ans[i]);printf("/n");
}
return 0;
}
本来这题属于线段树的,但是用了些技巧就不用了,就是记录每段更新区间的首尾两个值的变化,(首)加一,(尾+1)减一;最后某点颜色的修改次数就是该点及该点之前所有值的累加。实践证明,前置加比后置加快。
#include <iostream>
int f[100010];
int main() {
int n;
int i;
while ( scanf("%d",&n) , n ) {
memset(f,0,(n+1)*sizeof(int));
for ( i = 0 ; i < n ; ++i ) {
int a,b;
scanf("%d%d",&a,&b);
++f[a];
--f[b+1];
}
int m = 0;
for ( i = 1 ; i < n ; ++i ) {
m += f[i];
printf("%d ",m);
}
m += f[i];
printf("%d/n",m);
}
return 0;
}
int n;
int i;
while ( scanf("%d",&n) , n ) {
memset(f,0,(n+1)*sizeof(int));
for ( i = 0 ; i < n ; ++i ) {
int a,b;
scanf("%d%d",&a,&b);
++f[a];
--f[b+1];
}
int m = 0;
for ( i = 1 ; i < n ; ++i ) {
m += f[i];
printf("%d ",m);
}
m += f[i];
printf("%d/n",m);
}
return 0;
}