我们知道我们利用树状数组维护的是存到其中的a[ ]数组,但是我们做题需要的是sum[ ]数组,这才是我们真正需要的有用的信息,写这篇博客的目的便是整理一下sum数组是怎么样来应用解题的。

1. Stars

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

For example, look at the map shown on the figure above. Level
of the star number 5 is equal to 3 (it's formed by three stars with a
numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are
1. At this map there are only one star of the level 0, two stars of the
level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map.

Input

The first line of the input file contains a number of stars N
(1<=N<=15000). The following N lines describe coordinates of stars
(two integers X and Y per line separated by a space,
0<=X,Y<=32000). There can be only one star at one point of the
plane. Stars are listed in ascending order of Y coordinate. Stars with
equal Y coordinates are listed in ascending order of X coordinate.

Output

The output should contain N lines, one number per line. The first
line contains amount of stars of the level 0, the second does amount of
stars of the level 1 and so on, the last line contains amount of stars
of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
 
题目意思:有N个星星,给出N个星星的笛卡尔坐标,但是坐标给出的顺序有一定的规律,按照y坐标从小到大给出,当y坐标相同的时候按照x坐标的大小顺序给出;之后输出处在各个等级的星星的个数,等级就是看y坐标和x坐标同时小于等于这个星星的个数之和。
 
解题思路:我们直达星星坐标的y是按照从小到大给出的,那么我们算一个星星的等级只要判断有多少个星星的x是不是大于等于现在输入的x不就好了吗?
或许还可以这样理解,星星的等级是它左下角的个数,而按照y从小到大的排序,y是从小的位置依次攀升到大的位置,纵使后面的星星的x坐标可能会小于前面星星的x坐标,但将这些星星压缩到一个高度,左边包含自身位置星星的个数就是所求的等级。
在这里得到sum数组是lev数组(等级数组)的下标。
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int lev[];
int c[];///树状数组
int lowbit(int x)
{
return x&(-x);
}
int Getsum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
void update(int x,int n)
{
while(x<)
{
c[x]++;///将星星x坐标加1
x+=lowbit(x);
}
}
int main()
{
int x,y,i,n;
while(scanf("%d",&n)!=EOF)
{
memset(lev,,sizeof(lev));
memset(c,,sizeof(c));
for(i=; i<n; i++)
{
scanf("%d%d",&x,&y);
x++;///要注意坐标有可能为0,为0时会死循环,所以在读入坐标时应加1。
lev[Getsum(x)]++;///每次更新进一个星星,都要统计这个星星之前的星星的个数
update(x,);
}
for(i=; i<n; i++)
{
printf("%d\n",lev[i]);
}
}
return ;
}

2.Color the ball

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 解题思路;这道题我们在前面学习线段树的时候已经做过了,当时我们的思路是每涂一段色就相当区域更新(该区域每一点都+1),然后单点查询最后每一个点的状态。
也就是线段数的区段更新,单点查找
现在我们用树状数组来思考一下,这里我们使用一种技巧,比如我们要将[3,6]区段的数据+1,维护数据的过程我们不必去理会,使用树状数组,我们只需要将3那个点更新+1,和6之后的一个点
也就是7更新为-7就可以了。为什么要这样呢?因为我们最后需要的是那个sum数组,也就是要维护的前缀和!比如原先【3,6】区间全是0,那么此番操作后
sum[1]=0;
sum[2]=0;
sum[3]=1;
sum[4]=1;
sum[5]=1;
sum[6]=1;
sum[7]=0;
sum[8]=0;
发现了吗?现在的前缀和sum数组竟然可以来表示更新后单点的数据!
其实这也是树状数组的区段更新单点查找!
 #include <cstdio>
#include <cstring>
#define MAXN 100010
int c[MAXN];
int n;
int lowbit( int x )
{
return x&(-x);
}
void add( int i,int val) ///更新
{
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
int Getsum(int i)
{
int sum=;
while(i>)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
int i,x,y;
while(scanf("%d",&n)!=EOF)
{
if(n==)
{
break;
}
memset(c,,sizeof(c));
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,);
add(y+,-);
}
for(i=;i<=n;i++)
{
if(i==n)
{
printf("%d\n",Getsum(n));
}
else
{
printf("%d ",Getsum(i));
}
}
}
return ;
}

3.Japan

 
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.
Input
The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.
Output
For each test case write one line on the standard output: 
Test case (case number): (number of crossings)

Sample Input
1
3 4 4
1 4
2 3
3 2
3 1
Sample Output
Test case 1: 5

题目意思:

日本岛东海岸与西海岸分别有N和M个城市,现在修高速公路连接东西海岸的城市,求交点个数。

解题思路:

记每条告诉公路为(x,y), 即东岸的第x个城市与西岸的第y个城市修一条路。当两条路有交点时,满足(x1-x2)*(y1-y2) < 0。所以,将每条路按x从小到达排序,若x相同,按y从小到大排序。 然后按排序后的公路用树状数组在线更新,求y的逆序数之 和 即为交点个数。

由于x是从小到大排序的,假设当前我们在处理第k条边,那么第1~k-1条边的x必然是小于(等于时候暂且不讨论)第k条边的 x 的,那么前k-1条边中,与第k条边相交的边的y值必然大于yk的,所以此时我们只需要求出在前k-1条边中有多少条边的y值在区间[yk, M]即可,也就是求yk的逆序数,M为西岸城市个数,即y的最大值。 所以就将问题转化成区间求和的问题,树状数组解决。当两条边的x相同时,我们记这两条边的y值分别为ya,yb(ya<yb),我们先处理(x,ya),再处理(x,yb),原因很明显,因为当x相同时,这两条边是认为没有交点的,若先处理(x,yb),那么下次处理(x,ya)时,(x,ya)就会给(x,yb)增加一个逆序,也就是将这两条边做相交处理了,这是不正确的。


 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define N 1005*1005
