树状数组怒刷sum!!!(前缀和应用)

时间:2022-01-12 15:04:39

我们知道我们利用树状数组维护的是存到其中的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.
树状数组怒刷sum!!!(前缀和应用)

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