Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前綴和
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 s
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 (x
A star lies in a rectangle if it lies on its border or lies strictly inside it.
InputThe first line contains three integers n, q, c (1 ≤ n
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.
OutputFor each view print the total brightness of the viewed stars.
Examples Input2 3 3 1 1 1 3 2 0 2 1 1 2 2 0 2 1 4 5 5 1 1 5 5Output
3 0 3Input
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 51Output
3 3 5 0Note
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題有毒,每次都會做,每次都A不了。
Code
1 /** 2 * Codeforces 3 * Problem#835C 4 * Accepted 5 * Time:156ms 6 * Memory:3700k 7 */ 8 #include <bits/stdc++.h> 9 #ifndef WIN32 10 #define Auto "%lld" 11 #else 12 #define Auto "%I64d" 13 #endif 14 using namespace std; 15 typedef bool boolean; 16 const signed int inf = (signed)((1u << 31) - 1); 17 const signed long long llf = (signed long long)((1ull << 61) - 1); 18 const double eps = 1e-6; 19 const int binary_limit = 128; 20 #define smin(a, b) a = min(a, b) 21 #define smax(a, b) a = max(a, b) 22 #define max3(a, b, c) max(a, max(b, c)) 23 #define min3(a, b, c) min(a, min(b, c)) 24 template<typename T> 25 inline boolean readInteger(T& u){ 26 char x; 27 int aFlag = 1; 28 while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1); 29 if(x == -1) { 30 ungetc(x, stdin); 31 return false; 32 } 33 if(x == ‘-‘){ 34 x = getchar(); 35 aFlag = -1; 36 } 37 for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘); 38 ungetc(x, stdin); 39 u *= aFlag; 40 return true; 41 } 42 43 int n, q, c; 44 int xs[100005], ys[100005], ss[100005]; 45 int sum[105][105][11]; 46 47 inline void init() { 48 scanf("%d%d%d", &n, &q, &c); 49 for(int i = 1; i <= n; i++) { 50 scanf("%d%d%d", xs + i, ys + i, ss + i); 51 } 52 } 53 54 inline void getPreSum() { 55 memset(sum, 0, sizeof(sum)); 56 for(int i = 1; i <= n; i++) { 57 sum[xs[i]][ys[i]][ss[i]]++; 58 } 59 for(int i = 1; i <= 100; i++) { 60 for(int j = 1; j <= 100; j++) { 61 for(int k = 0; k <= 10; k++) 62 sum[i][j][k] += sum[i - 1][j][k] + sum[i][j - 1][k] - sum[i - 1][j - 1][k]; 63 } 64 } 65 } 66 67 inline int getAns(int x, int y, int t0) { 68 int rt = 0; 69 for(int i = 0; i <= 10; i++) 70 rt += (sum[x][y][i]) * ((i + t0) % (c + 1)); 71 return rt; 72 } 73 74 inline void solve() { 75 int t0, x0, y0, x1, y1; 76 while(q--) { 77 scanf("%d%d%d%d%d", &t0, &x0, &y0, &x1, &y1); 78 printf("%d\n", getAns(x1, y1, t0) - getAns(x0 - 1, y1, t0) - getAns(x1, y0 - 1, t0) + getAns(x0 - 1, y0 - 1, t0)); 79 } 80 } 81 82 int main() { 83 init(); 84 getPreSum(); 85 solve(); 86 return 0; 87 }
Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前綴和