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;
    }


  • 相关阅读:
    观察者设计模式
    JSP中用jsp:param传递中文参数出现乱码
    使用.msi进行安装mysql程序(超详细)
    扒来的lstdc++.6.0.9有效解决方案
    HTML响应状态码
    砸壳
    ipv6
    犀利的文章
    安装ReactNative开发IDE
    创建ReactNative的iOS项目
  • 原文地址:https://www.cnblogs.com/wang9897/p/7624401.html
Copyright © 2011-2022 走看看