链接:戳这里
Color the ball
Total Submission(s): 15159 Accepted Submission(s): 7546
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
练练手啦
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int sum[500100],lazy[500100];
void build(int root,int l,int r){
if(l==r){
sum[l]=0;
return ;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
sum[root]=sum[root*2]+sum[root*2+1];
}
void pushdown(int root,int l){
if(lazy[root]){
lazy[root*2]+=lazy[root];
lazy[root*2+1]+=lazy[root];
sum[root*2]+=(l-l/2)*lazy[root];
sum[root*2+1]+=(l/2)*lazy[root];
lazy[root]=0;
}
}
void update(int root,int l,int r,int x,int y,int z){
if(l>=x && r<=y){
sum[root]+=(r-l+1)*z;
lazy[root]+=z;
return ;
}
pushdown(root,r-l+1);
int mid=(l+r)/2;
if(y<=mid) update(root*2,l,mid,x,y,z);
else if(x>mid) update(root*2+1,mid+1,r,x,y,z);
else {
update(root*2,l,mid,x,mid,z);
update(root*2+1,mid+1,r,mid+1,y,z);
}
sum[root]=sum[root*2]+sum[root*2+1];
}
int query(int root,int l,int r,int x,int y){
if(l>=x && r<=y){
return sum[root];
}
pushdown(root,r-l+1);
int mid=(l+r)/2;
if(y<=mid) return query(root*2,l,mid,x,y);
else if(x>mid) return query(root*2+1,mid+1,r,x,y);
else {
return query(root*2,l,mid,x,mid)+query(root*2+1,mid+1,r,mid+1,y);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n==0) break;
mst(sum,0);
mst(lazy,0);
build(1,1,n);
for(int i=1;i<=n;i++){
int l,r;
scanf("%d%d",&l,&r);
update(1,1,n,l,r,1);
}
for(int i=1;i<=n;i++){
if(i!=1) cout<<" ";
printf("%d",query(1,1,n,i,i));
}
cout<<endl;
}
return 0;
}