LL ans;
struct node
{
int l;
int r;
} a[N];
int n,c[N];
int my_cmp(node a,node b)
{
if(a.l!=b.l)
return a.l<b.l;
return a.r<b.r;
}
int lowbit(int x)
{
return x&-x;
}
int Getsum(int x)
{
int ret = ;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} void add(int x,int d,int y)
{
while(x<=y)
{
c[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i,j,k,l,r,t,cas = ,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&x,&y,&n);
memset(c,,sizeof(c));
for(i = ; i<=n; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+,a++n,my_cmp);
ans = ;
for(i = ; i<=n; i++)
{
add(a[i].r,,y);
ans+=Getsum(y)-Getsum(a[i].r);
}
printf("Test case %d: %lld\n",cas++,ans);
}
return ;
}
 

树状数组怒刷sum!!!(前缀和应用)的更多相关文章

  1. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  2. 树状数组的理解(前缀和 and 差分)

    二更—— 有神仙反映数星星那个题外链炸了,我决定把图给你们粘一下,汉语翻译的话在一本通提高篇的树状数组那一章里有,同时也修改了一些汉语语法的错误 这段时间学了线段树组,当神仙们都在学kmp和hash的 ...

  3. ACdreamoj 1011(树状数组维护字符串hash前缀和)

    题目链接:http://acdream.info/problem? pid=1019 题意:两种操作,第一种将字符串某个位置的字符换为还有一个字符.另外一种查询某个连续子序列是否是回文串: 解法:有两 ...

  4. Nowcoder farm ( 树状数组、二维前缀和、二维偏序 )

    题目链接 分析 : 最简单的想法当然就是去模拟 直接对每个施肥料的操作进行模拟.然后计算贡献 但是这显然会超时.这题需要换一个思维 对于一个土地(也就是二维平面上的一个点)的种类是 T' 如果它被操作 ...

  5. 1042.D Petya and Array 前缀 + 树状数组

    11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...

  6. Codeforces 899 F. Letters Removing (二分、树状数组)

    题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...

  7. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

  8. 【HDU4947】GCD Array (莫比乌斯反演+树状数组)

    BUPT2017 wintertraining(15) #5H HDU- 4947 题意 有一个长度为l的数组,现在有m个操作,第1种为1 n d v,给下标x 满足gcd(x,n)=d的\(a_x\ ...

  9. ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解

    题意:给你数组a,有两个操作 1 l r,计算l到r的答案:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that ...

随机推荐

  1. vue面试题!!!

    由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...

  2. Web的基本工作原理、HTTP协议和URL说明

    Web工作原理 客户端和Web服务器通过HTTP协议进行通信.Web服务器有是也叫HTTP服务器或Web容器.HTTP协议采用的是请求/响应模式.即客户端发起HTTP请求,web服务器接收并解析处理H ...

  3. js基础(闭包实例)

    1,常用发送短信的闭包实现: function sms() { var count = 60; return { start: function() { if(count == 0) { count ...

  4. 浅析中国剩余定理(从CRT到EXCRT))

    前置知识 1. a%b=d,c%b=e, 则(a+c)%b=(d+e)%b(正确性在此不加证明) 2. a%b=1,则(d\(\times\)a)%b=d%b(正确性在此不加证明) 下面先看一道题(改 ...

  5. SQL分页过多时, 如何优化

    问题: 我们经常会使用到分页操作,这里有个问题,在偏移量非常大的时候,它会导致MySQL扫描大量不需要的行然后再抛弃掉.如: , ; 上述这条SQL语句需要查询10020条记录然后只返回最后20条.前 ...

  6. ionic ios 打包发布流程

    1.ionic cordova resources ios    在windows下 生成ios资源包 2.拷贝ionic 项目到mac电脑 不用拷贝platforms 并解压 3.正常情况下wido ...

  7. 将select的默认小三角替换成别的图片,且实现点击图片出现下拉框选择option

    最近做项目,要求修改select下拉框的默认三角样式,因为它在不同浏览器的样式不同且有点丑,找找网上也没什么详细修改方法,我就总结一下自己的吧. 目标是做成下图效果: 图一:将默认小三角换成红圈的三角 ...

  8. sql for xml path 处理

    1.将下列结果集 做成 aa   语文,数学 bb    英语,语文 这种格式 使用 for xml  path  记得去重复 WITH cte AS(SELECT stu.studentname,c ...

  9. Spring Web Async异步处理#Callable #DeferredResult

    Spring MVC 对于异步请求处理的两种方式 场景: Tomcat对于主线程性能瓶颈,当Tomcat请求并发数过多时,当线程数满时,就会出现请求等待Tomcat处理,这个时候可以使用子线程处理业务 ...

  10. 【转载++】fopen返回0(空指针NULL)且GetLastError是0

    结论来看,是一个简单又朴素的道理——打开文件句柄用完了得给关上.表现在现象上却是着实让人费解,以至于有人还怀疑起了微软的Winodws系统来了,可笑至极.还是那句话,先把自己的屁股先给擦干净喽再怀疑别 ...