Star sky CodeForces - 835C (二维前缀和)

本文介绍了一个星空模拟问题的解决方法,通过三维数组记录星星的位置和亮度变化,实现快速查询任意时刻指定矩形区域内的总亮度。

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

Star sky

CodeForces - 835C

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).

Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.

You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right — (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.

A star lies in a rectangle if it lies on its border or lies strictly inside it.


Input

The first line contains three integers n, q, c (1 ≤ n, q ≤ 105, 1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.

The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 ≤ xi, yi ≤ 100, 0 ≤ si ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness.

The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 ≤ ti ≤ 109, 1 ≤ x1i < x2i ≤ 100, 1 ≤ y1i < y2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle.

Output

For each view print the total brightness of the viewed stars.

Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note

Let's consider the first example.

At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.

At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.

At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.

题意:

给出 n个星星的坐标和它们的亮度si,星星最大亮度为c,t秒后星星的亮度为(si+t)%(c+1),q次询问,每次询问一个矩形区域内t秒后星星的亮度和。



思路:


开一个三维数组,记录每个前缀亮度为si的星星的个数即可

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sum[105][105][12];
int main(){
    int n,q,c;
    while(cin >> n >> q >> c){
        memset(sum,0,sizeof(sum));
        for(int i = 1; i <= n; i++){
            int x,y,v;
            cin >> x >> y >> v;
            sum[x][y][v]++;
        }
        for(int i = 1; i <= 100; i++){
            for(int j = 1; j <= 100; j++){
                for(int k = 0; k <= c; k++){
                    sum[i][j][k] += sum[i-1][j][k] + sum[i][j-1][k] - sum[i-1][j-1][k];
                }
            }
        }
        while(q--){
            int t,sx,sy,ex,ey,ans = 0;
            cin >> t >> sx >> sy >> ex >> ey;
            for(int i = 0; i <= c; i++){
                int x = (i + t) % (c + 1);
                ans += x * (sum[ex][ey][i] - sum[sx-1][ey][i] - sum[ex][sy-1][i] + sum[sx-1][sy-1][i]);
            }
            cout << ans << endl;
        }
    }
    return 0;
}

### Codeforces二维前缀和的应用 在解决涉及矩阵区域查询的问题时,二维前缀和是一种非常有效的工具。通过预先计算部分和,可以在常数时间内快速获取任意子矩形内的元素总和。 #### 什么是二维前缀和? 对于一个大小为 \( m \times n \) 的矩阵 `A`,定义其对应的前缀和矩阵 `sum` 如下: \[ sum[i][j] = A[0...i-1][0...j-1] \] 即 `sum[i][j]` 表示从原点 `(0, 0)` 到位置 `(i-1, j-1)` 所构成的矩形区域内所有元素之和[^2]。 为了方便处理边界情况,通常会将索引偏移一位,在实际编程中使用 `sum[i+1][j+1]` 来表示上述范围内的累加值。 #### 计算方法 构建前缀和的过程可以通过双重循环完成,时间复杂度为 O(mn),其中 m 和 n 分别代表矩阵的高度和宽度。核心代码如下所示: ```cpp for(int i = 1; i <= h; ++i){ for(int j = 1; j <= w; ++j){ // 当前格子加上左边、上面以及左上的三个方向已经累积的结果 sum[i][j] = A[i-1][j-1] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]; } } ``` 这里需要注意减去重复计算的部分 `- sum[i-1][j-1]`,因为这部分被前面两次相加操作多算了。 #### 查询指定矩形区域的和 假设要查询以坐标 `(x1,y1)` 作为左上角顶点,`(x2,y2)` 作为右下角顶点所围成的小矩形内部数值总和,则可以按照下面的方式进行计算: \[ query(x_1, y_1, x_2, y_2)=sum[x_2][y_2]-sum[x_1-1][y_2]-sum[x_2][y_1-1]+sum[x_1-1][y_1-1]\] 这同样遵循了容斥原理来排除重叠部分的影响。 #### 实际应用案例 考虑这样一个题目:“在一个整数矩阵中找到满足特定条件的最大/最小面积”。这类问题往往需要频繁地对不同尺寸的子矩形做求和运算,而借助于预处理好的前缀和表就可以大大简化这些操作并提高效率。 例如,在某些情况下可能还需要结合其他数据结构如线段树或者二分查找来进行更复杂的优化;而在另一些场景里则可以直接利用简单的四边形不等式性质加速搜索过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值