zoukankan      html  css  js  c++  java
  • HDU 1556Color the ball

    题意:线段树水题,区间更新,带上lazy标记即可;

    #include<iostream>
    #include<queue>
    #include<vector>
    #include<map>
    #include<set>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<cstdio>
    #define N 100005
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef struct node{
    	int x;int y;int date;
    }node;
    typedef struct list{
    	int x;int y;
    }list;
    node a[4*N];
    list c[N];
    int b[N];
    void built(int root,int first,int end){
    	if(first==end){
    		a[root].x=first;a[root].y=end;a[root].date=0;
    		return ;
    	}
    	int mid=(first+end)/2;
    	built(root*2,first,mid);
    	built(root*2+1,mid+1,end);
    	a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=0;
    }
    void U(int root,int first,int end,int l,int r){
    	if(l<=first&&end<=r){
    		a[root].date++;
    		return ;
    	}
    	int mid=(first+end)/2;
    	if(a[root].date!=0){
    		a[root*2].date+=a[root].date;
    		a[root*2+1].date+=a[root].date;
    		a[root].date=0;
    	}
    	if(l<=mid)  U(2*root,first,mid,l,r);
    	if(r>mid)   U(2*root+1,mid+1,end,l,r);
    }
    void Q(int root,int first,int end){
    	if(first==end){
    		b[first]=a[root].date;
    		return ;
    	}
    	if(a[root].date!=0){
    		a[root*2].date+=a[root].date;
    		a[root*2+1].date+=a[root].date;
    		a[root].date=0;
    	}
    	int mid=(first+end)/2;
    	Q(2*root,first,mid);
    	Q(2*root+1,mid+1,end);
    }
    int main(){
    	int n;
    	while(scanf("%d",&n)==1&&n!=0){
    		memset(b,0,sizeof(b));
    		for(int i=1;i<=n;i++){
    			scanf("%d %d",&c[i].x,&c[i].y);
    		}
    		built(1,1,n);
    		for(int i=1;i<=n;i++){
    			U(1,1,n,c[i].x,c[i].y);
    		}
    		Q(1,1,n);
    		for(int i=1;i<=n;i++){
    			if(i==1){
    				printf("%d",b[i]);
    			}
    			else{
    				printf(" %d",b[i]);
    			}
    		}
    		printf("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Javascript跨域后台设置拦截
    Hello ReactJS
    Redis 常用监控信息命令总结
    MySQL架构与业务总结图
    MySQL垂直拆分和水平拆分的优缺点和共同点总结
    MySQL实用工具汇总
    MySQL查看数据库表容量大小
    MySQL到底能支持多大的数据量?
    微信小程序wxss的background本地图片问题
    微信小程序中显示与隐藏(hidden)
  • 原文地址:https://www.cnblogs.com/wang9897/p/7624401.html
Copyright © 2011-2022 走看看