-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP12087.cpp
153 lines (143 loc) · 3.74 KB
/
P12087.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <stdio.h>
long get(int x, int y, long *querySumDown, long *querySumUp, long *querySumLeft, long *querySumRight) {
return querySumDown[y+1] + querySumUp[y-1] + querySumLeft[x-1] + querySumRight[x+1];
}
int readInt(char *w, int &idx) {
while(!isdigit(w[idx]))
++idx;
int ret = 0;
while(isdigit(w[idx])) {
ret = ret * 10 + (w[idx]-'0');
++idx;
}
return ret;
}
void writeInt(long n, char *w, int &idx) {
int len = 1;
long copy = n;
while(copy > 9) {
++len;
copy/=10;
}
for(int i = 0; i < len; ++i) {
w[idx+len-i-1] = '0'+(n%10);
n/=10;
}
idx+=len;
}
int main() {
char w[501*5];
int rowSums[502*502]; // (y+1)*(c+1) + x+1
int colSums[502*502];
long querySumRight[502];
long querySumUp[502];
long querySumLeft[502];
long querySumDown[502];
int T, r, c, q, val, x1, x2, y1, y2;
gets(w);
int idx = 0;
T = readInt(w, idx);
char testCaseStr[13] = "Test Case X:";
char outStr[39];
for(int cas = 1; cas <= T; ++cas) {
testCaseStr[10] = '0'+cas;
puts(testCaseStr);
gets(w); idx = 0;
r = readInt(w, idx);
c = readInt(w, idx);
q = readInt(w, idx);
int C = c+1;
//int R = r+1;
for(int y = 0; y <= r; ++y)
rowSums[y*C] = colSums[y*C] = 0;
for(int x = 0; x <= c; ++x)
rowSums[x] = colSums[x] = 0;
// Read matrix:
for(int y = 1; y <= r; ++y) {
gets(w); idx = 0;
for(int x = 1; x <= c; ++x) {
val = readInt(w, idx);
rowSums[y*C+x] = val + rowSums[y*C+x-1];
colSums[y*C+x] = val + colSums[(y-1)*C+x];
}
}
/*
// Debugging:
for(int y = 0; y <= r; ++y) {
for(int x = 0; x <= c; ++x) {
std::cerr << rowSums[y*C+x] << " ";
}
std::cerr << std::endl;
}
std::cerr << std::endl;
for(int y = 0; y <= r; ++y) {
for(int x = 0; x <= c; ++x) {
std::cerr << colSums[y*C+x] << " ";
}
std::cerr << std::endl;
}
std::cerr << std::endl;//*/
// Process queries:
for(int i = 1; i <= q; ++i) {
gets(w); idx = 0;
y1 = readInt(w, idx);
x1 = readInt(w, idx);
y2 = readInt(w, idx);
x2 = readInt(w, idx);
if(x1 > x2)
std::swap(x1, x2);
if(y1 > y2)
std::swap(y1, y2);
// Preprocess querySums:
long pop = 0;
querySumLeft[x1-1] = 0;
for(int xx = x1; xx < x2; ++xx) {
long add = colSums[y2*C+xx]-colSums[(y1-1)*C+xx];
pop += add;
querySumLeft[xx] = querySumLeft[xx-1] + pop;
}
pop = 0;
querySumUp[y1-1] = 0;
for(int yy = y1; yy < y2; ++yy) {
long add = rowSums[yy*C+x2]-rowSums[yy*C+x1-1];
pop += add;
querySumUp[yy] = querySumUp[yy-1] + pop;
}
pop = 0;
querySumRight[x2+1] = 0;
for(int xx = x2; xx > x1; --xx) {
long add = colSums[y2*C+xx]-colSums[(y1-1)*C+xx];
pop += add;
querySumRight[xx] = querySumRight[xx+1] + pop;
}
pop = 0;
querySumDown[y2+1] = 0;
for(int yy = y2; yy > y1; --yy) {
long add = rowSums[yy*C+x2]-rowSums[yy*C+x1-1];
pop += add;
querySumDown[yy] = querySumDown[yy+1] + pop;
}
// Walk to best in grid:
int bestX = x1;
int bestY = y1;
long best = get(x1, y1, querySumDown, querySumUp, querySumLeft, querySumRight), candidate;
while(bestY < y2 && (candidate = get(bestX, bestY+1, querySumDown, querySumUp, querySumLeft, querySumRight)) < best) {
++bestY;
best = candidate;
}
while(bestX < x2 && (candidate = get(bestX+1, bestY, querySumDown, querySumUp, querySumLeft, querySumRight)) < best) {
++bestX;
best = candidate;
}
// Output quickly:
idx = 0;
writeInt(i, outStr, idx);
outStr[idx++] = ' ';
writeInt(best, outStr, idx);
outStr[idx++] = '\0';
puts(outStr);
}
puts(""); // new line
}
}