Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19188 Accepted Submission(s): 9585
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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M 5000000
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
struct Tree
{
int l,r,len,lazy,sum;
};
Tree tree[M<<2];
int n;
void up(int o)
{
tree[o].sum=tree[ll].sum+tree[rr].sum;
}
void down(int o)
{
if(tree[o].lazy)
{
tree[ll].lazy+=tree[o].lazy;
tree[rr].lazy+=tree[o].lazy;
tree[rr].sum+=tree[o].lazy*tree[rr].len;
tree[ll].sum+=tree[o].lazy*tree[ll].len;
tree[o].lazy=0;
}
}
void build(int o,int l,int r)
{
tree[o].l=l;tree[o].r=r;
tree[o].len=r-l+1;tree[o].lazy=0;
if(l==r)
{
tree[o].sum=0;
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
up(o);
}
void update(int o,int l,int r,int val)
{
if(tree[o].l>=l&&tree[o].r<=r)
{
tree[o].lazy+=val;
tree[o].sum+=tree[o].len*val;
return;
}
down(o);
int mid=(tree[o].l+tree[o].r)>>1;
if(r<=mid) update(ll,l,r,val);
else if(l>mid) update(rr,l,r,val);
else
{
update(ll,l,mid,val);
update(rr,mid+1,r,val);
}
up(o);
}
int query(int o,int l,int r)
{
if(tree[o].l>=l&&tree[o].r<=r)
return tree[o].sum;
down(o);
int mid=(tree[o].l+tree[o].r)>>1;
if(r<=mid) query(ll,l,r);
else if(l>mid) query(rr,l,r);
else return query(ll,l,mid)+query(rr,mid+1,r);
}
int main()
{
while(scanf("%d",&n)&&n)
{
build(1,1,n);int k=n;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
update(1,a,b,1);
}
int i,j;
printf("%d",query(1,1,1));
for(i=2;i<=n;i++)
printf(" %d",query(1,i,i));
putchar('\n');
}
return 0;
}
线段树 不用结构体的模板
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 100000+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
int sum[MAXN<<2];
int add[MAXN<<2];
void Up(int o){
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void Down(int o,int m){
if(add[o]){
add[o<<1]+=add[o];
add[o<<1|1]+=add[o];
sum[o<<1]+=add[o]*(m-(m>>1));
sum[o<<1|1]+=add[o]*(m>>1);
add[o]=0;
}
}
void Build(int o,int le,int ri){
add[o]=0;
if(le==ri){
//scanf("%d",&sum[o]);
sum[o]=0;
return ;
}
int mid=(le+ri)>>1;
Build(o<<1,le,mid);
Build(o<<1|1,mid+1,ri);
Up(o);
}
void UpDate(int o,int le,int ri,int L,int R,int val){
if(L<=le&&ri<=R){
add[o]+=val;
sum[o]+=val*(ri-le+1);
return ;
}
Down(o,ri-le+1);
int mid=(ri+le)>>1;
if(L<=mid) UpDate(o<<1,le,mid,L,R,val);
if(R>mid) UpDate(o<<1|1,mid+1,ri,L,R,val);
Up(o);
}
int Query(int o,int le,int ri,int L,int R){
if(L<=le&&ri<=R){
return sum[o];
}
Down(o,ri-le+1);
int mid=(le+ri)>>1;
int res=0;
if(L<=mid) res+=Query(o<<1,le,mid,L,R);
if(R>mid) res+=Query(o<<1|1,mid+1,ri,L,R);
return res;
}
int main(){
CLOSE();
// fread();
// fwrite();
int n;
while(scanf("%d",&n)&&n){
Build(1,1,n);
int m=n;
while(m--){
int a,b;
scanf("%d%d",&a,&b);
UpDate(1,1,n,a,b,1);
}
for(int i=1;i<=n;i++)
printf("%d%s",Query(1,1,n,i,i),i==n?"":" ");
puts("");
}
return 0;
}