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. WKWebView 和 UIWebView 允许背景音乐自动播放(记录)

    WKWebView WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.allowsInlin ...

  2. servlet setCharacterEncoding setHeader 设置字符区别

    1. response.setCharacterEncoding("UTF-8"); 设置内容的字符集 2. response.setHeader("content-ty ...

  3. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...

  4. Android 性能优化(24)*性能工具之「Traceview,dmtracedump」Profiling with Traceview and dmtracedump :记录并查看函数调用栈*

    Profiling with Traceview and dmtracedump In this document Traceview Layout         Traceview工具界面介绍 T ...

  5. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  6. Eclipse 编译java文件后出错 左树无红叉

    问题描述: 今天遇见让人郁闷的问题,在项目工程中java文件编译通不过,eclipse在java文件中标示错误,但是却不不能在navigator的视图中像平常一样出现小红叉.通过clean proje ...

  7. [Windows Server 2012] SQL Server 备份和还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:SQL S ...

  8. Windows Socket五种I/O模型——代码全攻略(转)

    Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模式.可以通过多线程技术进行处理. 非阻塞模式:执行I/O操 ...

  9. Squid 正向代理

    实现通过特定设备对特定的网址访问加速 使用squid 正向代理 实现,区别于反向代理,两者区别的根本在于作为中转的服务器在一个完整的请求中是代表客户端还是代表服务器. 服务端设置 1.安装程序包(推荐 ...

  10. eclipse 新建 maven 项目 + 消除错误

    安装eclips以及maven自行去了解,这里不讲解 一.新建一个 maven 项目. 二.下一步选择项目架构 三.填写相关信息 group id: 一般都是 com点 项目名 aftriact.id ...