Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和
2017-08-01 09:33
393 查看
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.
Examples
Input
Output
Input
Output
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.
题目大意 天空中有一些星星,每个星星有一个初始亮度,如果一个星星的初始亮度为s, 那么在时刻t, 它的亮度为(s + t) % (c + 1)。有q个询问,询问在某一时刻天空中某个矩形内所有星星的亮度和。
x, y很小,c很小,而且有趣的是10 * 100 * 100 = 100000,标准cf数据范围。
所以考虑对每种星星的初始亮度搞一个前缀和。这样对于某一时刻,你可以算出某个矩形内亮度为x的星星数目。
于是这道题就很简单了。。
值得高兴的是,终于在考试的时候把C题A掉了。。。好开心。。一直认为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.
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.
题目大意 天空中有一些星星,每个星星有一个初始亮度,如果一个星星的初始亮度为s, 那么在时刻t, 它的亮度为(s + t) % (c + 1)。有q个询问,询问在某一时刻天空中某个矩形内所有星星的亮度和。
x, y很小,c很小,而且有趣的是10 * 100 * 100 = 100000,标准cf数据范围。
所以考虑对每种星星的初始亮度搞一个前缀和。这样对于某一时刻,你可以算出某个矩形内亮度为x的星星数目。
于是这道题就很简单了。。
值得高兴的是,终于在考试的时候把C题A掉了。。。好开心。。一直认为C题有毒,每次都会做,每次都挂。
Code
/** * Codeforces * Problem#835C * Accepted * Time:156ms * Memory:3700k */ #include <bits/stdc++.h> #ifndef WIN32 #define Auto "%lld" #else #define Auto "%I64d" #endif using namespace std; typedef bool boolean; const signed int inf = (signed)((1u << 31) - 1); const signed long long llf = (signed long long)((1ull << 61) - 1); const double eps = 1e-6; const int binary_limit = 128; #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) template<typename T> inline boolean readInteger(T& u){ char x; int aFlag = 1; while(!isdigit((x = getchar())) && x != '-' && x != -1); if(x == -1) { ungetc(x, stdin); return false; } if(x == '-'){ x = getchar(); aFlag = -1; } for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); ungetc(x, stdin); u *= aFlag; return true; } int n, q, c; int xs[100005], ys[100005], ss[100005]; int sum[105][105][11]; inline void init() { scanf("%d%d%d", &n, &q, &c); for(int i = 1; i <= n; i++) { scanf("%d%d%d", xs + i, ys + i, ss + i); } } inline void getPreSum() { memset(sum, 0, sizeof(sum)); for(int i = 1; i <= n; i++) { sum[xs[i]][ys[i]][ss[i]]++; } for(int i = 1; i <= 100; i++) { for(int j = 1; j <= 100; j++) { for(int k = 0; k <= 10; k++) sum[i][j][k] += sum[i - 1][j][k] + sum[i][j - 1][k] - sum[i - 1][j - 1][k]; } } } inline int getAns(int x, int y, int t0) { int rt = 0; for(int i = 0; i <= 10; i++) rt += (sum[x][y][i]) * ((i + t0) % (c + 1)); return rt; } inline void solve() { int t0, x0, y0, x1, y1; while(q--) { scanf("%d%d%d%d%d", &t0, &x0, &y0, &x1, &y1); printf("%d\n", getAns(x1, y1, t0) - getAns(x0 - 1, y1, t0) - getAns(x1, y0 - 1, t0) + getAns(x0 - 1, y0 - 1, t0)); } } int main() { init(); getPreSum(); solve(); return 0; }
相关文章推荐
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend Dp + 前缀 +离散化
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
- codeforces 354 div2 C Vasya and String 前缀和
- Codeforces 853B Round #433 Div2D& Div1B Jury Meeting:差分前缀和+模拟
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- C. Star sky(Codeforces Round #427 (Div. 2) C)
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- [最短路 主席树 Hash] Codeforces 464E #265 (Div. 1) E. The Classic Problem
- Codeforces 835C - Star sky 【预处理前缀和】
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- codeforces contest 855 problem B(前缀后缀)
- CodeForces - 835C Star sky (前缀和)
- Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces Round #427 (Div. 2) A. Key races B. The number on the board C. Star sky
- Codeforces Beta Round #29 (Div. 2, Codeforces format), problem: (C) Mail Stamps 图论
- Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力