-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightOJ__1081.c
executable file
·168 lines (144 loc) · 4.33 KB
/
lightOJ__1081.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <string.h>
#define ROW 513
#define NOTFOUND -9999
typedef struct{
int x, y;
}point;
typedef struct{
point leftCorner, rightCorner, value;
}node;
long nwChild( long index ) { index++; return(index<<2) - 3 ; }
long neChild( long index ) { index++; return (index<<2) - 2 ; }
long seChild( long index ) { index++; return (index<<2) - 1 ; }
long swChild( long index ) { index++; return (index<<2) ; }
long array[ROW][ROW];
node tree[(ROW*ROW)<<2];
void maximumValue(point *res, point *childRes){
int i;
*res = childRes[0];
for ( i = 1 ; i < 4 ; i++ ){
if ( res->x == NOTFOUND ) *res = childRes[i];
else if ( childRes[i].x != NOTFOUND && res->x != NOTFOUND
&& array[childRes[i].x][childRes[i].y] > array[res->x][res->y] )
*res = childRes[i];
}
}
void initializeTree( point *result, long index, int startx, int starty, int endx, int endy ){
point childResult[4];
int midx, midy;
int i;
tree[index].leftCorner.x = startx, tree[index].rightCorner.x = endx;
tree[index].leftCorner.y = starty, tree[index].rightCorner.y = endy;
if ( startx == endx && starty == endy ){
tree[index].value.x = startx, tree[index].value.y = starty;
*result = tree[index].value;
return;
}
if ( startx > endx || starty > endy ){
tree[index].value.x = NOTFOUND, tree[index].value.y = NOTFOUND;
*result = tree[index].value;
return;
}
midx = ( startx + endx ) >> 1, midy = ( starty + endy ) >> 1;
initializeTree( childResult, nwChild(index), startx, starty, midx, midy);
initializeTree( childResult+1, neChild(index), startx, midy + 1 , midx, endy);
initializeTree( childResult+2, seChild(index), midx + 1, midy + 1, endx, endy);
initializeTree( childResult+3, swChild(index), midx + 1, starty, endx, midy);
maximumValue(result, childResult);
tree[index].value = *result;
}
int _startx, _starty, _endx, _endy;
void query( point *result, long index){
point childResult[4];
if ( tree[index].leftCorner.x > tree[index].rightCorner.x || tree[index].leftCorner.y > tree[index].rightCorner.y ){
result->x = NOTFOUND, result->y = NOTFOUND;
return;
}
if ( tree[index].leftCorner.x > _endx || tree[index].leftCorner.y > _endy
|| tree[index].rightCorner.x < _startx || tree[index].rightCorner.y < _starty ){
result->x = NOTFOUND, result->y = NOTFOUND;
return;
}
if ( tree[index].leftCorner.x >= _startx && tree[index].leftCorner.y >=_starty &&
tree[index].rightCorner.x <= _endx && tree[index].rightCorner.y <= _endy ){
*result = tree[index].value;
return;
}
query( childResult, nwChild(index));
query( childResult + 1, neChild(index));
query( childResult + 2, seChild(index));
query( childResult + 3, swChild(index));
maximumValue(result, childResult);
}
int main(){
int i, j, n, x, test, kase;
long q;
point tmp;
int powerTwo[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
/*freopen("lightOJ__1081.in", "r", stdin);*/
/*freopen("lightOJ__1081.out", "w", stdout);*/
while ( scanf("%d", &test ) == 1 ){
for ( kase = 1 ; kase <= test ; kase++ ){
scanf("%d%ld", &n, &q);
for ( i = 0 ; i < n ; i++ ){
for ( j = 0 ; j < n ; j++ ) scanf("%ld", &array[i][j]);
}
initializeTree(&tmp, 0, 0, 0, n-1, n-1);
printf("Case %d:\n", kase);
while (q--){
scanf("%d%d%d", &i, &j, &x);
i--, j--;
_startx = i, _starty = j;
_endx = i + x - 1, _endy = j + x - 1;
query(&tmp, 0);
printf("%ld\n", array[tmp.x][tmp.y]);
}
}
}
return 0;
}
/*
3
4 10
67 1 2 3
8 88 21 1
89 12 0 12
5 5 5 5
1 1 2
1 3 2
3 3 2
1 1 4
2 2 3
1 1 3
2 2 2
1 3 2
3 1 2
2 3 2
4 10
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 1 2
1 3 2
3 3 2
1 1 4
2 2 3
1 1 3
2 2 2
1 3 2
3 1 2
2 3 2
5 5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
1 1 3
2 2 3
3 3 3
3 1 2
1 2 4
*/