#include <stdio.h>
#include <stdlib.h>
#define M 1000000
#define MD 0x7fffffff
#define X 123456789
long long min(long long a, long long b) { return a < b ? a : b; }
long long *ek[M]; int *ev[M], eo[M];
void init() {
int h;
for (h = 0; h < M; h++) {
ek[h] = (long long *) malloc(2 * sizeof *ek[h]);
ev[h] = (int *) malloc(2 * sizeof *ev[h]);
}
}
int hash(long long k) {
int k1 = k / MD, k2 = k % MD;
return ((long long) k1 * X + k2) % MD * X % MD % M;
}
void put(long long k, int v) {
int h = hash(k), o = eo[h]++;
if (o >= 2 && (o & o - 1) == 0) {
ek[h] = (long long *) realloc(ek[h], o * 2 * sizeof *ek[h]);
ev[h] = (int *) realloc(ev[h], o * 2 * sizeof *ev[h]);
}
ek[h][o] = k, ev[h][o] = v;
}
int get(long long k) {
int h = hash(k), o;
for (o = eo[h]; o--; )
if (ek[h][o] == k)
return ev[h][o];
return -1;
}
int c, cnt;
int solve(long long x1, long long y1, long long x2, long long y2) {
long long y, z;
int w;
if (x1 + y1 * c <= 0)
return 0;
if (x2 + y2 * c <= 0)
return 1;
if (x1 <= 0) {
if (x2 > 0) {
if (x2 >= c)
return 0;
if (y2 > 0) {
if (x2 - x1 >= c)
return 0;
return !solve(x2, y2, x1 + c, y1 - 1);
}
y = -x1 / (c - x2) + 1;
return y <= y1 && solve(x1 + y * (c - x2), y1 - y, x2, y2);
} else {
y = min(-x1 / c + 1, -x2 / c + 1);
return solve(x1 + y * c, y1 - y, x2 + y * c, y2 - y);
}
}
if (x1 >= x2 && (x1 >= c || y1 > 0))
return 1;
if (x2 >= c)
return !solve(x2 - x1, y2, x1, y1);
z = x1 >= 0 && x1 <= c && y1 >= 0 && y1 <= c && x2 >= 0 && x2 <= c && y2 >= 0 && y2 <= c ? (((long long) x1 * (c + 1) + y1) * (c + 1) + x2) * (c + 1) + y2 : -1;
z = -1;
if (z != -1 && (w = get(z)) != -1)
return w;
if (cnt < M)
cnt++;
else
z = -1;
if (!solve(x2 - x1, y2, x1, y1)) {
if (z != -1)
put(z, 1);
return 1;
}
w = x2 < c && y1 > 0 && solve(x1 + (c - x2) * y1, 0, x2, y2);
if (z != -1)
put(z, w);
return w;
}
int main() {
int t;
init();
scanf("%d%d", &c, &t);
while (t--) {
long long x1, y1, x2, y2;
scanf("%lld%lld%lld%lld", &x1, &y2, &x2, &y1), x1 -= y1 * c, x2 -= y2 * c;
printf(solve(x1, y1, x2, y2) ? "YES\n" : "NO\n");
}
return 0;
}
Compilation message
gradingserver.c: In function 'put':
gradingserver.c:30:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
30 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
gradingserver.c: In function 'main':
gradingserver.c:99:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d%d", &c, &t);
| ^~~~~~~~~~~~~~~~~~~~~
gradingserver.c:103:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%lld%lld%lld%lld", &x1, &y2, &x2, &y1), x1 -= y1 * c, x2 -= y2 * c;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
79312 KB |
Output is correct |
2 |
Correct |
43 ms |
79188 KB |
Output is correct |
3 |
Correct |
37 ms |
79184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
80212 KB |
Output is correct |
2 |
Correct |
123 ms |
80208 KB |
Output is correct |
3 |
Correct |
114 ms |
80200 KB |
Output is correct |
4 |
Correct |
37 ms |
79184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
79188 KB |
Output is correct |
2 |
Correct |
83 ms |
80172 KB |
Output is correct |
3 |
Correct |
80 ms |
80212 KB |
Output is correct |
4 |
Correct |
87 ms |
80208 KB |
Output is correct |
5 |
Correct |
84 ms |
80208 KB |
Output is correct |
6 |
Incorrect |
80 ms |
80208 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |