Codeforces 835C

来源:互联网 发布:两电一邮 知乎 编辑:程序博客网 时间:2024/06/08 02:42


C. Star sky


time limit per test 2seconds

memory limit per test     256megabytes


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

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

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

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

Input

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

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

The nextq lines contain the views description. The i-th from these lines contains fiveintegers 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 theviewed 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 moment2 its brightness is 3, so the answer is 3.

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

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

 


【题意】

有一片100*100的星空,上面有n颗星星,每个星星有一个亮度,且在0~C范围内周期性变化,现在给出q个查询,每个查询给出时间和一个矩形,求在该时间时矩形内星星的亮度和。


【思路】

由于数据范围较大,暴力去扫一遍一定不行。那么我们考虑去预处理。使每次查询都能在O(1)的时间内得到矩形内的星星。


不过由于星星亮度不同,无法直接计算,且亮度的范围较小,我们可以把不同亮度的星星分开统计,用dp[i][j][k]表示左上顶点为(1,1),右下顶点为(i,j)的矩形范围内初始亮度为k的星星数量。


这个数组的得到用一下下面的转移方程就好了

sum[i][j][k]=sum[i][j][k]+(sum[i-1][j][k]+sum[i][j-1][k]-sum[i-1][j-1][k]);


在每次查询时初始亮度不同的星星分开处理,得到左下角坐标为(x,y),右上角坐标为(xx,yy)构成的矩形内,初始亮度为k的星星数量的方程为


ans=sum[xx][yy][k]+sum[x-1][y-1][k]-sum[x-1][yy][k]-sum[xx][y-1][k];


(上面两个式子的得到其实就是容斥的思想,画个图就很好理解了)


然后就算出t时刻亮度*数量,最后累加即可。


#include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long  ll;const int maxn = 105;const ll mod = 1e9+7;const int INF = 0x3f3f3f;const double eps = 1e-9;int n,q,c;int sum[maxn][maxn][15];void init(){    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]);    }}int get_num(int k,int x,int y,int xx,int yy){    return sum[xx][yy][k]+sum[x-1][y-1][k]-sum[x-1][yy][k]-sum[xx][y-1][k];}int main(){    int x,y,xx,yy,t,k;    while(~scanf("%d%d%d",&n,&q,&c))    {        mst(sum,0);        for(int i=0;i<n;i++)        {            scanf("%d%d%d",&x,&y,&k);            sum[x][y][k]++;        }        init();        for(int i=0;i<q;i++)        {            scanf("%d%d%d%d%d",&t,&x,&y,&xx,&yy);            int ans=0;            for(int i=0;i<=c;i++)            {                int val=(t+i)%(c+1);                ans+=val*get_num(i,x,y,xx,yy);            }            printf("%d\n",ans);        }    }    return 0;}






原创粉丝点击