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. 平方分割poj2104K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59798   Accepted: 20879 Ca ...

  2. 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...

  3. Docker学习系列(二):Docker三十分钟快速入门(上)

    一.背景 ​ 最近,Docker技术真是一片火热,它的出现也弥补了虚拟机资源消耗过高的问题,直接让虚拟化技术有了质的飞跃.那么本文我们来聊一聊Docker,和大家一起认识Docker,简单入门Dock ...

  4. SQL编程语句

    视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序,in 等都不能写视图的用法: select * from 视图名 SQL编程 定义变量:d ...

  5. Java 创建Excel并逐行写入数据

    package com.xxx.common.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  6. 常见Z纯CSS小样式合集(三角形)

    三角形 .sanjiao{ width:0px; height: 0px; overflow: hidden; border-width: 100px; border-color: transpare ...

  7. APP上线被APPStore拒绝的各种原因

    1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3.游戏里有实物奖励的话,一定要说清楚,奖励由本公司负责,和苹果没有关系.4.用到苹果的标志 ...

  8. npm换淘宝源 yarn换淘宝源

    查询初始的源 npm get registry > https://registry.npmjs.org/ 设置淘宝源 npm config set registry http://regist ...

  9. 【笔记JS/HTML/CSS】web中的HTTP协议(1)

    最近都在coursera刷课,加上自己课业也忙起来了,总是忘记写学习笔记ORZ 自省ing... 在写HTML的时候,form表单需要通过HTTP协议向服务器提交.查询数据(如下图) 客户端通过HTT ...

  10. NSAllowsArbitraryLoadsInWebContent NSAllowsArbitraryLoads

    By specifying NSAllowsArbitraryLoadsInWebContent, you are overriding NSAllowsArbitraryLoads on iOS 1 ...