Codeforces 853C Star sky(二维前缀和)

原题链接

Problem Description

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.

Sample 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

Sample Output

3
3
5
0

题目大意

在一个平面上有n颗星星,它们有一个共同的最大亮度,都有自己的坐标和初始亮度,时间每过一秒,亮度就+1,如超过最大亮度就归零。现有q个询问,每个询问给出一个矩形和一个时间,问在这个时间,这个矩形内的星星的亮度总和。

解题思路

考虑到坐标范围比较小,星星的个数和询问数都达到了1e5,所以用一个数组mmp[i][j][p]表示在坐标 (i , j )上亮度为p的星星的个数。另设一个前缀和数组dp[p][i][j]表示坐标在(1,1)~(i , j )亮度为p的星星个数和,这个数组可由dp[p][i][j]=dp[p][i-1][j]+dp[p][i][j-1]-dp[p][i-1][j-1]+mmp[i][j][p]这个式子得到。对于每次询问,枚举亮度,通过dp[i][x2][y2]-dp[i][x1-1][y2]-dp[i][x2][y1-1]+dp[i][x1-1][y1-1]找到对应区域内亮度之和,加上时间再取模,再累加即可。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define db double
//#define rep(i,n) for(int i = 0;i < n; i++)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-8
#define MOD 1000000007ll
#define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0)))
using namespace std;
int mmp[105][105][12];
int dp[12][105][105];
int main() 
{

    cl(dp);
    int n,q,c;
    int tx,ty,tb;
    cin>>n>>q>>c;
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d%d",&tx,&ty,&tb);
        mmp[tx][ty][tb]++;
    }
    int time,x1,y1,x2,y2;
    for(int i=1;i<=100;++i)
    {
        for(int j=1;j<=100;++j)
        {
            for(int p=0;p<=c;++p)
            {
                dp[p][i][j]=dp[p][i-1][j]+dp[p][i][j-1]-dp[p][i-1][j-1]+mmp[i][j][p];
            }
        }
    }

    for(int z=1;z<=q;++z)
    {
        int res=0;
        scanf("%d%d%d%d%d",&time,&x1,&y1,&x2,&y2);
        for(int i=0;i<=c;++i)
        {
            res+=(((i+time)%(c+1))*(dp[i][x2][y2]-dp[i][x1-1][y2]-dp[i][x2][y1-1]+dp[i][x1-1][y1-1]));
        }
        printf("%d\n",res);
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/xj949967574/article/details/77141135