hdu Color the ball

本文详细介绍了树状数组中update函数的应用,特别关注了如何进行逆序更新操作。通过实例代码演示,帮助读者理解并掌握这一算法的高级用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556

 

树状数组的  update的应用,逆序更新

 

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <limits.h> 
 5 #include <algorithm>
 6 #include <iostream>
 7 #include <ctype.h>
 8 #include <iomanip>
 9 #include <queue>
10 #include <map>
11 #include <stdlib.h>
12 using namespace std;
13 
14 int c[100010],n;
15 
16 int lowbit(int x)
17 {
18     return x&(-x);
19 }
20 
21 void update(int x,int y)
22 {
23     while(x>0){
24         c[x]=c[x]+y;
25         x=x-lowbit(x);
26     }
27 }
28 
29 int sum(int x)
30 {
31     int r=0;
32     while(x<=n){
33         r+=c[x];
34         x+=lowbit(x);
35     }
36     return r;
37 }
38 
39 int main()
40 {
41     int a,b;
42     while(~scanf("%d",&n)&&n!=0){
43         memset(c,0,sizeof(c));
44         for(int i=1;i<=n;i++){
45             scanf("%d%d",&a,&b);
46             update(b,1);
47             update(a-1,-1);
48         }
49         for(int i=1;i<n;i++){
50             printf("%d ",sum(i));
51         }
52         printf("%d\n",sum(n));
53     }
54 }

 

转载于:https://www.cnblogs.com/wangmengmeng/p/4975602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值