【ICPC-429】hdu 1556 Color the ball

点击打开链接hdu1556

 

思路:线段树成段更新
分析:
1 简单的线段树的成段更新,我们把它看成区间的更改和区间的求和即可,那这样我们只要建立好线段树然后每一次进行更新,最后对每一个[i , i]区间进行求和即可

代码:

 


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
struct Node{
    int left;
    int right;
    int mark;//延时标记
    int sum;
};
Node node[4*MAXN];
int N;

//向上更新
void push_up(int pos){
    node[pos].sum = node[pos<<1].sum + node[(pos<<1)+1].sum;
}

//向下更新i
void push_down(int pos){
    if(node[pos].mark){
       node[pos<<1].mark += node[pos].mark;
       node[(pos<<1)+1].mark += node[pos].mark; 
       node[pos<<1].sum += node[pos].mark*(node[pos<<1].right-node[pos<<1].left+1);
       node[(pos<<1)+1].sum += node[pos].mark*(node[(pos<<1)+1].right-node[(pos<<1)+1].left+1);
       node[pos].mark = 0;
    }
}

//建立线段树
void buildTree(int left , int right , int pos){
    node[pos].left = left;
    node[pos].right = right;
    node[pos].mark = 0;
    if(left == right){
      node[pos].sum = 0;
      return;
    }
    int mid = (left+right)>>1;
    buildTree(left , mid , pos<<1);
    buildTree(mid+1 , right , (pos<<1)+1);
    push_up(pos);
}

//更新
void update(int left , int right , int value , int pos){
    if(left <= node[pos].left && right >= node[pos].right){
       node[pos].mark += value;
       node[pos].sum += value*(node[pos].right-node[pos].left+1);
       return;
    }
    push_down(pos);
    int mid = (node[pos].left + node[pos].right)>>1;
    if(right <= mid)
        update(left , right , value , pos<<1);
    else if(left > mid)
        update(left , right , value , (pos<<1)+1);
    else{
        update(left , mid , value , pos<<1);
        update(mid+1 , right , value , (pos<<1)+1);
    }
    push_up(pos);
}

//查询
int query(int left , int right , int pos){
    if(node[pos].left == left && node[pos].right == right)
      return node[pos].sum;
    int mid = (node[pos].left+node[pos].right)>>1;
    push_down(pos);//继续向下更新,这样才能完全更新完毕
    if(right <= mid)
       return query(left , right , pos<<1);
    else if(left > mid) 
       return query(left , right , (pos<<1)+1);
    else
       return query(left , mid , pos<<1) + query(mid+1 , right , (pos<<1)+1);
}

int main(){
    int x , y;
    while(scanf("%d" , &N) && N){
        buildTree(1 , N , 1);
        int cnt = N;
        while(cnt--){
           scanf("%d%d" , &x , &y);
           update(x , y , 1 , 1);
        }
        for(int i = 1 ; i <= N ; i++){
           if(i == 1) printf("%d" , query(i , i , 1));
           else printf(" %d" , query(i , i , 1));
        }   
        printf("\n");
    }
    return 0;
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。所以各大互联网厂商都瞄准移动互联网这个潮流进行各大布局,经过多年的大浪淘沙,各种移动操作系统的不断面世,而目前市场占有率最高的就是微信小程序,本次开发一套基于微信小程序的生签到系统,有管理员,教师,学生三个角色。管理员功能有个人中心,学生管理,教师管理,签到管理,学生签到管理,班课信息管理,加入班课管理,请假信息管理,审批信息管理,销假信息管理,系统管理。教师和学生都可以在微信端注册和登录,教师可以管理签到信息,管理班课信息,审批请假信息,查看学生签到,查看加入班级,查看审批信息和销假信息。学生可以查看教师发布的学生签到信息,可以自己选择加入班课信息,添加请假信息,查看审批信息,进行销假操作。基于微信小程序的生签到系统服务端用Java开发的网站后台,接收并且处理微信小程序端传入的json数据,数据库用到了MySQL数据库作为数据的存储。这样就让用户用着方便快捷,都通过同一个后台进行业务处理,而后台又可以根据并发量做好部署,用硬件和软件进行协作,满足于数据的交互式处理,让用户的数据存储更安全,得到数据更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值