time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xiyi), 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 (x1iy1i) and the upper right — (x2iy2i). 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 nqc (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 xiyisi(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 tix1iy1ix2iy2i (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.

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 109
#define N 100
/*
坐标的最大值比较小 开前缀数组求和
*/
LL a[MAXN][MAXN][];
LL n, q, c;
int main()
{
cin >> n >> q >> c;
int t1, t2, t3, t4;
for (LL i = ; i < n; i++)
{
cin >> t1 >> t2 >> t3;
a[t1][t2][t3]++;
}
for(int i =;i<=;i++)
for(int j=;j<=;j++)
for (int k = ; k < c + ; k++)
{
a[i][j][k] += a[i - ][j][k] + a[i][j - ][k] - a[i - ][j - ][k];
}
int T;
int x1, y1, x2, y2;
while (q--)
{
cin >> T >> x1 >> y1 >> x2 >> y2;
LL cnt = , sum = ;
for (int i = ; i < c + ; i++)
{
cnt = a[x2][y2][i] - a[x2][y1 - ][i] - a[x1 - ][y2][i] + a[x1 - ][y1 - ][i];
sum += cnt*((i + T) % (c + ));
}
cout << sum << endl;
}
}

错误的二分代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 100005
#define N 100
/*
二分查找区域内符合范围的点,取模运算求和
...
*/
struct star
{
LL x, y, s;
bool operator<(const star& rhs)
{
if (x == rhs.x)
return y < rhs.y;
else
return x < rhs.x;
}
}a[MAXN];
LL n, q, c;
LL t, X1, Y1, X2, Y2;
LL sum = ;
void solve(LL l, LL r)
{
if (l > r)
return;
if (l == r)
{
if (a[l].x >= X1&&a[l].x >= Y1&&a[l].x <= X2&&a[l].y <= Y2)
sum += (a[l].s + t) % (c + );
return;
}
LL mid = (l + r) / ;
//cout << mid << endl;
if (a[mid].x > X2)
solve(l, mid - );
else if (a[mid].x < X1)
solve(mid + , r);
else
{
if (a[mid].x >= X1&&a[mid].x >= Y1&&a[mid].x <= X2&&a[mid].y <= Y2)
sum += (a[mid].s + t) % (c + );
solve(l, mid - );
solve(mid + , r);
}
}
int main()
{
scanf("%lld%lld%lld", &n, &q, &c);
for (LL i = ; i < n; i++)
scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].s);
sort(a, a + n);
while (q--)
{
sum = ;
scanf("%lld%lld%lld%lld%lld", &t, &X1, &Y1, &X2, &Y2);
solve(, n-);
printf("%lld\n", sum);
}
return ;
}

C. Star sky 二维前缀和的更多相关文章

  1. Codeforces 835C - Star sky - [二维前缀和]

    题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...

  2. Star sky 二维前缀和

    C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

  4. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  5. poj-3739. Special Squares(二维前缀和)

    题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...

  6. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  7. 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

    题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...

  8. 二维前缀和模板题:P2004 领地选择

    思路:就是使用二维前缀和的模板: 先放模板: #include<iostream> using namespace std; #define ll long long ; ll a[max ...

  9. 二维前缀和好题hdu6514

    #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...

随机推荐

  1. react key的作用

    react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的(例如你为一个组件设置key之后不能获取组件的这个key props),而是给react自己用的. 简单来说,react利用key ...

  2. 添加jar到mvn私服

    1.生成jar文件 2.jar目录下执行: mvn install:install-file -Dfile=jave-1.0.2.jar -DgroupId=joinery -DartifactId= ...

  3. JavaScript--DOM方法

    getElementsByName()方法 返回带有指定名称的节点对象的集合. 语法: document.getElementsByName(name) 与getElementById() 方法不同的 ...

  4. 用SpringMVC实现的上传下载方式二(多文件上传)

    参考来源:      http://blog.csdn.net/qq_32953079/article/details/52290208 1.导入相关jar包 commons-fileupload.j ...

  5. WordPress强制跳转https教程

    在互联网火热的今天,安全问题显得越来越重要,为了用户信息安全,很多热门网站都启用了https 有小伙伴就问:我启用了https,为什么访问的时候显示的还是http呢? 其实,有时候并不是因为我们ssl ...

  6. iOS 声明属性关键字讲解

    atomic: 原子操作(原子性是指事务的一个完整操作,操作成功就提交,反之就回滚. 原子操作就是指具有原子性的操作)在objective-c 属性设置里面 默认的就是atomic ,意思就是 set ...

  7. 使用morphia实现对mongodb的聚合查询

    morphia是谷歌的一个针对mongodb的数据化持久框架: 关于mongodb的介绍不在这里展示,直接进入主题:采用morphia实现对mongodb的聚合查询 这里获取所有学生的分数总和 spr ...

  8. dbcp数据源配置

    <bean id="dbcpDataSource"  class="org.apache.commons.dbcp.BasicDataSource" de ...

  9. Win7访问不了WINXP共享文件

    用win xp的机器可以访问,但用win 7的机器无法访问共享文件 提示:您没有权限访问.请与网络管理员联系请求访问权限 网上找了相应的资料 做了如下动作 1. 打开网上邻居→本地连接→属性里,“看是 ...

  10. CLISTCTRL2

    回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...